Hi both,
Thank you for your replies.
@matsujirushi Thanks, but I need to output both time and formatted date (year-month-monthDay). I can only see how to output time from the arduino NTPClient.
@lakshan Thank you, I had not seen this. I have tried the code and it does work. However, it seems to only get Epoch time in seconds, then use that to set the inbuilt RTC in order to output in a formatted fashion. I cannot access formatted time or date without first ‘translating it’ via the inbuilt hardware RTC. This is very complicated and problematic for my application and I will explain why:
(I am currently using ESP to do the follwing task, and it works fine)
I need to get several time values from the NTP server in a burst; dateTime now, dateTime now +1min, dateTime now -29mins, dateTime now -30mins, dateTime now +30mins and dateTime now +31mins. I achieve this by setting time offsets when I make each request. That way, I don’t have to do complex calculations to do with day/month/year roll-overs etc. and the NTP request handles that for me. This is very straight forward with the ESP NTP code. Below is the ESP ardunio code I use for a simple dateTime request (without the multiple offsets, for clarity):
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char *ssid = "--------";
const char *password = "--------";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
//Week Days
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//Month names
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(0);
}
void loop() {
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
Serial.print("Epoch Time: ");
Serial.println(epochTime);
String formattedTime = timeClient.getFormattedTime();
Serial.print("Formatted Time: ");
Serial.println(formattedTime);
int currentHour = timeClient.getHours();
Serial.print("Hour: ");
Serial.println(currentHour);
int currentMinute = timeClient.getMinutes();
Serial.print("Minutes: ");
Serial.println(currentMinute);
int currentSecond = timeClient.getSeconds();
Serial.print("Seconds: ");
Serial.println(currentSecond);
String weekDay = weekDays[timeClient.getDay()];
Serial.print("Week Day: ");
Serial.println(weekDay);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
Serial.print("Month day: ");
Serial.println(monthDay);
int currentMonth = ptm->tm_mon+1;
Serial.print("Month: ");
Serial.println(currentMonth);
String currentMonthName = months[currentMonth-1];
Serial.print("Month name: ");
Serial.println(currentMonthName);
int currentYear = ptm->tm_year+1900;
Serial.print("Year: ");
Serial.println(currentYear);
//Print complete date:
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
Serial.print("Current date: ");
Serial.println(currentDate);
Serial.println("");
delay(2000);
}
On the ESP, this returns something like this:
Epoch Time: 1611883285
Formatted Time: 01:21:25
Hour: 1
Minutes: 21
Seconds: 25
Week Day: Friday
Month day: 29
Month: 1
Month name: January
Year: 2021
Current date: 2021-1-29
When I put the same code onto the Wio Terminal (with #include <Arduino.h> and #include <rpcWiFi.h> rather than #include <ESP8266WiFi.h>), I get this:
Epoch Time: 1611883712
Formatted Time: 01:28:32
Hour: 1
Minutes: 28
Seconds: 32
Week Day: Friday
Month day: 28
Month: 7
Month name: July
Year: -3589986
Current date: -3589986-7-28
As you can see, the time is fine, as is the week day name, but Month day, Month, Month name and Year are wrong. This means my formatted ‘Current date’ is also wrong.
It seems that for some reason the problem is arising from the functions that rely on
struct tm *ptm = gmtime ((time_t *)&epochTime);
Any idea why this might be? Is there a way I can modify this code to work on the Wio Terminal? I would really prefer to use the dateTime info directly from NTP without having to put it through the RTC multiple times.
Thank you