Transmission Status in Xbee in API mode 1

Hi guys

Thanks to a forum member, I was able to transmit data from the Arduino Uno via an Xbee to a PC. This code works perfectly, so part of the next step and part of one of the features of the Xbee in API mode, is to have a transmit status. I mean that the sensor knows that the frame it sent was received.

That is why I have added the option to this code that after he sends the data by transmit request, the sensor manages to print on the Serial monitor “SUCCESS” indicating that the transmit status confirms that the frame was delivered successfully.

For this code I base myself on the adrewrapp library, and copy and paste the lines of code that interest me. So here is the the main code.

#include <SafeString.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SoftwareSerial.h>

SoftwareSerial xbee(2, 3); //(Rx, TX)

byte packet1[] = {0x7E, 0x00, 0x21}; //Byte delimitador y Longitud de trama
byte packet2[] = {0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x41, 0x4E, 0xF1, 0x24, 0xFF, 0xFE, 0x00, 0x00,}; //Id de trama, tipo de trama, direccion de 64bits, opciones y numero de brincos.

// 00:11:46 05/01/2021 == 19 chars
byte fullPacket[3 + 14 + 19 + 1];// (Delimitador + longitud )+ (tipo + direccion) + fecha + checksum = 37
// 
char buffer [25]; // 19 la fecha  +1  '\0', 25 bytes de tamaño

#define DEBUG SafeString::Output

void setup() {
  Serial.begin(9600);
  xbee.begin(9600);
  delay(2000);
  Serial.println();
  Serial.println("DS1307RTC convertir valores a HEX");
  Serial.println("-------------------");

  SafeString::setOutput(Serial);
  }

void loop() {
  tmElements_t tm;
  if (RTC.read(tm)) {
    sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%04d", tm.Hour , tm.Minute, tm.Second, tm.Day, tm.Month, tmYearToCalendar(tm.Year));
    //sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%04d", 5 , 6, 7, 8, 9, 2021);
    Serial.println(buffer); // con este print compruebo que la fecha y hora
    // build fullpacket
    size_t idx = 0;
    memmove(fullPacket + idx, packet1, sizeof(packet1));
    idx += sizeof(packet1);
    memmove(fullPacket + idx, packet2, sizeof(packet2));
    idx += sizeof(packet2);
    if ((idx + 19 + 1) > sizeof(fullPacket)) {
      // Si el tamaño es incorrecto
      Serial.println(F("fullPacket size wrong"));
    }
    memmove(fullPacket + idx, buffer, 19);
    idx += 19;
    // dejar un byte para el cheksum
    if ((idx + 1) != sizeof(fullPacket)) {
      // fullPacket size wrong
      Serial.println(F("fullPacket size wrong"));
    }

    int chksum = 0;
    for (size_t i = 3; i < idx; i++) { // comienza despues del tamaño dela trama
      chksum += fullPacket[i]; // comienza a sumar 
    }
    chksum = (0xff & chksum);
    chksum = (0xff) & (0xff - chksum);
    fullPacket[idx] = chksum;
    idx++; // El tamao total de la trama
    if (idx != sizeof(fullPacket)) {
      Serial.println(F("fullPacket size error"));
    }

    // Imprime el paquete
    size_t printSize = idx * 3; // ..<space>
    cSF(sfPacket, printSize);
    for (size_t i = 0; i < idx; i++) {
      //sfPacket += "0x";
      if (fullPacket[i] < 16) {
        sfPacket += '0'; 
      }
      sfPacket.print(fullPacket[i], HEX); 
      sfPacket.print(' ');
    }
    Serial.println(sfPacket);

    xbee.write (fullPacket , idx);   
    delay(10000);
  }
}
void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

Here its is the Code in Andrewrapp in Github:

These are the lines, that I get:

 // 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) {
        // success.  time to celebrate
        flashLed(statusLed, 5, 50);
      } else {
        // the remote XBee did not receive our packet. is it powered on?
        flashLed(errorLed, 3, 500);
      }

And this the final code:

