How to choose which Xbee to send data to/from in a network

Thread Starter

zazas321

Joined Nov 29, 2015
559
Hey. I am trying to establish a network using 3 Xbees. 1 Xbee is programmed as Cordinator API and the other two are Router AT firmware.

One of the router Xbee devices are toggling digital pin high/low every one second

The second router Xbee is taking a temperature reading and sending the data back to the coordinator

I have tested both Xbees seperately and it works:
Digital pin toggling:


//toggles local LED and remote XBee LED on and off

C:
void setup(){
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  digitalWrite(led, HIGH);
setRemoteState1(0x5);
  //setRemoteState2(0x4);
  delay(1000);
  digitalWrite(led, LOW);
  setRemoteState1(0x4);
  //setRemoteState2(0x5);
  delay(1000);
}

// first xbee
void setRemoteState1 (char value){
  Serial.write(0x7E); //start byte
  Serial.write((byte)0x0); //high part of length (always zero)
  Serial.write(0x10);  //low part of length
  Serial.write(0x17); //0x17 is remote AT command
    Serial.write((byte)0x0);  //frame ID set to zero for no reply


Serial.write(0x00);
  Serial.write(0x13);
  Serial.write(0xA2);
  Serial.write(0x00);
  Serial.write(0x40);
  Serial.write(0xB8);
  Serial.write(0xF4);
  Serial.write(0x1F);
  //16 bit of recipient of 0xFFFE
  Serial.write(0xFF);
  Serial.write(0xFE);
  Serial.write(0x02);

  Serial.write('D');
  Serial.write('4');


  //command data in as many bytes
  Serial.write(value);
  //checksum
  long sum = 0x17 + 0x13 + 0xA2 + 0x40 + 0xB8 +0xF4+0x1F+0xFF+0xFE + 0x02 + 'D' + '4' + value;
  Serial.write(0xFF - (sum & 0xFF));
}












Temperature reading:

C:
//SET D2 TO ADC
float temp;
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:

}

void loop() {
if(Serial.available()>=21){
if (Serial.read()==0x7E){
  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;
   Serial.print(temp);
   Serial.println("degrees C");
  }
}
}
upload_2019-2-21_17-15-0.png

However, I am struggling to put these two programs together, because I am not sure how to acknowledge which Xbee is doing what.

When I am reading analog sample bytes, it automatically reads from the other Xbee as well which has nothing to do with Analog data therefore i get some garbage data at the output.





combined code:
C:
float temp;
int led = 13;

void setup(){
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop(){


if(Serial.available()>=21){
 if (Serial.read()==0x7E){
  for(int i=1;i<19;i++){
    byte discardByte = Serial.read();
  }
   int analogMSB=Serial.read();
  // Serial.print("MSB:");
      //Serial.println(analogMSB);
   int analogLSB= Serial.read();
   //Serial.print("LSB:");
   //Serial.println(analogLSB);
   
   int analogReading =analogLSB+(analogMSB*256);
   if (analogReading >0){
    temp=(analogReading/1023.0)*120;
    Serial.print(temp);
   Serial.println("degrees C");

   
  }
  setRemoteState1(0x5);
  delay(1000);
  setRemoteState1(0x4);
  delay(1000);
}
}
   

// first xbee
void setRemoteState1 (char value){
  Serial.write(0x7E); //start byte
  Serial.write((byte)0x0); //high part of length (always zero)
  Serial.write(0x10);  //low part of length
  Serial.write(0x17); //0x17 is remote AT command
    Serial.write((byte)0x0);  //frame ID set to zero for no reply


 Serial.write(0x00);
  Serial.write(0x13);
  Serial.write(0xA2);
  Serial.write(0x00);
  Serial.write(0x41);
  Serial.write(0x82);
  Serial.write(0xD1);
  Serial.write(0xB9);
  //16 bit of recipient of 0xFFFE
  Serial.write(0xFF);
  Serial.write(0xFE);
  Serial.write(0x02);
  Serial.write('D');
  Serial.write('4');


  //command data in as many bytes
  Serial.write(value);
  //checksum
  long sum = 0x17 + 0x13 + 0xA2 + 0x41 + 0x82 +0xD1+0xB9+0xFF+0xFE + 0x02 + 'D' + '4' + value;
  Serial.write(0xFF - (sum & 0xFF));
}



Serial monitor output:
upload_2019-2-21_17-20-53.png

Could someone give advice how to read the analog data from the Xbee that I want instead of automatically reading from both Xbees because they share the PAN ID?

 
Last edited:

Thread Starter

zazas321

Joined Nov 29, 2015
559
Okay so I have done some further testing, it appears that I get the garbage data at my serial monitor output before I even connect the TX and RX pins for the Xbees communications so it is not possible that the garbage data is coming from the Xbees. From my understanding, the garbage data is coming from me writing(toggling digital pin) and reading(sensor value) at the same time. Though I am not sure how to overcome this issue? Any suggestions
 
Top