Objectives
- Connect the M5Stack Core to a 2.4 GHz 802.11 b/g/n Wi-Fi network (5 GHz is not supported by the classic ESP32).
- Retrieve the time via NTP using configTime and display it on the screen.
- Understand that this board has no RTC (unlike the Core2 with its BM8563): the time is lost every time the power is turned off.
- Handle the display of the Europe/Paris time zone with daylight saving time rules via TZ.
Hardware
- 1 M5Stack Core + access to a 2.4 GHz Wi-Fi network (SSID and password known).
- Charged battery or USB power supply 5 V 500 mA minimum (Wi-Fi causes current spikes).
Complete code
#include <M5Unified.h>
#include <WiFi.h>
const char* SSID = "VOTRE_WIFI";
const char* PASS = "VOTRE_MOT_DE_PASSE";
// Europe/Paris : decalage -1 h, changement FR/dernier dimanche
const char* TZ_INFO = "CET-1CEST,M3.5.0,M10.5.0/3";
void setup {
Serial.begin(115200);
auto cfg = M5.config;
M5.begin(cfg);
M5.Display.setTextSize(2);
configTzTime(TZ_INFO, "pool.ntp.org", "time.cloudflare.com");
WiFi.begin(SSID, PASS);
Serial.print("Connexion Wi-Fi");
while (WiFi.status != WL_CONNECTED) {
delay(400); Serial.print(".");
M5.update;
if (M5.BtnA.wasPressed) { WiFi.disconnect; WiFi.begin(SSID, PASS); }
}
Serial.printf("\nIP : %s\n", WiFi.localIP.toString.c_str);
}
void loop {
struct tm t;
M5.Display.fillRect(0, 30, 320, 120, TFT_BLACK);
M5.Display.setCursor(10, 40);
if (getLocalTime(&t, 1000)) {
char buf[24];
strftime(buf, sizeof(buf), "%d/%m/%Y %H:%M:%S", &t);
M5.Display.println(buf);
Serial.println(buf);
} else {
M5.Display.println("Attente NTP...");
}
M5.Display.setCursor(10, 120);
M5.Display.printf("RSSI : %d dBm", WiFi.RSSI);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
Expected verification
- After a few seconds, the screen displays the local date and time (Europe/Paris) ticking by the second, plus the RSSI.
- The serial monitor displays the IP address obtained via DHCP.
- After a restart (double-click the red button then power on again), the time goes back to "Waiting for NTP...": proof that there is no hardware real-time clock.
Common errors
- 5 GHz SSID: the ESP32 only sees 2.4 GHz — check the band on your router.
- Forgetting configTzTime before WiFi.begin or neglecting TZ: the displayed time is in UTC (offset by 1 or 2 hours).
- Using a sensor on ADC2 (G2, G4, G12-15, G25...): ADC2 is blocked while Wi-Fi is running; stick to ADC1 (G32-39).
- Reconnection loop without delay: brownout/reset on weak USB power (the board draws up to 500 mA).
- Believing the time is preserved when powered off: without an RTC or battery, it is lost — replay the NTP sync at startup.