This tutorial refers to a product that has reached its end-of-life status.

Arduino Yún Time Check

Get the time from a network time server and print it to the serial monitor.

This example for a Yún device gets the time from the Linux processor via Bridge, then parses out hours, minutes and seconds for the Arduino. The Yún device must be connected to a network to get the correct time. If you used the web-based Wi-Fi interface to configure the Yún device for the network, make sure you've selected the proper time zone.

Hardware Required

  • Yún board or shield

  • Wi-Fi network connected to the internet

Circuit

There is no circuit for this example.

The circuit for this tutorial.
The circuit for this tutorial.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

You need to include the Process class :

#include <Process.h>

Create an instance of Process that will be used to get the date, and create variables to hold the current hour, minute, and second. You'll also want to create a variable to store the previous time. To start, put in a value that cannot be a valid time (like a negative number).

1Process date;
2int hours, minutes, seconds;
3int lastSecond = -1;

In @@setup()@ initialize Bridge and Serial. Wait until there is a serial connection before continuing with the sketch and printing a status to the serial monitor.

1void setup() {
2
3 Bridge.begin();
4
5 Serial.begin(9600);
6
7 while(!Serial);
8
9 Serial.println("Time Check");

Run an initial process to get the time by adding the parameter "T" to

date
.

1if (!date.running()) {
2
3 date.begin("date");
4
5 date.addParameter("+%T");
6
7 date.run();
8
9 }
10}

In

loop()
check to see if a second has passed since the previous time. If so, print out the current time to the serial monitor and restart the
date
process if it has stopped.

1void loop() {
2
3 if(lastSecond != seconds) { // if a second has passed
4
5 if (hours <= 9) Serial.print("0"); // adjust for 0-9
6
7 Serial.print(hours);
8
9 Serial.print(":");
10
11 if (minutes <= 9) Serial.print("0"); // adjust for 0-9
12
13 Serial.print(minutes);
14
15 Serial.print(":");
16
17 if (seconds <= 9) Serial.print("0"); // adjust for 0-9
18
19 Serial.println(seconds);
20
21 if (!date.running()) {
22
23 date.begin("date");
24
25 date.addParameter("+%T");
26
27 date.run();
28
29 }
30
31 }

If there is a result from the

date
process, parse the data. First, store the result (hh:mm:ss) in a string, then find the location of the colons with
indexOf()
and
lastIndexOf()
.

1while (date.available()>0) {
2
3 String timeString = date.readString();
4
5 int firstColon = timeString.indexOf(":");
6
7 int secondColon= timeString.lastIndexOf(":");

Knowing the index numbers of the colons, you can separate the hours minutes and seconds as substrings :

1String hourString = timeString.substring(0, firstColon);
2
3 String minString = timeString.substring(firstColon+1, secondColon);
4
5 String secString = timeString.substring(secondColon+1);

Finally, convert the strings to ints, and save the previous second to do a time comparison in the next

loop()
.

1hours = hourString.toInt();
2
3 minutes = minString.toInt();
4
5 lastSecond = seconds;
6
7 seconds = secString.toInt();
8
9 }
10}

The complete code follows :

1/*
2
3 Time Check
4
5 Gets the time from Linux via Bridge then parses out hours,
6
7 minutes and seconds using a YunShield/Yún.
8
9 created 27 May 2013
10
11 modified 21 June 2013
12
13 By Tom Igoe
14
15 This example code is in the public domain.
16
17 http://www.arduino.cc/en/Tutorial/TimeCheck
18
19 */
20
21#include <Process.h>
22
23Process date; // process used to get the date
24int hours, minutes, seconds; // for the results
25int lastSecond = -1; // need an impossible value for comparison
26
27void setup() {
28
29 Bridge.begin(); // initialize Bridge
30
31 SerialUSB.begin(9600); // initialize serial
32
33 while (!Serial); // wait for Serial Monitor to open
34
35 SerialUSB.println("Time Check"); // Title of sketch
36
37 // run an initial date process. Should return:
38
39 // hh:mm:ss :
40
41 if (!date.running()) {
42
43 date.begin("date");
44
45 date.addParameter("+%T");
46
47 date.run();
48
49 }
50}
51
52void loop() {
53
54 if (lastSecond != seconds) { // if a second has passed
55
56 // print the time:
57
58 if (hours <= 9) {
59
60 SerialUSB.print("0"); // adjust for 0-9
61
62 }
63
64 SerialUSB.print(hours);
65
66 SerialUSB.print(":");
67
68 if (minutes <= 9) {
69
70 SerialUSB.print("0"); // adjust for 0-9
71
72 }
73
74 SerialUSB.print(minutes);
75
76 SerialUSB.print(":");
77
78 if (seconds <= 9) {
79
80 SerialUSB.print("0"); // adjust for 0-9
81
82 }
83
84 SerialUSB.println(seconds);
85
86 // restart the date process:
87
88 if (!date.running()) {
89
90 date.begin("date");
91
92 date.addParameter("+%T");
93
94 date.run();
95
96 }
97
98 }
99
100 //if there's a result from the date process, parse it:
101
102 while (date.available() > 0) {
103
104 // get the result of the date process (should be hh:mm:ss):
105
106 String timeString = date.readString();
107
108 // find the colons:
109
110 int firstColon = timeString.indexOf(":");
111
112 int secondColon = timeString.lastIndexOf(":");
113
114 // get the substrings for hour, minute second:
115
116 String hourString = timeString.substring(0, firstColon);
117
118 String minString = timeString.substring(firstColon + 1, secondColon);
119
120 String secString = timeString.substring(secondColon + 1);
121
122 // convert to ints,saving the previous second:
123
124 hours = hourString.toInt();
125
126 minutes = minString.toInt();
127
128 lastSecond = seconds; // save to do a time comparison
129
130 seconds = secString.toInt();
131
132 }
133
134}

Last revision 2016/05/25 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.