HI im trying to communicate two arduinos with the use of two Xbee series 2, one as a Coordinator and the other one as a router. I manage to get the TX code running fine, i been able to send data to my other xbee when it is connected to my computer. However when i try to do it sending the data to the xbee connected to the arduino. its not working. here i send you the code if you could help me. im trying to do a wireless arcade controller to my retropie arcade. the due project is TOMORROW thank you
RX code
#include <XBee.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
void setup() {
Serial.begin(9600);
xbee.begin(Serial);
}
void loop() {
// RECEIVE DATA BLOCK //
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
Serial.println("Got packet!");
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
#ifdef DEBUG
Serial.println("Got packet!");
#endif
xbee.getResponse().getZBRxResponse(rx);
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
String response = "";
for (int i = 0; i < rx.getDataLength(); i++) {
char character = rx.getData(i);
response = response + String(character);
}
#ifdef DEBUG
Serial.print("Response: ");
Serial.println(response);
#endif
if(response == String("open")){
}else if(response == String("close")){
}
} else {
Serial.print("Hola");
}
}
}
// End receive block //
}
TX code:
#include <XBee.h>
// Xbee communication
XBee xbee = XBee();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
void setup()
{
Serial.begin(9600);
xbee.setSerial(Serial);
}
void xbeeSend(char data[],uint8_t length)
{
// SH + SL Address of receiving XBee
XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x40b7950f);
ZBTxRequest zbTx = ZBTxRequest(addr64,(uint8_t*)data,length);
xbee.send(zbTx);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("aaaaaaaaaaa");
String output("Hola Mundo");
int len = output.length()+1;
char charPayload[len];
output.toCharArray(charPayload,len);
xbeeSend(charPayload,sizeof(charPayload));
// after sending a tx request, we expect a status response
// wait up to half second for the status response
if (xbee.readPacket(500)) {
// got a response!
// should be a znet tx status
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
// get the delivery status, the fifth byte
if (txStatus.getDeliveryStatus() == SUCCESS) {
Serial.println("Llego Lai");
} else {
// the remote XBee did not receive our packet. is it powered on?
Serial.println("No LLego");
}
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
}
delay(1000);
}