/****************************************************************************** * * Project Name: WS603B Demo Program * File name: main.java * Version: 0.6 04/04/09 * * Copyright (C) 2009 by T. Bitson - All rights reserved. * * formated for tab = 2 spaces * * This code has only been minimally tested. Use at your own risk! *****************************************************************************/ package WS603aDemo; import java.util.Date; import java.io.*; import com.dalsemi.onewire.*; import com.dalsemi.onewire.adapter.*; import com.dalsemi.onewire.container.*; public class Main { // this is the name of the WS603B dual 1-Wire USB to serial interface public static final String ONE_WIRE_SERIAL_PORT = "/dev/tty.SLAB_USBtoUART"; // serial number of the DS2760 1-Wire Device in the WS603A private final String WS603B_ID = "51000000038EAB30"; // class constants public static final String ADAPTER_TYPE = "DS9097U"; // class variables public static boolean debugFlag = false; // 1-wire adapter object private DSPortAdapter adapter; // sensors private WS603a ws1; public Main() { // get the 1-wire adapter try { // get an instance of the 1-Wire adapter adapter = OneWireAccessProvider.getAdapter(ADAPTER_TYPE, ONE_WIRE_SERIAL_PORT); if (adapter != null) { System.out.println("Found Adapter: " + adapter.getAdapterName() + " on Port " + adapter.getPortName()); } else { System.out.println("Error: Unable to find 1-Wire adapter!"); System.exit(1); } // reset the 1-Wire bus resetBus(); } catch (OneWireException e) { System.out.println("Error Finding Adapter: "+ e); System.exit(1); } } public void mainLoop() { Date date = new Date(); boolean quit = false; InputStreamReader in = new InputStreamReader(System.in); float temp; float windSpeed; int windDir; // initialize WS603A ws1 = new WS603a(adapter, WS603B_ID); System.out.println("**** WS603A Demo Program version 0.6 ****"); System.out.println("Press 'q' to quit\n"); sleep(2000); // main program loop while(!quit) { System.out.println("Time = " + new Date()); // read WS603 temperature temp = ws1.getTemperature(); System.out.println("Temp = " + temp ); // get wind speed windSpeed = ws1.getWindSpeed(); System.out.println("Wind Speed = " + windSpeed ); // read direction windDir = ws1.getWindDir(); System.out.println("Wind Direction = " + windDir + " = " + ws1.getWindDirStr(windDir)); // LEDs: mode 0 = manual mode // mode 1 through 6 are auto blink mode // set LEDs to red if wind is > 2 MPH, otherwise set to blue if (windSpeed > 2) ws1.setLEDMode(0, ws1.LED_HIGH, ws1.LED_OFF); else ws1.setLEDMode(0, ws1.LED_OFF, ws1.LED_HIGH); System.out.println("\n"); // check for 'q' key press try { if (in.ready()) if (in.read() == 'q') { quit = true; } } catch (IOException e) { } // don't care // the WS603A internally updates once a second sleep(1000); } // end of main loop // set WS603A to auto blink mode on quit ws1.setLEDMode(1, ws1.LED_HIGH, ws1.LED_HIGH); // free up the serial port try { adapter.reset(); adapter.freePort(); } catch (OneWireException e) { System.out.println("Error Closing Adapter: "+ e); } System.out.println("Bye!") ;System.exit(0); } public static void main(String[] args) { try { // get instances to the primary object Main weatherServer = new Main(); // call the main program loop weatherServer.mainLoop(); } catch(Throwable t) { System.out.println("Exception: Main() " + t); } finally { System.out.println("Program Stopped"); System.exit(0); } } private void resetBus() // reset the 1-wire bus { System.out.println("Resetting 1-wire bus"); try { int result = adapter.reset(); if (result == 0) System.out.println("Warning: Reset indicates no Device Present"); if (result == 3) System.out.println("Warning: Reset indicates 1-Wire bus is shorted"); } catch (OneWireException e) { System.out.println("Exception Resetting the bus: " + e); } } // helper routine to simplfy the code public void sleep(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) {/* don't care */} } }