Ok,
I modified a bit my code, but it doesn't work yet. I need to send and receive a simple message, but I want to use API mode to check (ACK, like TCP) the receivement of the message, so sender can re-send the message in case the message will be loose.
I write my code below, so if somebody could check it and tell me a tip, it would be great.
SEND:
#include <XBee.h>
XBee xbee = XBee();
uint8_t payload[] = { "OK" };
int pin = 13;
//char sendMsg[] = "OK";
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x10101010); //@MAC coordinator
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
void setup() {
pinMode(pin, OUTPUT);
xbee.begin(57600);
}
void loop() {
digitalWrite(pin, HIGH);
// payload = (uint8_t)sendMsg;
// Serial.print("Send Message: [");
// Serial.print(payload);
// Serial.println("]");
xbee.send(zbTx);
Serial.println("Sending TX packets.");
if (xbee.readPacket(5000)) {
Serial.println("\tHaving Response.");
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
Serial.println("\t\tTX STATUS");
if (txStatus.getDeliveryStatus() == SUCCESS) {
Serial.println("\t\t\tSUCCESS");
}
}
}
Serial.flush();
delay(500);
digitalWrite(pin, LOW);
delay(5000);
}
RECEIVE:
#include <XBee.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
int pin = 13;
uint8_t *data;
void setup() {
pinMode(pin, OUTPUT);
xbee.begin(57600);
}
void loop() {
digitalWrite(pin, HIGH);
xbee.readPacket();
Serial.println("Checking RX packets.");
if (xbee.getResponse().isAvailable()) {
Serial.println("There's some RX.");
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
Serial.println("\tThere's RX packet.");
xbee.getResponse().getZBRxResponse(rx);
data = rx.getData();
}
}
Serial.flush();
delay(500);
digitalWrite(pin, LOW);
delay(5000);
}