My idea when using these two instructions is to run the code and the Arduino is asking itself: has it received a frame? Once it has received a frame, it does not really matter that it carries the frame, simply by just receiving a signal from the PC the Arduino will execute the code that I present below. I take examples from Andrewrapp’s Xbee Arduino library, the following functions
Intructions:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
xbee.read();
if (xbee.getResponse().isAvailable()) {
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This code that I am going to present to you, generates a mode 1 API frame successfully when I run the program.
And this is my code:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include
#include
#include
#include
#include
#include
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(“Sensor manda la hora y la fecha a la PC”);
Serial.println(“-------------------”);
SafeString::setOutput(Serial);
}
void loop() {
xbee.read();
if (xbee.getResponse().isAvailable()) {
tmElements_t tm; //Necesario para poder obtener los valores hora:min:seg dia/mes/año
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; // ..
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);
}