//jleblanc 7.3.08 //arduino //WUP->XBee with parsing and API //Software Serial Setup #include #define rxPin 2 #define txPin 3 SoftwareSerial softserial = SoftwareSerial(rxPin, txPin); //Hold for input char Input[256]; char Watts[16]; char header[3]="#d"; int wattsptr=-1; char* wattssection; //Destination Address for base XBee int addr=0xFFFA; void setup() { //Initiate Hardware Serial Serial.begin(115200); //Initiate Software Serial pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); softserial.begin(9600); //Pause delay(200); //"Open" Device 1 (Watt's Up Pro) Serial.print("SC 0"); Serial.print(13, BYTE); //Pause delay(100); } void loop() { //Clear Internal Memory Serial.print("DSD 7"); Serial.print(13, BYTE); Serial.print("#R,W,0;"); //Set to log every 5 seconds Serial.print("DSD 13"); //check if this must be hex Serial.print(13, BYTE); Serial.print("#L,W,3,I,-,5;"); //wait 11 seconds: delay(11000); //flush anything in the serial buffer Serial.flush(); //Request Data Serial.print("DSD 7"); Serial.print(13, BYTE); Serial.print("#D,R,0;"); //pause delay(50); //Grab the Data Grab(); //Grab substring starting after header (#d) wattsptr=search(&header[0]); //If the string is not NULL then grab substring starting 3 ',' away if(wattsptr!=-1) { wattssection = move_moveforward(&Input[wattsptr], ',', 3); } //Grab substring from begining of this str to next ',' buildwatts(wattssection); //Print out the Watts softserial.print(Watts); //Grab the rest of the data but discard it (we flush serial buffer at top of this loop) Grab(); //Reset Watts and Input Watts[0]='\0'; Input[0]='\0'; } void Grab() { Serial.flush(); int i=0; //Ask for Data Serial.print("DRD"); Serial.print(13, BYTE); delay(50); //Read Data in while((Serial.available() > 0) && (i < 255)) { Input[i]=Serial.read(); i++; } Input[i]='\0'; } void buildwatts(char* str)//basically just copies over the watts char and then replaces the first ',' with '\0' { int strpos=0; int Wpos=0; while(str[strpos]!='\0') { if((str[strpos]<='9')&&(str[strpos]>='0')) { Watts[Wpos]=str[strpos]; Wpos++; } strpos++; if(str[strpos]==',') {break;} } Watts[Wpos]='\0'; } int search(char* key)//restricted to key being 2 characters long! { int pos=0; int returnval=-1; while(Input[pos+1]!='\0') { if((Input[pos]==key[0]) && (Input[pos+1]==key[1]) ) { returnval = pos+2; break;} pos++; } return returnval; } char* move_moveforward(char* str, char key, int numf) { int numt=0; int pos=0; char* ptr=str; while((str[pos]!='\0') && (numt!=numf)) { if(str[pos]==key) { numt++;} pos++; } if(numt==numf) { ptr = &str[pos];} return ptr; } int strlength(char* str) { int pos=0; while(str[pos]!='\0') { pos++;} return pos; }