simple example for time via internet (esp32)

hello,
I am looking for a very simple example for time via internet (esp32) - no timezone, no winter time, no daylight (summer) time, no displays, just GMT via Serial.print().
All examples I found are overcomplicated and confusing by using those extra timezone (Christensen) or display (LED1602 , OLED, ILI9341) libs, some even need a MAC address/some do not (?). Unfortunately also the Arduino Playground example is very ambiguous and also seems to be outdated...

I just needed a very simple example for
esp32,
get GMP (UTC) time via WiFi,
Serial.println(time)
(e.g., perhaps updated in loop() each second, not using MAC though...,
and it must be compatible with WebServer.h running additionally)

How about this...

#include "time.h"
#include <ESP8266WiFi.h>

const char* ssid     = "*****";                    // your network SSID (name)
const char* password = "**********";       // your network password

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 0;
const int   daylightOffset_sec = 3600;

void setup()
{
  Serial.begin(115200);
  delay(10);
 
  Serial.print("\n\nConnecting to ");
  Serial.println(ssid);
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
 configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
 
  Serial.println("\nWaiting for time");
  unsigned timeout = 5000;
  unsigned start = millis();
  while (!time(nullptr))
  {
    Serial.print(".");
    delay(1000);
  }
  delay(1000);
 
  Serial.println("Time...");
}

void loop()
{
  time_t current = time(nullptr);
  Serial.print(ctime(&current));
  delay(500);
}

thank you very much!
Actually stand-allone it works well, showing the correct time (increasing seconds after 2x500ms).

Unfortunately, if I c+p the snippets to my (huge) proprietary Wifi/Webserver code, then suddenly I always get the wrong day/month/year
Thu Jan 1 01:00:07 1970 (increasing seconds though (I delay 1000ms) )

(PS:
I falsely wrote ESP8266, but actually it's a ESP32, I changed that above -
#include <ESP8266WiFi.h>
is now
#include <WiFi.h> )

Connected to WLAN123  IP address: 192.168.2.137

Connected to WLAN-3YA7LD  IP address: 192.168.2.222

Waiting for time
Time...
HTTP server started!
setup(): done!

Thu Jan  1 01:00:07 1970
Thu Jan  1 01:00:08 1970
Thu Jan  1 01:00:09 1970
Thu Jan  1 01:00:10 1970
Thu Jan  1 01:00:11 1970
Thu Jan  1 01:00:12 1970
Thu Jan  1 01:00:13 1970
Thu Jan  1 01:00:14 1970

any ideas how to fix that Jan 1970 thing in order to retrieve (currently) Dec 2019?

Zoso93:
any ideas how to fix that Jan 1970 thing in order to retrieve (currently) Dec 2019?

As you have discovered, changing the ESP8266WiFi.h to WiFi.h allows it to compile and work on the ESP32.
I can only assume the problem is the way you combine it with your code or something your code does to prevent it working.

it already fails in setup, and then repetitively in loop(), no idea why.

void setup() {

   Serial.begin(500000);
   delay(500);

   //LED_pin=LED_BUILTIN;  // default: LED_pin=LED_BUILTIN
   pinMode(LED_pin, OUTPUT);

   //---------------------------------------------------------
   Adafruit_HX8357_ini(1);  // init function in lib <display_HX3857.h>

   COLOR_BGND = BLACK;
   COLOR_TEXT = WHITE;
   display.fillScreen(COLOR_BGND);
   display.setTextColor(WHITE);
   display.setTextSize(2);
   //display.setFont(&FreeMono9pt7b);
   Serial.println("setup(): display setup done!");
   Serial.println();
   
   //---------------------------------------------------------
   //TS buttons
   TSbutton1.initButton(&display, display.width()-30, 20,  60,30,  CYAN, BLUE, YELLOW, "Btn1", 2);
   TSbutton2.initButton(&display, display.width()-30,100,  60,30,  CYAN, BLUE, YELLOW, "Btn2", 2);
   TSbutton3.initButton(&display, display.width()-30,180,  60,30,  CYAN, BLUE, YELLOW, "Btn3", 2);
   TSbutton4.initButton(&display, display.width()-30,260,  60,30,  CYAN, BLUE, YELLOW, "Btn4", 2);


   Serial.println("setup(): ts buttons setup done!");
   Serial.println();
   

   //---------------------------------------------------------
   // SD
    
   if( !SDioerr ) {
      Serial.println("SD.begin(SD_CS) failed!");
      Serial.println();      
      delay(2000); // delay here
   }
   else {
      SdPath = SD.open("/");
      Serial.println("setup(): SD setup done!\n");      
   }


   //---------------------------------------------------------
   //i2c Wire

   Wire.begin();
   Wire.setClock(400000);
   ads0.begin();
   Serial.println("setup(): i2c+ads1115 setup done!\n");
   
   init_MPU6050();

   //-----------------------------------------------------
   // connecting to router
   //-----------------------------------------------------
   Serial.print("try WiFi, localIP "); Serial.println(local_ip);
   Serial.println();
   display.setTextColor(YELLOW);   
   delay(1);

   //WiFi.mode(WIFI_STA);   delay(1);
   WiFi.config(local_ip, gateway, subnet);
   WiFi.begin(ssid, password);
   delay(1);

   // Wait for connection
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
   }
   delay(1);
   Serial.println();
   Serial.print("  Connected to ");   Serial.print(ssid);
   Serial.print("  IP address: ");
   Serial.println(WiFi.localIP());

   delay(1);

   //---------------------------------------------------------
   //init and get the time
   delay(1);
   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
   delay(1);
   
   Serial.println("\nWaiting for time");
   unsigned start = millis();
   while (!time(nullptr))
   {
      Serial.print(".");
      delay(1000);
   }
   delay(1000);

   Serial.println("Time...");
   currenttime = time(nullptr);
   String StrBuf=ctime(&currenttime);
   
   Serial.print(StrBuf);
   
   delay(100);


   //---------------------------------------------------------
   // WebServer

   server.on("/", handleRoot);
   server.on("/inline", []() {
      server.send(200, "text/plain", "this works as well");
   });
   server.onNotFound(handleNotFound);

   server.begin();
   Serial.println("HTTP server started!");
   delay(1);


   //---------------------------------------------------------
   // end of setup
   Serial.println("setup(): done!\n");
   
   delay(3000);

   

}

