Arduino YUN Time Synchronization

Arduino YUN Access Point mode Time Synchronization

Arduino YUN Access Point mode Time Synchronization

I will show how to synchronize the Arduino YUN board in Access Point (AP) configuration by using the client browser clock.

Arduino YUN boards use a quartz crystal for timing, but it does not have battery and real time clock onboard, so it can’t  remember time when the device is powered off. Instead, it is able to sync with time servers automatically when it powers up and connects to an existing network using NTP.

It is possible to synchronize the Arduino YUN clock in AP mode with a simple software trick, by requesting the client browser clock and then set the onboard clock via UNIX command.

Hardware
Arduino YUN

Firmware Arduino Code
Firmware for Arduino YUN programming

/***********************************************************
 * Arduino YUN Time Synchronization
 * in Acces Mode configuration
 * by using bridge library
 * license: Creative Commons Attribution 4.0
 * author: Physics Light
 * date: 2 July 2014
 ***********************************************************/
// Libraries
 #include <Bridge.h>
 #include <YunServer.h>
 #include <YunClient.h>
// Init YUN Server
YunServer server; // init YunServer object named server
// Function
void msgCmd (YunClient client) {
char command;
String msgCmd = client.readStringUntil('/');
msgCmd.trim();
// clock setting message
if (msgCmd == "clock") {
// call the function to set time
setBoardTime(client);
}
}
// set onboard time via browser clock
void setBoardTime(YunClient client) {
String result;
// read time from browser, format: seconds since 1 Jan 1970
// after point there are milliseconds, eliminated
String time = client.readStringUntil('.');
time.trim();
Process setTime;
// Linux command line to set time
// command date
// parameters %s seconds since 1 Jan 1970
// -s set command
// @ time in seconds since 1 Jan 1970
String cmdTimeStrg = "date +%s -s @" + time;
setTime.runShellCommand(cmdTimeStrg);
}
// this function return a string with the Arduino YUN time stamp
String getTimeStamp() {
String result;
Process time;
// Linux command line to get time
// command date
// parameters D for the complete date mm/dd/yy
// T for the time hh:mm:ss
time.begin("date");
time.addParameter("+%D-%T");
time.run(); // run the command
// read the output of the command
while (time.available() > 0) {
char c = time.read();
if (c != '\n')
result += c;
}
return result;
}
void setup() {
Bridge.begin();
server.listenOnLocalhost();
server.begin();
}
void loop() {
// check the Arduino YUN internal time
String boardTime = getTimeStamp();
// start client
YunClient client = server.accept(); // get clients coming from server
if (client) {
String cmd = client.readStringUntil('/');
cmd.trim(); // eliminate whitespace
// check the command
// data command: print the Arduino YUN clock in client browser
if ( cmd == "data"){
Serial.print("Time: " );
Serial.println (boardTime);
client.print( "<p> Time " + boardTime + "</p>");
}
// msg command: get clock from client browser
if ( cmd == "msg" ) msgCmd(client);
}
// stop client
client.stop();
}

HTML Web page code
This is the client web app code in HTML

</pre>
<pre><!DOCTYPE html>
<head>
<title>YUN Sync Time</title>
</head>
<html>
<font face="arial" size="3" color="#424242">
<h2> Arduino YUN Synchronization Time </h2>
<script src="jquery-1.9.1.js"></script>
<!-- TIME SYNC CLOCK BUTTON -->
<p>
<input type="button" onclick="clockMsg()" value="SYNC TIME"/>
</p>
<script type="text/javascript"> 
function clockMsg() {
 var d = new Date();
 var n = d.getTime() / 1000;
 $.get('/arduino/msg/clock/' + n + '/');
 return false;
}
</script>
<!-- GET ARDUINO YUN TIME -->
<script type="text/javascript">
function refresh() {
 $('#content').load('/arduino/data/');
}
</script>
<body onload="setInterval(function() {refresh()}, 1000);">
 <span id="content">Waiting...</span>
</body> 
</html>
</font>

Connect wireless your device (notebook, phone, pad) to YUN wiFi network Yún-XXXXXXXXXXXX. Upload the firmware and the web page wirelessly by using the Arduino IDE. Once connected you can reach the web page at http://192.168.240.1/sd/YUN_sync_time/

You can synchronize the onboard YUN clock by pressing the SYNC TIME button.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: