Wednesday, February 03, 2010

Setting up my first Weather station.

Setting up my first Weather station

I am setting up my first Weather station using the AAG usb 485 unit trying to measure wind speed to estimate if it is with wile to get a wind turbine for alternative energy.
The API dock for the rs485 interface is not very good and had find out with trial and error what the command is in hex.

here is the command to the weather meter to get readings on hex
[2B][77][73][03][00][EB][A1][8C]
= +ws[HEX VALUE OF NUMBER OF BITES][STATION IS IN HEX][COMMAND MODE][TYPE READ DATA][CHECKSUM]
The checksum is calculated as follows = 00 + EB + A1 = 186 and you only take the least significant byte which is 86

the reply for the weather station is as follows
+ws\0x0f\0x01\0xea\0xa1\0x00\0x00G\0x01\0x81\0x01\0xa0\0xae\0x88\0xff\0x005
this is as in the API dock

hope this helps.
ps here is a log off all the commands from the windows aplication accesing the wind meter.
Request: 4/26/2010 7:41:14 PM.34664
2B 77 73 04 00 EB B1 00 9C +ws..ë±.?
Answer: 4/26/2010 7:41:14 PM.67464 (+0.3281 seconds)
2B 77 73 07 01 EA B1 04 07 00 3C E3 +ws..ê±...<ã
Request: 4/26/2010 7:41:15 PM.23764 (+0.5625 seconds)
2B 77 73 04 00 EB B2 00 9D +ws..ë².�
Answer: 4/26/2010 7:41:15 PM.67464 (+0.4375 seconds)
2B 77 73 07 01 EA B2 01 03 03 00 A4 +ws..ê²....€
Request: 4/26/2010 7:41:16 PM.25264 (+0.5781 seconds)
2B 77 73 07 01 EB A1 00 00 00 00 8D +ws..ë¡....�
Answer: 4/26/2010 7:41:16 PM.67464 (+0.4219 seconds)
2B 77 73 0F 01 EA A1 0A 00 00 1C 00 C0 01 00 AF +ws..ê¡.....À..¯
00 FF 00 21 .ÿ.!
Request: 4/26/2010 7:41:19 PM.42464 (+2.7500 seconds)
2B 77 73 07 01 EB A2 00 C0 01 00 4F +ws..ë¢.À..O
Answer: 4/26/2010 7:41:20 PM.97164 (+0.5469 seconds)
2B 77 73 0F 01 EA A2 09 00 00 1C 00 C0 01 00 B0 +ws..ê¢.....À..°
00 FF 00 22 .ÿ."
Request: 4/26/2010 7:41:20 PM.31564 (+0.3438 seconds)
2B 77 73 07 01 EB A2 00 C0 01 00 4F +ws..ë¢.À..O
Answer: 4/26/2010 7:41:21 PM.83064 (+0.5156 seconds)
2B 77 73 0F 01 EA A2 09 00 00 1C 00 C0 01 00 AE +ws..ê¢.....À..®
00 FF 00 20 .ÿ.

Port closed

Here is a java program that will get the wind speed data from a AAG wind meter via the serial (RS485) port every 10 seconds and log the data in a log file.
The AAG station id must be one or change the program.

***beginning of code***

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;



/*
* AAGWetherLogger.java
* @author anton Janovsky
* This program use the RXTX api for serial port communication
* I haven't complimented all the wetherstation api yet will update newer version when completed
* Version 1.0
* Date 10/05/2010
*/

