Hello Arduiforeros, good afternoon, my intention with this code is to obtain the Time and Date data from the RTC DS1307 and send it from an Xbee on the Arduino, to an Xbee on the PC and read it through the XCTU. This is a test because later on I would like to send a voltage value along with the time and date. I have a problem when viewing the characters in HEX mode because when I import the array the characters are not displayed.
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3);
char buffer [20];
char packet2 [40];
char varHex ;
void setup() {
Serial.begin(9600);
while (!Serial) ; // Espero al serial
delay(200);
Serial.println("DS1307RTC convertir valores a HEX");
Serial.println("-------------------");
}
void loop() {
tmElements_t tm;
if (RTC.read(tm)) {
//Sprintf Crea una cadena tipo string
sprintf(buffer, "%02d:%02d:%02d %02d/%02d/%04d", tm.Hour, tm.Minute, tm.Second, tm.Day, tm.Month, tmYearToCalendar(tm.Year));
Serial.println(buffer); // con este print compruebo que la fecha y hora
//Pasar de ASCII a HEX
// Formato ASCII :00:11:46 1/5/2021
// Formato HEX : 30 30 3A 31 31 3A 34 36 20 7C 20 31 2F 35 2F 32 30 32 31 0D
for (int i=0; i<20; i++){
varHex = (buffer[i], HEX);
packet2[i] = varHex;
Serial.println(buffer[i], HEX); // con este print compruebo que he convertido los caracteres a Hex
} // Quisiera tener estos Hex como como el formato HEX para enviarlos por el serial del Xbe
Serial.println(packet2); //Pero cuando los imprimo aqui argumentando que inicialice un array, me imprime otra cosa.
//xbee.write (packet , sizeof(packet)); // Manda el mensaje al nodo PC
}
delay(10000);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
I have this result.