[Question] NTP, time and ESP 8266

For disclosure, this is my first week with Arduino (and C).

My project is an E-Ink screen printer, done from GxEPD2WiFiExample. I'm generating an image using Java, which has current time and some other stuff (mostly temperature sensors, for which I am yet to write mqtt infrastructure and relevant service ecosystem).

The image upload takes 15 seconds, so the best time to make the request to the server (server is adjusted to return an image with +1 minute) is somewhere on the 40th second.

So I'm looking for a working (and simple!) solution to have the actual time.

I found this tutorial - https://www.instructables.com/Getting-Time-From-Internet-Using-ESP8266-NTP-Clock/

But either I'm doing something wrong, or... (the close match is just a lucky case, where I powered the board in the almost perfect moment)

15:37:29.859 -> 0:31:15
15:37:29.859 -> Delay insufficient
15:37:29.859 -> Updating time
15:37:30.917 -> 0:31:16

With the code being:

...
#include "NTPClient.h" (I know that \" should be used for local libraries/files, but this is how the example provided it)
#include "WiFiUdp.h"

...
uint8_t utcOffsetInSeconds = 3*60*60;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
...
void setup() {
...
  timeClient.begin();

  Serial.println("Setup finished");
}
...
void loop() {
  Serial.println("Updating time");
  timeClient.update();

  // Serial.print(daysOfTheWeek[timeClient.getDay()]);
  // Serial.print(", ");
  Serial.print(timeClient.getHours());
  Serial.print(":");
  Serial.print(timeClient.getMinutes());
  Serial.print(":");
  Serial.println(timeClient.getSeconds());
...
}

Thanks

I have tried numerous examples, and I get
Thu Jan 1 00:00:14 1970
in all of them. Tried changing NTP servers to google's, but no luck.

this ran on an ESP8266

// SNTP time
//  from https://techtutorialsx.com/2021/09/01/esp32-system-time-and-sntp/

#include <ESP8266WiFi.h>
 
const char* ssid = "xxxxx";
const char* password =  "zzzzzzzz";
 
const char* ntpServer = "time.google.com";
 
void printTime(){
  struct tm time;
  if(!getLocalTime(&time)){
    Serial.println("Could not obtain time info");
    return;
  }
  Serial.println("\n---------TIME----------");
  Serial.println(asctime(&time));
  char buffer[80];
  strftime(buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", &time);
  Serial.println(buffer);
  strftime(buffer, sizeof(buffer), "%H:%M:%S", &time);
  Serial.println(buffer);
  strftime(buffer, sizeof(buffer), "%Y/%m/%d", &time);
  Serial.println(buffer);
}
 
void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print('.');
  }
  Serial.println("\nConnected with success");
  configTime(0, 3600, ntpServer);
}
 
void loop()
{
  printTime();
  delay(5000);
}

serial monitor output

...........
Connected with success

---------TIME----------
Thu Feb  6 16:36:15 2025

2025/02/06 16:36:15
16:36:15
2025/02/06

---------TIME----------
Thu Feb  6 16:36:20 2025

2025/02/06 16:36:20
16:36:20
2025/02/06
1 Like

May this somehow be related to me not getting IP by default and I need to use something like this:

    WiFi.begin(ssid, password);

  wifi_station_dhcpc_stop();
  struct ip_info info;
  IP4_ADDR(&info.ip, 192, 168, 50, 189);
  IP4_ADDR(&info.gw, 192, 168, 50, 1);
  IP4_ADDR(&info.netmask, 255, 255, 255, 0);
  wifi_set_ip_info(STATION_IF, &info);

Because I am now running from a fresh clean sketch, just by copypasting solutions... and all of them show the same issue.

Finally! Your example works! Thank you

Finally figured it out.

The second example (like in the author's post, but I was too tired to see it), the 'gateway' and 'subnet' were in the wrong places.

Also DNS. At some point I realized that some DNS' simply fail to start the connection.

For anyone, finding this thread

IPAddress staticIP(192, 168, 50, 189); //ESP static ip
  IPAddress gateway(192, 168, 50, 1); //IP Address of your WiFi Router (Gateway)
  IPAddress subnet(255, 255, 255, 0); //Subnet mask
  IPAddress dns(9,9,9,9); //DNS
  IPAddress dns2(149,112,112,112); //DNS
  // wifi_station_dhcpc_stop();
  WiFi.config(staticIP, gateway, subnet, dns, dns2);


  // WiFi.persistent(false);
  // WiFi.mode(WIFI_STA);

  delay(100);

even if solved, i would like to add my stripped down example from the ESP core example:

https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm

2 Likes

Thank you. I believe I stumbled upon your stripped version somewhere here on the forum. It was quite useful (I thought that maybe I missed something when I was trying the Editor`s cut. But one thing is missing in all of the examples I have stumbled upon (except the one from above) - printing that the ntp is not yet resolved(although I have quite a few years of professional Java development, C and Arduino with their lack of documentation was a challenge to get through).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.