How to put date and time in arduino code

Hi,

I have a LoRa P2P RFM96 set up. One is set as transmitter and the other as receiver. I modified a code that the LoRa receiver receives a data from the transmitter every 15 minutes by putting a delay function. And it worked.

However I wanted to add the date and time that the receiver gets it the data. How can I do this?

I already tried several getTime codes I found in the internet but none seems to work.

Thank you!

Probably not the best way to do the timing as it blocks program execution for 15 minutes at a time

What processor do the boards have on them and what did you try to get time ? I suggest that you write a sketch simply to get the time at first before incorporating it into your main sketch

Probably not the best way to do the timing as it blocks program execution for 15 minutes at a time

Hello @UKHeliBob actually I've tried also using millis() since I also found several codes online but they weren't working that's why I resorted to use delay function. May I kindly ask if you have any suggested codes that I could get idea from?

What processor do the boards have on them and what did you try to get time ? I suggest that you write a sketch simply to get the time at first before incorporating it into your main sketch

This one of the codes I tried so far...

#include <DateTime.h>

#include <DateTimeStrings.h>


#define TIME_MSG_LEN  11   // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

void setup() {
  Serial.begin(19200);
}

void  loop() {
  getPCtime();   // try to get time sync from pc
  if (DateTime.available()) { // update clocks if time has been synced
    unsigned long prevtime = DateTime.now();
    while ( prevtime == DateTime.now() ) // wait for the second to rollover
      ;
    DateTime.available(); //refresh the Date and time properties
    digitalClockDisplay( );   // update digital clock

    // send our time to an app listening on the serial port
    Serial.print( TIME_HEADER, BYTE); // this is the header for the current time
    Serial.println(DateTime.now());
  }
}

void getPCtime() {
  // if time available from serial port, sync the DateTime library
  while (Serial.available() >=  TIME_MSG_LEN ) { // time message
    if ( Serial.read() == TIME_HEADER ) {
      time_t pctime = 0;
      for (int i = 0; i < TIME_MSG_LEN - 1; i++) {
        char c = Serial.read();
        if ( c >= '0' && c <= '9')
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
      }
      DateTime.sync(pctime);   // Sync DateTime clock to the time received on the serial port
    }
  }
}

void digitalClockDisplay() {
  // digital clock display of current time
  Serial.print(DateTime.Hour, DEC);
  printDigits(DateTime.Minute);
  printDigits(DateTime.Second);
  Serial.print(" ");
  Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
  Serial.print(" ");
  Serial.print(DateTimeStrings.monthStr(DateTime.Month));
  Serial.print(" ");
  Serial.println(DateTime.Day, DEC);
}

void printDigits(byte digits) {
  // utility function for digital clock display: prints colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits, DEC);
}

But it wasn't compiling. Do you also have suggested code that I could try for this one too?

Thank you so much!

What's arduino board do you used?

Hi @b707 I'm using arduino uno

Arduino UNO does not have real time clock. For using this code there must be a program running on the PC that can get the time from the PC's clock and send the data to the Arduino. Do you have such a program already?

And note that the sketch can be work only when the arduino is connected to PC.

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Arduino UNO does not have real time clock. For using this code there must be a program running on the PC that can get the time from the PC's clock and send the data to the Arduino. Do you have such a program already?

As of now I do not have. Can't I use that code for this???

And note that the sketch can be work only when the arduino is connected to PC.

Yes, my Arduino Uno is connected to my laptop.

Thank you for this one @UKHeliBob so you're not really recommending to use delay function for this one tho the result is okay.

This code is only "receiver" for date-time sending from PC. For using it, you need the 'transceiver" code also.

Read this thread

Using delay() is fine until you change the program requirements, which you almost certainly will. In fact you already want to incorporate date/time into the program

Suppose that you use a 15 minute delay() to allow 15 minutes to elapse between some events and it all works OK, which is what you seem to have done

Now suppose that you want to implement a button to cause the event to occur right now regardless of the time since the previous event, or a button to pause the program or a button to abort the program or maybe all 3. Now you need to make a fundamental change to the program to implement non blocking timing

I would prefer to use non blocking timing from the very beginning of the project to future proof it somewhat against later changes. Using millis() for this is not difficult as you will see if you try it using the right techniques as illustrated in the topics that I posted links to

Can you give a completer description of the project.

I understand that the receiver has a LoRa module; is that connected to a PC or to another Arduino?

If it's a PC, you can write a PC application that adds the PC time to the data that it receives.

If it's another Arduino, the best option in my opinion is to add a RTC module to either the receiver or the sender; you set the time once and:

  1. If you did connect the RTC to the receiver, use it to read the date/time from it when data is received
  2. If you did the RTC to the sender, use it to read the date/time and add that to the data that you send.

If you don't need a very accurate timestamp, you can use a millis() based timing to keep track of e.g. the seconds that have lapsed; that is, you set a date/time once manually when the Arduino starts and save the millis() value as a reference and every time millis() indicates that a second has passed, you update the date/time by adding one second.

How you set the date/time depends on what you have available; it can be a terminal program like serial monitor, it can be a keypad and LCD display.

If you don't have anything available or don't want to add anything, you can only use a relative time in e.g. seconds since the start of the Arduino.

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