NTP library for Wio Terminal

Hi,

I need to query pool.ntp.org NTP to get time and date. I can do this successfully on ESP using NTPClient.h and WiFiUdp.h

When I put the same code on the Wio Terminal, it sort-of works. The time and epochTime work fine, but Month Day, Month and Year produce incorrect results.

This is the part of the code that produces the incorrect date values:

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

Is there a reason why this produces incorrect date values on the Wio Terminal? Is there a native NTP library in the pipeline?

Thanks in advance

Hi @ardvino,

Could you try this please?

Best Regards,
Lakshantha

I use arduino-libraries/NTPClient.
My code is here.

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

Hi @ardvino,

I understand that you don’t need the RTC functionality on the Wio Terminal, that will make your project more complex.

So, I have found a different library and have tested on the Wio Terminal. It seems to work well.

Following is the library:

Please use the following example inside the library:

Please delete the following libraries:

  • ESP8266WiFi.h
  • WiFiUdp.h

Then, add the following libraries:

  • rpcWiFi.h
  • Arduino.h

Also, use pool.ntp.org as the ntpServerName, since it gave me the most accurate time result.

Finally, change the following line to Serial.println(localPort);

Furthermore, you can visit the following header file inside the library to check all the available functions:

Here is my version after doing the above alterations:

Hope it helps!

Best Regards,
Lakshantha

Thank you for taking time to look at this @lakshan! Much appreciated!

This works great and is a much more streamlined way of me accessing NTP time.

I’m really enjoying using the Wio Terminal! Thank you for your help :slight_smile:

1 Like