Send Time&Date from RTC DS1307 with XBee and arduino in API mode to other Xbee in my computer

Hi community, I try to sent via Xbee in API mode Time&Date from my DS1307 RTC and the problem here is when I read the Time&Date from the function -- if (RTC.read(tm)) {...---
it give me a this Example: 00:11:46 | 1/5/2021. So i Want to convert that data in a array of caracteres. When i have the arrays, I going to convert to HEX, and next I going to concatenate whith my FRAME in HEX, that I going to sent via Xbee.

This is my code.

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

SoftwareSerial xbee(2, 3);

String formatoData; 

byte packet[] = {};

//Trama de Datos: (FALTA VOLTAJE) La longitud de la trama nunca a variar, ya que el formato esta establecido para nunca variar.
//Formato de la data: XX.XX 00:11:46 01/05/2021

//Trama de datos 
//7E 00 28 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00 58 58 2E 58 58 20 30 30 3A 31 31 3A 34 36 20 30 31 2F 30 35 2F 32 30 32 31 0D 34

// Encabezado de Trama: Delimitador, longitud,tipo de trama, direccion de destnatario. ESTO NO VARIA.
//7E 00 28 10 01 00 13 A2 00 41 4E F1 24 FF FE 00 00
byte packet1[] = {0x7E, 0x00, 0x28, 0x10, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x41, 0x4E, 0xF1, 0x24, 0xFF, 0xFE, 0x00, 0x00};

//Data: ESTO SI VARIA.
// 58 58 2E 58 58 20 30 30 3A 31 31 3A 34 36 20 30 31 2F 30 35 2F 32 30 32 31 0D
//Checksum: Esto si varia.
//34
byte packet2[] = {};
//Declaro e inicializo un array tipo char para almacenar el string XX.XX 00:11:46 01/05/2021

char packetString[] = {};

void setup() {
  Serial.begin(9600);
  xbee.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test and Send Test w/ Xbee API");
  Serial.println("-------------------");
}

void loop() {
  
  tmElements_t tm;
  
  if (RTC.read(tm)) {
    Serial.print(" ");
    char hora[] ={tm.Hour};
    print2digits(hora);
    
    Serial.write(':');
    char minuto[] ={tm.Minute};
    print2digits(minuto);
    
    Serial.write(':');
    char segundo[] ={tm.Second};
    print2digits(segundo);
    
    Serial.print(" ");
    char dia[] ={tm.Day};
    print2digits(dia);
    
    Serial.write('/');
    char mes[] ={tm.Month};
    print2digits(mes);
    
    Serial.write('/');
    char years [] = {tmYearToCalendar(tm.Year)};
    Serial.print(years);
    
    Serial.println();
   //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     
   
   //xbee.write (packet , sizeof(packet));  // Manda el mensaje al nodo PC 
  }
  delay(3000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

When I run this i have this in my serial monitor :

DS1307RTC Read Test and Send Test w/ Xbee API

2299:2298:2297 2296/2295/⸮e
2299:2298:2297 2296/2295/⸮

Instead of:

Try this construct instead:

int hora = tm.Hour ;
print2digits(hora) ;

To convert numerical time stamp components into character array, you can use sprintf. For example:

sprintf(buffer, "%02d/%02d %02d:%02d", tm.Day, tm.Month, tm.Hour, tm.Minute); 
Serial.print(buffer);

where buffer is a char array. Ensure it is at least large enough to hold the largest resulting string including the termination character.

1 Like

Thank you so much. Its Works.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.