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

Discussion in 'Wireless & RF Design' started by zazas321, Feb 21, 2019.

  1. zazas321

    Thread Starter Member

    Nov 29, 2015
    412
    6
    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

    Code (C):
    1.  
    2.  
    3. void setup(){
    4.   pinMode(led, OUTPUT);
    5.   Serial.begin(9600);
    6. }
    7.  
    8. void loop(){
    9.   digitalWrite(led, HIGH);
    10. setRemoteState1(0x5);
    11.   //setRemoteState2(0x4);
    12.   delay(1000);
    13.   digitalWrite(led, LOW);
    14.   setRemoteState1(0x4);
    15.   //setRemoteState2(0x5);
    16.   delay(1000);
    17. }
    18.  
    19. // first xbee
    20. void setRemoteState1 (char value){
    21.   Serial.write(0x7E); //start byte
    22.   Serial.write((byte)0x0); //high part of length (always zero)
    23.   Serial.write(0x10);  //low part of length
    24.   Serial.write(0x17); //0x17 is remote AT command
    25.     Serial.write((byte)0x0);  //frame ID set to zero for no reply
    26.  
    27.  
    28. Serial.write(0x00);
    29.   Serial.write(0x13);
    30.   Serial.write(0xA2);
    31.   Serial.write(0x00);
    32.   Serial.write(0x40);
    33.   Serial.write(0xB8);
    34.   Serial.write(0xF4);
    35.   Serial.write(0x1F);
    36.   //16 bit of recipient of 0xFFFE
    37.   Serial.write(0xFF);
    38.   Serial.write(0xFE);
    39.   Serial.write(0x02);
    40.  
    41.   Serial.write('D');
    42.   Serial.write('4');
    43.  
    44.  
    45.   //command data in as many bytes
    46.   Serial.write(value);
    47.   //checksum
    48.   long sum = 0x17 + 0x13 + 0xA2 + 0x40 + 0xB8 +0xF4+0x1F+0xFF+0xFE + 0x02 + 'D' + '4' + value;
    49.   Serial.write(0xFF - (sum & 0xFF));
    50. }
    51.  












    Temperature reading:

    Code (C):
    1.  
    2. //SET D2 TO ADC
    3. float temp;
    4. void setup() {
    5.   Serial.begin(9600);
    6.   // put your setup code here, to run once:
    7.  
    8. }
    9.  
    10. void loop() {
    11. if(Serial.available()>=21){
    12. if (Serial.read()==0x7E){
    13.   for(int i=1;i<19;i++){
    14.     byte discardByte = Serial.read();
    15.   }
    16.    int analogMSB=Serial.read();
    17.    int analogLSB= Serial.read();
    18.    int analogReading =analogLSB+(analogMSB*256);
    19.    temp=(analogReading/1023.0)*120;
    20.    Serial.print(temp);
    21.    Serial.println("degrees C");
    22.   }
    23. }
    24. }
    25.  
    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:
    Code (C):
    1.  
    2. float temp;
    3. int led = 13;
    4.  
    5. void setup(){
    6.   pinMode(led, OUTPUT);
    7.   Serial.begin(9600);
    8. }
    9.  
    10. void loop(){
    11.  
    12.  
    13. if(Serial.available()>=21){
    14.  if (Serial.read()==0x7E){
    15.   for(int i=1;i<19;i++){
    16.     byte discardByte = Serial.read();
    17.   }
    18.    int analogMSB=Serial.read();
    19.   // Serial.print("MSB:");
    20.       //Serial.println(analogMSB);
    21.    int analogLSB= Serial.read();
    22.    //Serial.print("LSB:");
    23.    //Serial.println(analogLSB);
    24.    
    25.    int analogReading =analogLSB+(analogMSB*256);
    26.    if (analogReading >0){
    27.     temp=(analogReading/1023.0)*120;
    28.     Serial.print(temp);
    29.    Serial.println("degrees C");
    30.  
    31.    
    32.   }
    33.   setRemoteState1(0x5);
    34.   delay(1000);
    35.   setRemoteState1(0x4);
    36.   delay(1000);
    37. }
    38. }
    39.    
    40.  
    41. // first xbee
    42. void setRemoteState1 (char value){
    43.   Serial.write(0x7E); //start byte
    44.   Serial.write((byte)0x0); //high part of length (always zero)
    45.   Serial.write(0x10);  //low part of length
    46.   Serial.write(0x17); //0x17 is remote AT command
    47.     Serial.write((byte)0x0);  //frame ID set to zero for no reply
    48.  
    49.  
    50.  Serial.write(0x00);
    51.   Serial.write(0x13);
    52.   Serial.write(0xA2);
    53.   Serial.write(0x00);
    54.   Serial.write(0x41);
    55.   Serial.write(0x82);
    56.   Serial.write(0xD1);
    57.   Serial.write(0xB9);
    58.   //16 bit of recipient of 0xFFFE
    59.   Serial.write(0xFF);
    60.   Serial.write(0xFE);
    61.   Serial.write(0x02);
    62.   Serial.write('D');
    63.   Serial.write('4');
    64.  
    65.  
    66.   //command data in as many bytes
    67.   Serial.write(value);
    68.   //checksum
    69.   long sum = 0x17 + 0x13 + 0xA2 + 0x41 + 0x82 +0xD1+0xB9+0xFF+0xFE + 0x02 + 'D' + '4' + value;
    70.   Serial.write(0xFF - (sum & 0xFF));
    71. }
    72.  



    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?

    [​IMG]
     
    Last edited: Feb 21, 2019
  2. zazas321

    Thread Starter Member

    Nov 29, 2015
    412
    6
    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
     
Loading...