Hello guys, I have a problem with my program I want to give my array the necessary format to send a Data Frame in API mode through my Xbee. So basically I take the Time&Date from a RTC DS1307 and make an HEX array.
Like this:
{0x7E, 0x00, 0x35, 0x10, 0x01, 0x00}
but I have this:
30363A32333A34352030352F30332F32303231
I need this :
{0x30, 0x36, 0x3A, ...... }
My code :
#include <SafeString.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SoftwareSerial.h>
char buffer [25]; // 25 and a bit
char packet2 [40]; // using SafeString will ensure this does not overflow.
void setup() {
Serial.begin(9600);
for (int i = 10; i > 0; i--) {
Serial.print(' '); Serial.print(i);
delay(500);
}
Serial.println();
Serial.println("DS1307RTC convertir valores a HEX");
Serial.println("-------------------");
}
void loop() {
sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%04d", 6, 23, 45, 5, 3, 2021);
Serial.println(buffer); // con este print compruebo que la fecha y hora
cSFA(sfPacket,packet2); // or long hand, createSafeStringFromCharArray(sfPacket, packet2); // picks up packet2 size automatically
for (size_t i = 0; i < strlen(buffer); i++) { // only process the chars we have
if (buffer[i] < 16) {
sfPacket += '0'; // add leading 0
}
sfPacket.print(buffer[i], HEX); // you can print to SafeStrings
Serial.println(buffer[i], HEX); // con este print compruebo que he convertido los caracteres a Hex
}
Serial.println(packet2); // now updated with hex values
//xbee.write (buffer , sizeof(buffer)); // Manda el mensaje al nodo PC or sizeof(buffer)+1 if you want to send the terminating '\0'
//}
delay(10000);
}