🛠 Wi-Fi 2,4 GHz et horloge NTP sur l'écran

M5Stack Core (ESP32) — famille Basic / Gray (K001 / K002) · intermediaire · Schéma : IMG-m5stack-core-esp32-intermediaire-3-m5stack-core-esp32-wifi-ntp.svg — M5Stack Core relié par des ondes Wi-Fi 2,4 GHz à une box Internet puis à un serveur NTP, écran affichant date et heure, mention « pas de RTC : remise à zéro à chaque redémarrage »

Objectifs

Matériel

Code complet

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

Vérification attendue

Erreurs fréquentes

Autres tutoriels pour cette carte

Exemples de code pour cette carte

← Retour à la fiche 🛠 Tous les tutoriels