public class AAGWetherLogger implements SerialPortEventListener{


public AAGWetherLogger() {
// initComponents();
SetupSerialPort("com3");
sendDataToAAG();
while(true)
{
//System.out.println("doing wile loop");
byte[] b = {0x2B, 0x77, 0x73, 0x03, 0x00, (byte)0xEB, (byte)0xA2, (byte)0x8D};
dataCount = 0;
String getWindSpeedData = new String(b);
try {
//System.out.println("wait 10000");
Thread.sleep(10000);
} catch (Exception ex) {
ex.printStackTrace();
}
sendstring(getWindSpeedData);
}
}

//protected JTextArea textArea;
private final static String newline = "\n";

static CommPortIdentifier portId;
static CommPortIdentifier saveportId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
static int dataCount = 0;
Thread readThread;
static String messageString = "Hello, world!";
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
static int[] resevedString = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
public String myport = "COM3";
boolean portFound = false;
String defaultPort = "";
void SetupSerialPort(String port)
{
if (!myport.equals(""))
{
defaultPort = myport;
} else {
String osname = System.getProperty("os.name","").toLowerCase();
if ( osname.startsWith("windows") ) {
// windows
defaultPort = "COM1";
} else if (osname.startsWith("linux")) {
// linux
//defaultPort = "/dev/ttyS0";
defaultPort = "/dev/ttyUSB0";
} else if ( osname.startsWith("mac") ) {
// mac
defaultPort = "???";
} else {
//System.out.println("Sorry, your operating system is not supported");
System.exit(1);
}
}

//System.out.println("Set default port to "+defaultPort);

portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//System.out.println("Available port: " + portId.getName());
if (portId.getName().equals(defaultPort)) {
//System.out.println("Found port: "+defaultPort);
portFound = true;
//nulltest reader = new nulltest();
break;
}
}

}
if (!portFound) {
//System.out.println("port " + defaultPort + " not found.");
System.exit(1);
}


try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}

try {
inputStream = serialPort.getInputStream();
} catch (IOException e)
{
e.printStackTrace();
}

try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e)
{
e.printStackTrace();
}

serialPort.notifyOnDataAvailable(true);

try {
int boardrate = 9600;
int serialDataBits = SerialPort.DATABITS_8;
int serialStopBits = SerialPort.STOPBITS_1;
int serialParity = SerialPort.PARITY_NONE;
try {
serialPort.setSerialPortParams(boardrate, serialDataBits, serialStopBits, serialParity);
} catch (gnu.io.UnsupportedCommOperationException ex) {
//Logger.getLogger(SerialTerminal.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
//System.out.println("Board rate =" + boardrate);
//System.out.println("Data Bits =" + serialDataBits);
//System.out.println("Stop Bits =" + serialStopBits);
//System.out.println("Parity =" + serialParity);
} catch (Exception e)
{
e.printStackTrace();
}

initwritetoport();

}

public void initwritetoport() {
// initwritetoport() assumes that the port has already been opened and initialized

try {
outputStream = serialPort.getOutputStream();
} catch (IOException e)
{
e.printStackTrace();
}

try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
//System.out.println("Error setting event notification");
//System.out.println(e.toString());
e.printStackTrace();
System.exit(-1);
}

}

public void sendstring(String string) {

//System.out.println("Send: "+string);

try {
//serialPort.FLOWCONTROL_RTSCTS_OUT
serialPort.setRTS(true);
//System.out.println("set rts pin");
outputStream.write(string.getBytes());
//System.out.println("clear rts pin");
serialPort.setRTS(false);
} catch (IOException e)
{
e.printStackTrace();
}

}
public static int[] stringToHex(byte[] readBuffer,int numBytes)
{
//+ws\0x0f\0x01\0xea\0xa1\0x09\0x00\0x00G\0x01\0x81\0x01\0xa0\0xa9\0x8b\0xff\0x002
BigInteger biginteger = new BigInteger(readBuffer);
////System.out.println(result);
// BigInteger bi = new BigInteger(barray);
String s ="";
//String s = bi.toString(2); // 100100000111111110000 // Format to
////System.out.println(s);
s = biginteger.toString(16); // 120ff0
//System.out.println(s);
if (s.length() % 2 != 0)
{ // Pad with 0
s = "0"+s;
}
int loopcount = 0;
while ( dataCount < numBytes*2)
{
int iVal= Integer.parseInt(s.substring(dataCount,dataCount+2),16);
// System.out.print(":" + dataCount + " " + s.substring(dataCount,dataCount+2)+"="+ iVal) ;
resevedString[loopcount] = iVal;
//:0 2b=43:2 77=119:4 73=115:6 0f=15
//:0 01=1:2 ea=234:4 a1=161:6 0b=11:8 00=0:10 00=0:12 46=70:14 01=1:16 80=128:18 01=1:20 a0=160:22 bb=187:24 38=56:26 ff=255:28 00=0:30 f1=241
dataCount = dataCount +2;
loopcount++;
}
//System.out.println("returning data");
return resevedString;
}


