Hi all,
I am have bougth new Arduino UNO Rev2 WIFI less than three weeks ago and I try to use to create a simple NTP Client.
I have understood how to get UNIX timestamp using NTPClient library.
After that I would convert these Unix timestamp in a string formatted like this:
dd/mm/yyyy hh:mm:ss
This is my actual code
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <time.h>
const char *ssid = "FRITZ!Box";
const char *password = "FritzPass";
char orario[80];
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup() {
Serial.begin(9600);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid,password);
delay(500);
Serial.println("In attesa di connessione...");
}
timeClient.begin();
}
void loop() {
timeClient.update();
Serial.println(timeClient.getEpochTime());
strftime(orario,80,"%d/%m/%y -- %H:%M:%S", timeClient.getEpochTime());
Serial.println(orario);
delay(1000);
}
I can compile these code, but Unix timestamp is not properly converted as you can see in the picture...
How can I solve it?
Is it correct to use strftime C function?
Best regards
Federico
Doesn't timeClient.getEpochTime() return an epoch time but strftime require a tm struct?
Hi,
Yes timeClient.getEpochTime() return an epoch time (Unix timestamp).
WiFi.getTime() return time in seconds since January 1st, 1970.
I need to "edit" this epoch time and format this value in a string with the following format:
day/month/year -- hour: minutes: seconds
Thanks 
Juraj
5
epoch time is seconds since January 1st, 1970
so the same thing
Hi all,
I solved in this way:
- I download and added TimeLib.h library in my code
- I used day(), month(), year(), hour(), minute() and second() functions to manipulate epoch time.
For example:
time_t t=timeClient.getEpochTime();
datetime=String(day(t))+"/"+String(month(t))+"/"+String(year(t))+" -- "+String(hour(t))+":"+String(minute(t))+":"+String(second(t));
Thanks @Juraj
but now I need to edit this value to get day, month, year, hour, minutes and seconds.
Regards
Juraj
8
better is to use TmeLib's setTime(now);
. then the library will keep the time with millis().
then you can always get the current time with hour(), minute(), seconds().
(it is possible to set the TimeLib to periodically call WiFi.setTime to adjust the time.)
1 Like
@coppolino you have to use the predefined (in time.h) struct tm for strftime() function.
time_t now = timeClient.getEpochTime();
struct tm timeInfo = *localtime(&now);
strftime(orario, sizeof(orario), "%d/%m/%Y -- %H:%M:%S", &timeInfo );
1 Like
system
Closed
10
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.