using Time library for Arduino Uno

Dear community,

I am kind of writing a whole new topic as in my previous topic, I think I did not make things quite clear for everybody to understand!

I am trying to create absolute real time time and date for my sensor data using the srduino’s Time library. Therefore, I try to modify the ‘Time Serial’ example sketch given as part of the Time library download files. The original sketch is given below:

/* 
 * TimeSerial.pde
 * example code illustrating Time library set through serial port messages.
 *
 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
 T1357041600  
 *
 * A Processing example sketch to automatically send the messages is included in the download
 * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
 */ 
 
#include <TimeLib.h>

#define TIME_HEADER  "T"   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  pinMode(13, OUTPUT);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if (Serial.available()) {
    processSyncMessage();
  }
  if (timeStatus()!= timeNotSet) {
    digitalClockDisplay();  
  }
  if (timeStatus() == timeSet) {
    digitalWrite(13, HIGH); // LED on if synced
  } else {
    digitalWrite(13, LOW);  // LED off if needs refresh
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

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


void processSyncMessage() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
       setTime(pctime); // Sync Arduino clock to the time received on the serial port
     }
  }
}

time_t requestSync()
{
  Serial.write(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}

As we can see that this code, it uses the setSyncProvider( requestSync) to sync the Arduino and system time. What it needs is the user to send a message to the arduino like: ‘T’ combined with ten-digit Unix timestamp. Also in this case it waits for this user input untill it starts showing the system time and date. And I guess it all happens in time function - requestSync().

I tried to modify this sketch. What I want to do is somehow get the Unix time stamp corresponding to the real PC time (I guess by using now() function). Then combine it with character ‘T’ and write it into the arduino. So that whenever the sketch is started, then it syncs automatically in real time instead of waiting for the user input. The modified sketch is found below:

/* 
 * TimeSerial.pde
 * example code illustrating Time library set through serial port messages.
 *
 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
 T1357041600  
 *
 * A Processing example sketch to automatically send the messages is included in the download
 * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
 */ 
 
#include <Time.h>
#include <TimeLib.h>
#include <String.h>

#define TIME_HEADER  "T"   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  pinMode(13, OUTPUT);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if (Serial.available()) {
    processSyncMessage();
  }
  if (timeStatus()!= timeNotSet) {
    digitalClockDisplay();  
  }
  if (timeStatus() == timeSet) {
    digitalWrite(13, HIGH); // LED on if synced
  } else {
    digitalWrite(13, LOW);  // LED off if needs refresh
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

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


void processSyncMessage() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
       setTime(pctime); // Sync Arduino clock to the time received on the serial port
     }
  }
}

time_t requestSync()
{
  String str1, str2;
  time_t t = now();
  long t1 = t;
  str1 =  String('T');
  str2.concat(t1);
  str2.toCharArray(chr,1000);
  Serial.write(chr);  
  //return 0; // the time will be sent later in response to serial mesg
}

As you can see I have essentially only modified the requestSync() function. Somehow, this sketch shows ‘NOTHING’ on the output monitor!! :frowning: What am doing wrong here!
Please guide me…

Any help would deeply appreciated! :slight_smile:

Thank you so much!

Cheers!

Pramit

Puzzled. How can you get the current time from now() before it has been set? It's like trying to pull yourself up by your bootstraps.

haha!!

Thank you for such a quick reply!

I just read it in the description to the library, that now() returns number of seconds since Jan 1 1970 till the moment it is called. So I guessed it gives this 10 digit Unix time stamp..

Please guide me if I am wrong. I am trying this library for the first time!

Regards

Pramit

Well, ha ha ha. Where do you think it gets the time since Jan 1 1970? There is no magic clock on the Arduino unless you add an RTC.

I think what you need to do is write software for the PC side, that will send the T message automatically, instead of having the user type it.

sood: Dear community, I am kind of writing a whole new topic as in my previous topic, I think I did not make things quite clear for everybody to understand!

Why could you not have simply made everything clear for everybody in the previous topic? This is considered a double post, it is discouraged on the forum.

Oh ok!

I am really sorry for this! I would definitely take care of this aspect in the future!

And any hints how to do such a 'PC side code'... :confused:. Forgive me but I am not a professional programmer.. I would need some concrete suggestion on what you say

I guess you mean a code to write the PC date and time to the serial port from where the arduino reads it.. But from where to start this particular code...?

Regards

Pramit

Any special reason why you don't want to use an RTC?

Well I am just short on time.. :( Ordering and delivery would tak atleast 3 days.. But I guess I would also try that option! I guess it is much simpler..

In the meanwhile I wanted to do it using the software library.. So any suggestions on creating such a code where we could send the PC date and time to serial port for the arduino to hear it..

Thank you so much!!

Regards

Pramit

Well I really appreciate for sending such a relevant thread..

I guess I need to look at it more deeply!

I am a lil confused on what is going on here... like:

How does character string- '?T' is being actually used? What if I use something different? How is PC responding to the request...

It seems a bit incomplete to me... But the idea behind is clear

If it would not be too much to ask for an explanation... Could you understand these points which I am missing?

Thank you so much for your support!

Regards

Pramit