public void serialEvent(SerialPortEvent event) {

switch (event.getEventType()) {
case SerialPortEvent.BI:
System.out.println("BI");
case SerialPortEvent.OE:
System.out.println("OE");
case SerialPortEvent.FE:
System.out.println("FE");
case SerialPortEvent.PE:
System.out.println("PE");
case SerialPortEvent.CD:
System.out.println("CD");
case SerialPortEvent.CTS:
System.out.println("CTS");
case SerialPortEvent.DSR:
System.out.println("DSR");
case SerialPortEvent.RI:
System.out.println("RI");
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[25];
try {
int numBytes = 0;
while (inputStream.available() > 0)
{
numBytes = inputStream.read(readBuffer);
}
String result = new String(readBuffer);
int hexStr[] = stringToHex(readBuffer,numBytes);
if (hexStr[0] == 43 && hexStr[1] == 119 && hexStr[2] == 115 && hexStr[4] == 1 && hexStr[5] == 234 && hexStr[6] == 162)
{
// System.out.println("\n");
// System.out.println(getDateTime());
// System.out.println("Direction = " + hexStr[7] + " " + getDirection(hexStr[7]) );
// System.out.println("Speed = " + hexStr[8]);
// System.out.println("Tempreture = " + hexStr[9]);
// System.out.println("Light mode = " + hexStr[10]);
// System.out.println("Light status = " + hexStr[11]);
// System.out.println("Light level = " + hexStr[12]);
// System.out.println("Light threshold = " + hexStr[13]);
// System.out.println("Voltage = " + hexStr[14]);
// System.out.println("Intensity = " + hexStr[15]);
// System.out.println("SpeedCal = " + hexStr[16]);
// System.out.println("DirectionCal = " + hexStr[17]);

System.out.print("\n");
System.out.print(getDateTime());
System.out.print("," + hexStr[7] + " " + getDirection(hexStr[7]) );
System.out.print("," + hexStr[8]);
System.out.print("," + hexStr[9]);
System.out.print("," + hexStr[10]);
System.out.print("," + hexStr[11]);
System.out.print("," + hexStr[12]);
System.out.print("," + hexStr[13]);
System.out.print("," + hexStr[14]);
System.out.print("," + hexStr[15]);
System.out.print("," + hexStr[16]);
System.out.print("," + hexStr[17]);

try{
// Create file
FileWriter fstream = new FileWriter("Wether_station" + getDate() +".log",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(getDateTime() + "," + hexStr[7] + "," + getDirection(hexStr[7]) + "," + hexStr[8] + "," + hexStr[9] + "," + hexStr[10] + "," + hexStr[11] + "," + hexStr[12] + "," + hexStr[13] + "," + hexStr[14] + "," + hexStr[15] + "," + hexStr[16] + "," + hexStr[17] + "," + hexStr[18] + "\n");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
for (int k=0; k < hexStr.length;k++)
{
//System.out.println("hex string =" + k + "=" + hexStr[k]);
}
result = result.substring(0, numBytes);

} catch (IOException e)
{
e.printStackTrace();
}

break;
}
}


private String getDirection(int directionVal) {


// 1=N 2=NNE, 3=NE,4=NEE, 5=E, 6=EES, 7=ES, 8=ESS, 9=S, 10=SSW, 11=SW,12=SWW, 13=W, 14=WWN, 15=WN, 16=WNN
String[] dirVal = {"?","N","NNE","NE","NEE","E","EES","ES","ESS","S","SSW","SW","SWW","W","WWN","WN","WNN"};


return dirVal[directionVal];
}


void sendDataToAAG()
{
// TODO add your handling code here:
String myport ="/dev/ttyUSB0";
//String getWindSpeedData = "+ws????";

int baudRate = 9600;
int dataBits = 8;
int StopBits = 1;
int parityBits = 0;
//App1 app1 = new App1(myport);
try {
serialPort.setSerialPortParams(baudRate, dataBits, StopBits, parityBits);
} catch (Exception ex) {
//Logger.getLogger(SerialTerminal.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
byte[] b = {0x2B, 0x77, 0x73, 0x03, 0x00, (byte)0xEB, (byte)0xA2, (byte)0x8D};
dataCount = 0;
//App1.resevedString;
String getWindSpeedData = new String(b);
sendstring(getWindSpeedData);
}
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd_MM_yyyy");
Date date = new Date();
return dateFormat.format(date);
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{

AAGWetherLogger aagwether = new AAGWetherLogger();

}


}
**end of code**

The Above programe creates the logfile Wether_station[date].log file.

Here is an exsample of the content of the file

10/05/2010 21:45:44,8,ESS,3,0,61,141,129,1,0,155,106,255,10
10/05/2010 21:45:54,11,SW,2,0,61,141,129,1,0,155,105,255,10
10/05/2010 21:46:14,2,NNE,2,0,61,141,129,1,0,155,105,255,10
10/05/2010 21:48:54,9,S,0,245,4,1,0,147,113,255,10,119,10
10/05/2010 21:49:14,9,S,0,245,4,1,0,147,113,255,10,119,10
10/05/2010 21:51:14,11,SW,2,0,61,141,1,1,0,155,106,255,10
10/05/2010 21:51:24,11,SW,3,0,60,141,1,1,0,154,105,255,10
10/05/2010 21:51:34,9,S,3,0,60,141,1,1,0,152,105,255,10
10/05/2010 21:51:35,9,S,3,0,60,141,1,1,0,152,105,255,10
10/05/2010 21:51:45,9,S,4,0,60,141,1,1,0,152,105,255,10
10/05/2010 21:51:45,9,S,4,0,60,141,1,1,0,152,105,255,10
10/05/2010 21:51:55,9,S,4,0,60,141,1,1,0,152,105,255,10
10/05/2010 21:52:05,11,SW,60,141,1,1,0,155,105,255,10,113,10
10/05/2010 21:52:15,11,SW,60,141,1,1,0,155,105,255,10,113,10
10/05/2010 21:52:55,8,ESS,3,0,58,141,0,155,105,255,10,113,10
10/05/2010 21:52:55,8,ESS,3,0,58,141,0,155,105,255,10,113,10
10/05/2010 21:53:05,9,S,4,0,58,141,1,1,0,158,107,255,10
10/05/2010 21:53:15,11,SW,3,0,58,141,1,1,0,157,106,255,10
10/05/2010 21:53:25,11,SW,1,0,58,141,1,1,0,157,106,255,10
10/05/2010 21:53:35,11,SW,2,0,58,141,1,1,0,158,106,255,10
10/05/2010 21:53:45,10,SSW,1,0,58,141,1,1,0,155,106,255,10
10/05/2010 21:53:45,10,SSW,1,0,58,141,1,1,0,155,106,255,10
10/05/2010 21:53:55,10,SSW,2,0,58,141,1,1,0,155,106,255,10
10/05/2010 21:53:55,10,SSW,2,0,58,141,1,1,0,155,106,255,10
10/05/2010 21:54:05,9,S,1,0,58,141,1,1,0,155,106,255,10
10/05/2010 21:54:05,9,S,1,0,58,141,1,1,0,155,106,255,10
10/05/2010 21:54:35,9,S,1,0,58,141,1,1,0,155,106,255,10
10/05/2010 21:54:45,11,SW,3,0,10,111,1,1,0,155,106,255,10
10/05/2010 21:54:45,11,SW,3,0,10,111,1,1,0,155,106,255,10
10/05/2010 21:54:55,9,S,5,0,57,141,1,1,0,159,107,255,10
10/05/2010 21:55:05,9,S,5,0,57,141,1,1,0,159,107,255,10
10/05/2010 21:55:15,10,SSW,3,0,57,141,220,1,0,129,113,255,10
10/05/2010 21:55:35,8,ESS,1,0,57,141,220,1,0,134,115,255,10
10/05/2010 21:55:55,10,SSW,2,0,57,141,220,1,0,129,114,255,10
10/05/2010 21:56:05,7,ES,2,0,58,141,220,1,0,128,114,255,10
10/05/2010 21:56:15,7,ES,2,0,58,141,220,1,0,128,114,255,10

Hope this help