2
votes

I have two Arduino Unos, two lcds and Xbee s2. I connected two xbee s2 module to each arduino uno. I searched this implementation hard and tried it. but, it didn't work. I appreciate your help.

Configuration: AP=2 Coordinator API and Router API

Sender: Router

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

#include <LiquidCrystal.h>
#define LED 13
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial xSerial(3,4);
XBee xbee=XBee();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

void setup(){
  //Serial.begin(9600);
  xSerial.begin(9600);
  xbee.setSerial(xSerial);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, Gang!");
  //Serial.println("xbee start");
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  delay(3000);
}

void loop(){
  ZBTxRequest zbTx;
  uint8_t payload[]={'H', 'E', 'Y', '\0'};
  XBeeAddress64 address=XBeeAddress64(0x13a200, 0x40b450f4);
  zbTx=ZBTxRequest(address, payload, sizeof(payload));
  xbee.send(zbTx);
   if (xbee.readPacket(500)) {              
    if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
      xbee.getResponse().getZBTxStatusResponse(txStatus);
      // get the delivery status, the fifth byte
      if (txStatus.getDeliveryStatus() == SUCCESS) {
        lcd.setCursor(0,1);
        lcd.print("Success");
      } else {
        // the remote XBee did not receive our packet. is it powered on?
      }
    }
  } else if (xbee.getResponse().isError()) {
    //nss.print("Error reading packet.  Error code: ");  
    //nss.println(xbee.getResponse().getErrorCode());
  } else {
    // local XBee did not provide a timely TX Status Response -- should not happen
  }

  delay(1000);
}

Receiver: Coordinator

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

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

SoftwareSerial xSerial(3,4);

XBee xbee=XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();

void setup(){
  xSerial.begin(9600);
  xbee.setSerial(xSerial);
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, tang!");
  delay(3000);  
}

void loop(){
  xbee.readPacket();
  if (xbee.getResponse().isAvailable()) {
      // got something
      lcd.setCursor(0, 1);
      lcd.print( "MSG");
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a zb rx packet
        // now fill our zb rx class
        xbee.getResponse().getZBRxResponse(rx);
        lcd.setCursor(0, 1);
        lcd.print(rx.getData(1));

        if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
            // the sender got an ACK
            lcd.setCursor(0,1);
            lcd.print( "ACK");

        } else {
            lcd.setCursor(0,1);
            lcd.print("ERROR");
        }
      } else {
        // not something we were expecting
        //flashLed(errorLed, 1, 25);   
       lcd.print("7 print"); 
      }
    } else if (xbee.getResponse().isError()) {
      lcd.print("error");
    }
    delay(3000);
}

Ah, I connect xbee s2 to xbee pro shield on arduino. There are no dline/uart switch on xbee shield. so, I use softwareserial library...But it didn't work.

1
Have you confirmed (maybe with X-CTU) that the XBee radios have formed a network? Are you getting any frames from the XBee? Is your XBee library configured to use ATAP=2 instead of ATAP=1? Do you have the correct baud rate? If you connect the receiving XBee to a computer running X-CTU, does it display the received frame? What have you confirmed as working and have you isolated the problem at all?tomlogic

1 Answers

0
votes

I have done the communication between two XBee with Arduino, you can take a look at my coding and try to change your coding a little bit.

Sender =

#include <SoftwareSerial.h>
#define Dout 2
#define Din 3
#define LED 9
SoftwareSerial XBeeSerial (Dout, Din);

void setup() {  
XBeeSerial.begin(9600);
pinMode(LED, OUTPUT);
}

void loop() {

delay(500);
XBeeSerial.write("+");
analogWrite(LED,13);
delay(3000);
XBeeSerial.write("-");
analogWrite(LED,0);
delay(500);
}

Receiver =

#include<SoftwareSerial.h>
#define LED 9
#define Dout 2
#define Din 3
SoftwareSerial XBeeSerial( Dout, Din);
char XBee_message;

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

void loop() {
while(XBeeSerial.available()) {
  XBee_message = XBeeSerial.read();
  Serial.print(XBee_message);

 switch (XBee_message){
 case '+':
 analogWrite(LED, 13);
 break;
 case '-':
 analogWrite(LED, 0);
 break;
 default:  
 analogWrite(LED, 0);
}
}
}