prints:

Adafruit_HX8357_ini: Touchscreen started
Adafruit_HX8357_ini: Initializing SD card... SD OK.

setup(): display setup done!

setup(): ts buttons setup done!

setup(): SD setup done!

setup(): i2c+ads1115 setup done!

try WiFi, localIP 192.168.2.222

.
Connected to WLAN-1234 IP address: 192.168.2.222

Waiting for time
Time...
Thu Jan 1 01:00:03 1970
HTTP server started!
setup(): done!

now I wish you all Merry Christmas!

again, Merry Christmas at all!

I now also tried it with this more advanced lib:

but same issue:
original code works,

10:25:09
10:25:10
10:25:11
10:25:12
10:25:13

but as soon as c+p'ed all settings into my own bigger code, it's always starting at 01:00:00 (1970), strangely with some extra 1sec delays.
what the heck...

01:00:19
01:00:21
01:00:23
01:00:25
01:00:27

Does it work if you comment out WiFi.config(local_ip, gateway, subnet);
and also wait for time to sync before progressing to server setup.

 Serial.println("\nWaiting for time");
  while (!time(nullptr))
  {
    Serial.print(".");
    delay(1000);
  }
  delay(1000);

the crucial idea!
in fact, out-commenting WiFi.config(local_ip, gateway, subnet); it works!

other way round, having WiFi.config(local_ip, gateway, subnet); in YOUR example, it also fails!
Thu Jan 1 01:00:02 1970
Thu Jan 1 01:00:02 1970
Thu Jan 1 01:00:03 1970
Thu Jan 1 01:00:03 1970

edit, update,
appearently using the ntp server, additonally a dns server has to be configed:
WiFi.config(local_ip, gateway, subnet, gateway, dns1, dns2);
[solved]

many thanks for your contributions! 8)

Glad you got it sorted out in the end.