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.