Getting Yun's Date & Time

hello, I have some trouble about getting time with Yun, I`ve following this tutorial and I want add date on my project so I makes some improve with that code

here is my code

/*
  Time Check

 Gets the time from the Linux processor via Bridge
 then parses out hours, minutes and seconds for the Arduino
 using an Arduino Yún.

 created  27 May 2013
 modified 21 June 2013
 By Tom Igoe

 This example code is in the public domain.

 */


#include <Process.h>

Process date;                 // process used to get the date
int dates, month, years, hours, minutes, seconds;  // for the results
int lastSecond = -1;          // need an impossible value for comparison

void setup() {
  Bridge.begin();        // initialize Bridge
  Serial.begin(9600);    // initialize serial  

  while(!Serial);               // wait for Serial Monitor to open
  Serial.println("Time Check");  // Title of sketch

  // run an initial date process. Should return:
  // hh:mm:ss :
  if (!date.running())  {
    date.begin("date");
    date.addParameter("+%D-%T");
    date.run();
  }
}

void loop() {

  if(lastSecond != seconds) {  // if a second has passed
    // print the time:
    if (dates <= 9) Serial.print("0");    // adjust for 0-9
    Serial.print(dates);    
    Serial.print("/");
    if (month <= 9) Serial.print("0");    // adjust for 0-9
    Serial.print(month);    
    Serial.print("/");
    if (hours <= 9) Serial.print("0");    // adjust for 0-9
    Serial.print(years);    
    Serial.print(" ");
    if (hours <= 9) Serial.print("0");    // adjust for 0-9
    Serial.print(hours);    
    Serial.print(":");
    if (minutes <= 9) Serial.print("0");  // adjust for 0-9
    Serial.print(minutes);
    Serial.print(":");
    if (seconds <= 9) Serial.print("0");  // adjust for 0-9
    Serial.println(seconds);

    // restart the date process:
    if (!date.running())  {
      date.begin("date");
      date.addParameter("+%D-%T");
      date.run();
    }
  }

  //if there's a result from the date process, parse it:
  while (date.available()>0) {
    // get the result of the date process (should be hh:mm:ss):
    String timeString = date.readString();    

    // find the colons:
    int firstColon = timeString.indexOf("/");
    int secondColon= timeString.indexOf("/");
    int thirdColon= timeString.indexOf(" ");
    int fourthColon= timeString.indexOf(":");
    int fifthColon= timeString.lastIndexOf(":");

    // get the substrings for hour, minute second:
    String datesString = timeString.substring(0, firstColon);
    String monthString = timeString.substring(firstColon+1, secondColon);
    String hourString = timeString.substring(secondColon+1, fourthColon);
    String minString = timeString.substring(fourthColon+1, fifthColon);
    String secString = timeString.substring(fifthColon+1);

    // convert to ints,saving the previous second:
    dates = hourString.toInt();
    month = hourString.toInt();
    years = hourString.toInt();
    hours = hourString.toInt();
    minutes = minString.toInt();
    lastSecond = seconds;          // save to do a time comparison
    seconds = secString.toInt();
  }

}

but when I check, Serial Monitor the output:

16/16/16 16:28:08
16/16/16 16:28:09
16/16/16 16:28:10
16/16/16 16:28:11

my expectation should be:

16/05/15 10:28:11

Can someone please tell me exactly what I’m doing wrong?

dexvils:
::::SNIP::::
Can someone please tell me exactly what I’m doing wrong?

@dexvils,
you are not doing anything wrong, you just don’t have the proper reference.

http://unixhelp.ed.ac.uk/CGI/man-cgi?date
I can’t finish this this minute. I will return in about an hour to explain.


Sorry, I realized when I returned, you have a different problem.

I think you need to set the time, or you have some corrupt memory. It is likely the first.

This sketch will get the date from your machine.

#include <Process.h>

Process date;                 // process used to get the date

void setup() {
  Bridge.begin();        // initialize Bridge
  Serial.begin(9600);    // initialize serial  

  while(!Serial);               // wait for Serial Monitor to open

  Serial.println("Time Check");  // Title of sketch
}

void loop() {
  date.begin("/bin/date");
  date.run();
  
  //if there's a result from the date process, get it.
  while (date.available()>0) {
    // print the results we got.
    Serial.print(date.readString());
  }
  
  delay(5000); // wait 5000ms
}

TO FIX

Google: arduino yun set date

Several ways here:
[SOLVED]Setting Yun’s Date/Time

Alternative:

  • webbrowser: http://arduino.local/
  • [configure] button on top.
  • advanced configuration panel (luci)
  • System->Local time
  • Menu should now look like below.

Let us know how it is going.
Jesse

thanks sir, its work.. But when I tried the first code Ive used before, the problem has come again..I think something wrong with my code, can you explain me, how to show the time like this on yun:

16/05/2015 12:20:23

dexvils:
Can someone please tell me exactly what I’m doing wrong?

Here is the relevant code:

   String timeString = date.readString();    

    // find the colons:
    int firstColon = timeString.indexOf("/");
    int secondColon= timeString.indexOf("/");
    int thirdColon= timeString.indexOf(" ");
    int fourthColon= timeString.indexOf(":");
    int fifthColon= timeString.lastIndexOf(":");

And here is the data that it’s calculating:

  • firstColon=2 - Correct
  • secondColon=2 - Wrong, because it’s searching from the beginning of the string again
  • thirdColon=-1 - Wrong, because there’s no space in the string (you used a ‘-’ in your format string.)
  • fourthColon=11 - Correct
  • fifthColon=14 - Correct, because you’re using last index

So you won’t extract out the fields you want. Then, in the next block of code:

   // convert to ints,saving the previous second:
    dates = hourString.toInt();
    month = hourString.toInt();
    years = hourString.toInt();
    hours = hourString.toInt();
    minutes = minString.toInt();
    lastSecond = seconds;          // save to do a time comparison
    seconds = secString.toInt();

Your hourString is converted multiple times, so you keep getting the “16” value for dates, month, years, and hours.

Finally, you have this:

   if (hours <= 9) Serial.print("0");    // adjust for 0-9
    Serial.print(years);    
    Serial.print(" ");

You’re using the hours value to determine if the years value needs a leading zero. Because of the above errors, this works, but won’t once the previous logic is fixed.

But I have to wonder why you are going through all of this work to parse out the individual values, just to recombine them? The date command has a rich formatting ability, you can easily get it to generate the output you want. For example, with a slight modification to Jesse’s sketch, the date command returns exactly the string you want, with much less code:

#include <Process.h>

Process date;                 // process used to get the date

void setup() {
  Bridge.begin();        // initialize Bridge
  Serial.begin(9600);    // initialize serial  

  while(!Serial);               // wait for Serial Monitor to open

  Serial.println("Time Check");  // Title of sketch
}

void loop() {
  date.begin("/bin/date");
  date.addParameter("+%d/%m/%Y %T");
  date.run();
  
  //if there's a result from the date process, get it.
  while (date.available()>0) {
    String timeString = date.readString();    
    // print the results we got.
    Serial.print(timeString);
  }
  
  delay(5000); // wait 5000ms
}

thanks shape your code is working perfecly, you`re awesome..

