How do i get the time from my yun board into a sketch.
#include <Console.h>
#include <Process.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
void setup() {
// Bridge startup
Bridge.begin();
Console.begin();
// Wait for Console port to connect
while (!Console);
Console.println("Start");
setTime(16,52,0,26,12,17); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms, to trigger at specific times
Alarm.alarmRepeat(16, 0, 0, LightOn); // 8:30am every day
Alarm.alarmRepeat(22, 30, 0, LightOff); // 8:30;15am every day
}
I want to change "setTime" statement to get time from internet or something like that.
I have been searching but i can not find any solution.
You can use LuCI to syn your board Local Time in the "system" tab.
In case you don't remember how to find it, if your board is in AP mode, connect to it or connect to your wifi if it's in STA then go on arduino.local, default password is doghunter. Click configure then Advanced configuation panel.
You can get the time in unix seconds from the linux side with a process like this:
long getUnixTime() { // This function return a long with the time stamp
String result; //creates a result string
Process time; //names the process Time
time.begin("/bin/date"); //call for Unix "Date" command line utility
time.addParameter("+%s"); // parameters: s for the complete unix time seconds since `00:00:00 1970-01-01 UTC'
time.run(); // run the command
while (time.available() > 0) { // read the output of the command
char c = time.read();
if (c != '\n')
result += c;
}
return result.toInt();
}
That will return a number like: 1514391450
which represents GMT: Wednesday, December 27, 2017 4:17:37 PM
One of the two ways to set the time with the library you are looking at:
setTime(t); // Set the system time to the given time t
"Time uses a special time_t variable type, which is the number of seconds elapsed since 1970. Using time_t lets you store or compare times as a single number, rather that dealing with 6 numbers and details like the number of days in each month and leap years. "
You might have to get the unix timestamp into the time_t variable type or it might just work; you'll have to experiment a little there.
I do understand this part of the sketch but the rest is magic.
Process time; //names the process Time
time.begin("/bin/date"); //call for Unix "Date" command line utility
time.addParameter("+%s"); // parameters: s for the complete unix time seconds since `00:00:00 1970-01-01 UTC'
time.run(); // run the command
Well, I'll mostly let you figure out the function, but this works:
#include <Console.h>
#include <Process.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
void setup() {
// Bridge startup
Bridge.begin();
Console.begin();
// Wait for Console port to connect
while (!Console);
Console.println("Start");
setTime( getUnixTime() );
Console.print("Time set to:");
Console.println( now() );
}
void loop() {
Console.print( day() );
Console.print(" ");
Console.print( hour() );
Console.print(":");
Console.print( minute() );
Console.print(":");
Console.println( second() );
delay(1000);
}
long getUnixTime() { // This function return a long with the time stamp
String result; //creates a result string
Process time; //names the process Time
time.begin("/bin/date"); //call for Unix "Date" command line utility
time.addParameter("+%s"); // parameters: s for the complete unix time seconds since `00:00:00 1970-01-01 UTC'
time.run(); // run the command
while (time.available() > 0) { // read the output of the command
char c = time.read();
if (c != '\n')
result += c;
}
return result.toInt();
}