Go Down

Topic: Problem in communicating two Xbee modules with Arduino (Read 186 times) previous topic - next topic

rguaschz

I need your help, I want to connect 2 integrated Xbee S2C modules to an Arduino Uno R3, the structure of the devices is as follows:

0013A2004172FE0E - Shild Xbee - Arduino UnoR3 (Transmit data)
0013A2004172FE0C - Shild Xbee - Arduino UnoR3 (Receive data)

Currently I have these codes (sketch) in Arduino and I have the problem that the devices have not been communicated, I do not know if it is a problem of the device that transmits, or the problem of the device from which it receives. The tests carried out indicate that the data seems to be transmitted, on the other side, in the device that receives the function xbee.getResponse (). IsAvailable ()) returns 0. Apparently the function xbee.readPacket (); is not working
In XCTU the devices were configured with the following parameters:

0013A2004172FE0E (Transmit)
BREAD ID: 1308
CE: enabled
OP: 1308
OI: DB57
CH: 18
AP: API enabled 1

0013A2004172FE0C (Receive)
BREAD ID: 1308
CE: disabled
OP: 1308
OI: DB57
CH: 18
AP: API enabled 1


Transmit from 0013A2004172FE0E

Code: [Select]
#include <SoftwareSerial.h>
#include <XBee.h>

XBee xbee = XBee();
XBeeAddress64 Broadcast = XBeeAddress64(0x0013a200, 0x417fe0c);
char Hello[] = "Hello";
char Buffer[128];  

void setup() {
 Serial.begin(9600);
 xbee.setSerial(Serial);
 Serial.println("Initialization all done!");
}

void loop() {
 Serial.println("1");
 ZBTxRequest zbtx = ZBTxRequest(Broadcast, (uint8_t *)Hello, strlen(Hello));
 Serial.println("2");
 xbee.send(zbtx);
 Serial.println("3");
 delay(30000);
 strcpy(Buffer,"I saw what you did last night");
 zbtx = ZBTxRequest(Broadcast, (uint8_t *)Buffer, strlen(Buffer));
 xbee.send(zbtx);
 delay(30000);
 Serial.println("End");
}



Receive data from 0013A2004172FE0E

Code: [Select]
#include <XBee.h>

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();

void setup() {
 Serial.begin(9600);
 xbee.setSerial(Serial);
 Serial.println("starting up yo!");
}

void loop() {
   xbee.readPacket();
   if (xbee.getResponse().isAvailable()) {
     Serial.print("Frame Type is ");
     Serial.println(xbee.getResponse().getApiId(), HEX);
   
     if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
       xbee.getResponse().getZBRxResponse(rx);
       Serial.print("Got an rx packet from: ");
       XBeeAddress64 senderLongAddress = rx.getRemoteAddress64();
       print32Bits(senderLongAddress.getMsb());
       Serial.print(" ");
       print32Bits(senderLongAddress.getLsb());
     
       uint16_t senderShortAddress = rx.getRemoteAddress16();
       Serial.print(" (");
       print16Bits(senderShortAddress);
       Serial.println(")");
     
       if (rx.getOption() & ZB_PACKET_ACKNOWLEDGED)
         Serial.println("packet acknowledged");
       if (rx.getOption() & ZB_BROADCAST_PACKET)
         Serial.println("broadcast Packet");
         Serial.print("checksum is ");
         Serial.println(rx.getChecksum(), HEX);
         Serial.print("packet length is ");
         Serial.print(rx.getPacketLength(), DEC);
         Serial.print(", data payload length is ");
         Serial.println(rx.getDataLength(),DEC);
         Serial.println("Received Data: ");
       for (int i = 0; i < rx.getDataLength(); i++) {
         print8Bits(rx.getData()[i]);
         Serial.print(' ');
       }
     
       Serial.println();
       for (int i= 0; i < rx.getDataLength(); i++){
         Serial.write(' ');
         if (iscntrl(rx.getData()[i]))
           Serial.write(' ');
         else
           Serial.write(rx.getData()[i]);
         Serial.write(' ');
       }
       Serial.println();
       // So, for example, you could do something like this:
       handleXbeeRxMessage(rx.getData(), rx.getDataLength());
       Serial.println();
     }
     else if (xbee.getResponse().getApiId() == ZB_IO_SAMPLE_RESPONSE) {
       xbee.getResponse().getZBRxIoSampleResponse(ioSample);
       Serial.print("Received I/O Sample from: ");
       XBeeAddress64 senderLongAddress = ioSample.getRemoteAddress64();
       print32Bits(senderLongAddress.getMsb());
       Serial.print(" ");
       print32Bits(senderLongAddress.getLsb());
       uint16_t senderShortAddress = ioSample.getRemoteAddress16();
       Serial.print(" (");
       print16Bits(senderShortAddress);
       Serial.println(")");
       if (ioSample.containsAnalog()) {
         Serial.println("Sample contains analog data");
           uint8_t bitmask = ioSample.getAnalogMask();
           for (uint8_t x = 0; x < 8; x++){
           if ((bitmask & (1 << x)) != 0){
             Serial.print("position ");
             Serial.print(x, DEC);
             Serial.print(" value: ");
             Serial.print(ioSample.getAnalog(x));
             Serial.println();
           }
         }
       }
       if (ioSample.containsDigital()) {
         Serial.println("Sample contains digtal data");
         uint16_t bitmask = ioSample.getDigitalMaskMsb();
         bitmask <<= 8;  //shift the Msb into the proper position
         bitmask |= ioSample.getDigitalMaskLsb();
         for (uint8_t x = 0; x < 16; x++){
           if ((bitmask & (1 << x)) != 0){
             Serial.print("position ");
             Serial.print(x, DEC);
             Serial.print(" value: ");
             Serial.print(ioSample.isDigitalOn(x), DEC);
             Serial.println();
           }
         }
       }
       Serial.println();
     }

     else {
       Serial.print("Got frame id: ");
       Serial.println(xbee.getResponse().getApiId(), HEX);
     }
   }
   else if (xbee.getResponse().isError()) {
     Serial.print("************************************* error code:");
     Serial.println(xbee.getResponse().getErrorCode(),DEC);
   }
   else {
     // I hate else statements that don't have some kind
     // ending.  This is where you handle other things
   }
}

void handleXbeeRxMessage(uint8_t *data, uint8_t length){
 for (int i = 0; i < length; i++){
//    Serial.print(data[i]);
 }
//  Serial.println();
}

void showFrameData(){
 Serial.println("Incoming frame data:");
 for (int i = 0; i < xbee.getResponse().getFrameDataLength(); i++) {
   print8Bits(xbee.getResponse().getFrameData()[i]);
   Serial.print(' ');
 }
 Serial.println();
 for (int i= 0; i < xbee.getResponse().getFrameDataLength(); i++){
   Serial.write(' ');
   if (iscntrl(xbee.getResponse().getFrameData()[i]))
     Serial.write(' ');
   else
     Serial.write(xbee.getResponse().getFrameData()[i]);
   Serial.write(' ');
 }
 Serial.println();
}

void print32Bits(uint32_t dw){
 print16Bits(dw >> 16);
 print16Bits(dw & 0xFFFF);
}

void print16Bits(uint16_t w){
 print8Bits(w >> 8);
 print8Bits(w & 0x00FF);
}

void print8Bits(byte c){
 uint8_t nibble = (c >> 4);
 if (nibble <= 9)
   Serial.write(nibble + 0x30);
 else
   Serial.write(nibble + 0x37);
     
 nibble = (uint8_t) (c & 0x0F);
 if (nibble <= 9)
   Serial.write(nibble + 0x30);
 else
   Serial.write(nibble + 0x37);
}

Go Up