1

I have been working on a project which involves xbees. My set-up is simply one Xbee ( coordinator Api mode ) connected to an Arduino as a master unit. This master unit recieves data from multiple Xbees ( slaves ) that are only powered up by a battery and reading data from there ADC pin17. ADC values are transmitted to the master xbee to display it on the serial terminal. The slaves Xbee are configured as ( Router AT mode ). I've bee doing this between two xbees only : 1 master and 1 slave. I have a code that reads the mac address of the sending Xbee and then displays the sent ADC value which is transmitted. All of that was ok until I added another slave, I really need help since I can't associate each mac address with the right ADC value. Neither my code is capable of reading from both alternatively; at some point it stops reading from a one. If any advice on how to recognize data coming from multiple xbees on the same network, I will be grateful. Here's my code :

#include <XBee.h>
#include <SoftwareSerial.h>

SoftwareSerial myserial(5,6);

float distance;

uint8_t myaddress[10];

XBee xbee = XBee();

uint8_t shCmd[] = {'S','H'};
uint8_t slCmd[] = {'S','L'};
AtCommandRequest atRequestSH = AtCommandRequest(shCmd);
AtCommandRequest atRequestSL = AtCommandRequest(slCmd);
AtCommandResponse atResponse = AtCommandResponse();

void getMyAddress(){
  xbee.send(atRequestSL);

  if(xbee.readPacket(5000)){
      xbee.getResponse().getAtCommandResponse(atResponse);
      if (atResponse.isOk()){
        for(int i = 0; i < atResponse.getValueLength(); i++){
          myaddress[i] = atResponse.getValue()[i];
        }
      }
  }
  delay(100);
}

void setup(){
Serial.begin(1200);
myserial.begin(1200);
xbee.begin(myserial);
}

void loop() {
  getMyAddress();
  for(int i=0; i < 10; i++) {
      Serial.print(myaddress[i], HEX);
      Serial.print(" ");
  }

Serial.print("\n");

if (myserial.available() >= 21) { //
if (myserial.read() == 0x7E) { 
  for (int i = 1; i<19; i++) { // Skip ahead to the analog data

    byte discardByte = myserial.read();
}
  int analogMSB = myserial.read(); // Read the first analog byte data
  int analogLSB = myserial.read(); // Read the second byte
  int analogReading = analogLSB + (analogMSB * 256);
  distance = ((analogReading *1.0) / 1023.0)* 3.3;
  Serial.println(distance);

}
 } 
}  
tomlogic
  • 11,489
  • 3
  • 33
  • 59

1 Answers1

0

In the code where you're skipping data to get to the analog value, you'll find the MAC address of the device sending the data.

if (myserial.read() == 0x7E) { 
  for (int i = 1; i<19; i++) { // Skip ahead to the analog data

    byte discardByte = myserial.read();
}

You should be taking a closer look at that data before processing it -- ensure that it's an I/O sample by looking at the frame type, check the frame length, check the checksum at the end of the frame, etc. The library you're using appears to have functions for processing AT commands and responses, perhaps it also has functions for the I/O Sample frame type.

This Digi support page explains I/O sampling and documents the various fields in the IO Data Sample Received frame (type 0x92).

tomlogic
  • 11,489
  • 3
  • 33
  • 59
  • After trying during the week to follow your advice I managed to make it work. Thanks a lot :) – Aboorah Abeer Apr 14 '15 at 16:06
  • Then you might want to post an answer to your own question, showing the code that worked, so someone searching in the future can see how you solved the problem. – tomlogic Apr 14 '15 at 22:49
  • @AboorahAbeer I know it has been a while but if you would post the answer to your question, it would be very helpful to me and much appreciated. – Mike C. Oct 12 '17 at 15:14