I have a question again, when I add a line in your code like :

Serial.println("hello dave");

after this line:

Serial.print(timeString);

and the result is:

18/05/2015 00:51:42 hello dave 18/05/2015 00:52:06 hello dave 18/05/2015 00:52:12 hello dave

I think the result suppose to be:

18/05/2015 00:51:42 hello dave 18/05/2015 00:52:06 hello dave 18/05/2015 00:52:12 hello dave

Ive tried many ways to solve this so what should I do but it wasnt, If I want the display like the last one? tks for your answer

The issue is that the extra newline character is part of the string returned by the Process object that is calling the date command. If you want to put output on the same line, you will probably have to trim that newline character from the timeString variable.

I've not tried it, but you could try:

   String timeString = date.readString();
    timeString.trim();
    // print the results we got.
    Serial.print(timeString);
    Serial.println("hello dave");

ShapeShifter: The issue is that the extra newline character is part of the string returned by the Process object that is calling the date command. If you want to put output on the same line, you will probably have to trim that newline character from the timeString variable.

I've not tried it, but you could try:

   String timeString = date.readString();
    timeString.trim();
    // print the results we got.
    Serial.print(timeString);
    Serial.println("hello dave");

yeah its work, Ive little experience about arduino, this the first time I know about trim.. thank you very much..

But I have to wonder why you are going through all of this work to parse out the individual values, just to recombine them? The date command has a rich formatting ability, you can easily get it to generate the output you want. For example, with a slight modification to Jesse’s sketch, the date command returns exactly the string you want, with much less code:

#include <Process.h>

Process date;                 // process used to get the date

void setup() {
 Bridge.begin();        // initialize Bridge
 Serial.begin(9600);    // initialize serial

while(!Serial);               // wait for Serial Monitor to open

Serial.println(“Time Check”);  // Title of sketch
}

void loop() {
 date.begin("/bin/date");
 date.addParameter("+%d/%m/%Y %T");
 date.run();
 
 //if there’s a result from the date process, get it.
 while (date.available()>0) {
   String timeString = date.readString();    
   // print the results we got.
   Serial.print(timeString);
 }
 
 delay(5000); // wait 5000ms
}




Thanks for this! Helped me a lot as well! Is this getting the time from the internet or from the computer it's plugged into? I'm looking for something that will work when my Yun on it's own, connected to the internet. Any thoughts? Thanks a lot!

jacobjelen: Thanks for this! Helped me a lot as well! Is this getting the time from the internet or from the computer it's plugged into? I'm looking for something that will work when my Yun on it's own, connected to the internet. Any thoughts? Thanks a lot!

The code is running a process on the AR3391 processor running Linux, and returning the result back to the '32U4 processor running the sketch. The Linux process is running the time command which is reading the Linux clock, not getting the time from the Internet. When Linux boots, if it has an Internet connection, it should get the time from the Internet and set the internal Linux clock. But this code is just reading the Linux clock, whether or not it has been set properly from the Internet.