andrewrapp / xbee-arduino

Arduino library for communicating with XBee radios in API mode
GNU General Public License v2.0
334 stars 162 forks source link

Value being received is not what is expected #58

Closed naren16 closed 6 years ago

naren16 commented 6 years ago

Received packet: 7E 00 0E 90 00 7D 33 A2 00 41 5D 7A 35 00 00 01 ED FF 80

Where RF data in hex is : ED FF. Which is way off..!

Coordinator Code:

include

include

XBee xbee = XBee(); int LEDPin=13; float temp; int temp2; XBeeAddress64 address = XBeeAddress64(0x0013a200, 0x415d7a44); String str1,str2;

void setup() { xbee.begin(Serial); Serial.begin(9600); pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); str1="Temperature :";

}

void loop() { if (Serial.available() >= 21){

if(Serial.read() == 0x7E){     //start byte 
  for(int i=1; i<19; i++){
    byte discardByte=Serial.read();
  }
  int analogMSB = Serial.read();
  int analogLSB = Serial.read();   
  int analogReading = analogLSB + (analogMSB * 256);

  temp = analogReading / 1023.0 * 120;
  temp = temp - 50;            // holds temperature value in type float
  str2=str1+temp;
  temp2=(int)temp;
  //Serial.println(str2);               
  uint8_t* utemp = reinterpret_cast<uint8_t*>(&temp2);     // stores int type data in uint8_t* format
  ZBTxStatusResponse txStatus = ZBTxStatusResponse();
  ZBTxRequest zbTx = ZBTxRequest(address, utemp, sizeof(utemp));
  xbee.send(zbTx);
  if(temp>24){
    digitalWrite(LEDPin, HIGH);
    }
  else{
    digitalWrite(LEDPin, LOW);
    }

}
} }

davidsainty commented 6 years ago

It's pretty hard to tell what this code is trying to do. But I take it you are receiving valid data, so sending the data is working. sizeof(utemp) is definitely inaccurate, sizeof(temp2) makes more sense.

0xffed might equal -19. -19 + 50 == 21, seems like a plausible temperature, assuming everything else is correct?