#include <SafeString.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SoftwareSerial.h>
#include <XBee.h>

SoftwareSerial xbee(2, 3); //(Rx, TX)
XBee xbeer = XBee();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();

byte packet1[] = {0x7E, 0x00, 0x21}; //Byte delimitador y Longitud de trama
byte packet2[] = {0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x41, 0x4E, 0xF1, 0x24, 0xFF, 0xFE, 0x00, 0x00,}; //Id de trama, tipo de trama, direccion de 64bits, opciones y numero de brincos.

// 00:11:46 05/01/2021 == 19 chars
byte fullPacket[3 + 14 + 19 + 1];// (Delimitador + longitud )+ (tipo + direccion) + fecha + checksum = 37
// 
char buffer [25]; // 19 la fecha  +1  '\0', 25 bytes de tamaño

#define DEBUG SafeString::Output

void setup() {
  Serial.begin(9600);
  xbee.begin(9600);
  xbeer.begin(Serial);
  delay(2000);
  Serial.println();
  Serial.println("DS1307RTC convertir valores a HEX");
  Serial.println("-------------------");

  SafeString::setOutput(Serial);
  }

void loop() {
  tmElements_t tm;
  if (RTC.read(tm)) {
    sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%04d", tm.Hour , tm.Minute, tm.Second, tm.Day, tm.Month, tmYearToCalendar(tm.Year));
    //sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%04d", 5 , 6, 7, 8, 9, 2021);
    Serial.println(buffer); // con este print compruebo que la fecha y hora
    // build fullpacket
    size_t idx = 0;
    memmove(fullPacket + idx, packet1, sizeof(packet1));
    idx += sizeof(packet1);
    memmove(fullPacket + idx, packet2, sizeof(packet2));
    idx += sizeof(packet2);
    if ((idx + 19 + 1) > sizeof(fullPacket)) {
      // Si el tamaño es incorrecto
      Serial.println(F("fullPacket size wrong"));
    }
    memmove(fullPacket + idx, buffer, 19);
    idx += 19;
    // dejar un byte para el cheksum
    if ((idx + 1) != sizeof(fullPacket)) {
      // fullPacket size wrong
      Serial.println(F("fullPacket size wrong"));
    }

    int chksum = 0;
    for (size_t i = 3; i < idx; i++) { // comienza despues del tamaño dela trama
      chksum += fullPacket[i]; // comienza a sumar 
    }
    chksum = (0xff & chksum);
    chksum = (0xff) & (0xff - chksum);
    fullPacket[idx] = chksum;
    idx++; // El tamao total de la trama
    if (idx != sizeof(fullPacket)) {
      Serial.println(F("fullPacket size error"));
    }

    // Imprime el paquete
    size_t printSize = idx * 3; // ..<space>
    cSF(sfPacket, printSize);
    for (size_t i = 0; i < idx; i++) {
      //sfPacket += "0x";
      if (fullPacket[i] < 16) {
        sfPacket += '0'; 
      }
      sfPacket.print(fullPacket[i], HEX); 
      sfPacket.print(' ');
    }
    Serial.println(sfPacket);

    xbee.write (fullPacket , idx);   
    delay(10000);

    if (xbeer.readPacket(500)) {
    // got a response!

    // should be a znet tx status              
    if (xbeer.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
      xbeer.getResponse().getZBTxStatusResponse(txStatus);

      // get the delivery status, the fifth byte
      if (txStatus.getDeliveryStatus() == SUCCESS) {
        // success.  time to celebrate
        Serial.println("SUCCESS");
      } else {
        // the remote XBee did not receive our packet. is it powered on?
        Serial.println("ERROR");
      }
    }
  }
}
}
void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

As you can see, change the lines so that this time it does not turn on LEDs but prints a SUCCESS message. And create another object (xbeer) that works at the same baud rate, because if you used the same object (xbee) you would have problems, since it belongs to String …

The problem is that I do not have the SUCCES printout on the Serial monitor. And I don’t know what to do anymore, I thought I had made all the necessary changes.