Yun time in sketch.

Hello,

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.

Regards Henk,

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.

I know how to work with LuCI to sync.
But i want to replace

setTime(16,52,0,26,12,17); // set time to Saturday 8:29:00am Jan 1 2011

for the the time thats in LuCI.

If i do not use ''set Time" the sketch start in 1970.
Is there a to replace this.

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

Any more information is welcome.

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();
}

The console returns:

Start
Time set to:1514496280
28 21:24:40
28 21:24:41
28 21:24:42
28 21:24:43
28 21:24:44
28 21:24:45
28 21:24:46
28 21:24:47
28 21:24:48
28 21:24:49
28 21:24:50
28 21:24:51
28 21:24:52
28 21:24:53
28 21:24:54
28 21:24:55
28 21:24:56
28 21:24:57
28 21:24:58
28 21:24:59
28 21:25:0
28 21:25:1

Thanks, it works, i only have to give it one hour plus.

now() +3600.

Regards Henk,