The following code provides a correct result for the ESP8266. Unfortunately not for the ESP32 processor. What could be the reason?
Thank you.
//main
#include <Arduino.h>
//#include <ESP8266WiFi.h>
//#include <ESP8266HTTPClient.h>
//#include <WiFiClientSecureBearSSL.h>
//#include <ArduinoJson.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <time.h>
// Configuration of NTP example @ DateTime
#define MY_NTP_SERVER "eu.pool.ntp.org"
#define MY_TZ "CET-1CEST,M3.5.0,M10.5.0/3" // Europe/Zurich CET-1CEST,M3.5.0,M10.5.0/3
unsigned long epoch;
time_t now; // this is the epoch value
tm localT; // the global variable "localT" is of tm structure that provides localT.tm__year values etc.
const char *days[] = {"So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"};
const char *months[] = {"Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"};
const char* charDir[] = {"N", "NO", "O", "SO", "S", "SW", "W", "NW"};
void showTime(); // forward declaration
const char* ssid = "++++";
const char* password = "+++";
void setup() {
Serial.begin(115200);
Serial.printf("\n\nCompiled from: %s at: %s %s", __FILE__, __DATE__, __TIME__);
Serial.println("\nESP32_showtime");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println("\n");
}
void loop(){
showTime();
while(true){yield();};
}
//------------- get & show time -----------------------------
void showTime(){
epoch = time(&now); // now is the current Unix-time as by internal sys-variable
Serial.printf("\n Epoch %lu ",epoch);
time(&now); // read the current time
localtime_r(&now, &localT); // update the structure tm with the current time
Serial.print("\n");
Serial.print(days[(localT.tm_wday)]);
Serial.print(" ");
Serial.print(localT.tm_mday);
Serial.print(".");
Serial.print(months[(localT.tm_mon)]);
Serial.print(" ");
Serial.print(localT.tm_hour);
Serial.print(":");
if (localT.tm_min < 10)
Serial.print("0");
Serial.print(localT.tm_min);
Serial.print(":");
Serial.print(localT.tm_sec);
}
platformio.ini
[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
monitor_speed = 115200
upload_speed = 512000
lib_deps = arduino-libraries/NTPClient@^3.2.1