mpaper_ntp_time_sync.ino

M5Stack M5Paper · Fichier : mpaper_ntp_time_sync.ino · Langage : C++ · Usage : Synchroniser l'horloge interne avec un serveur NTP et afficher l'heure locale.

Obtention de l'heure précise via NTP et affichage formaté sur l'écran e-Ink.

Matériel : M5Paper connecté au Wi-Fi. Logiciel : Bibliothèques WiFi.h et time.h. Exécution : Remplacez les identifiants Wi-Fi. Le code se connecte, synchronise l'horloge avec pool.ntp.org et affiche l'heure locale mise à jour périodiquement.

mpaper_ntp_time_sync.ino

#include <M5Paper.h>
#include <WiFi.h>
#include <time.h>

const char* ssid = "VOTRE_WIFI";
const char* password = "VOTRE_MOT_DE_PASSE";

// Configuration du fuseau horaire (ex: Europe/Paris)
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;

void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  
  M5.display.clear();
  M5.display.setTextSize(4);
  M5.display.setCursor(10, 10);
  M5.display.println(&timeinfo, "%H:%M:%S");
  
  M5.display.setTextSize(2);
  M5.display.setCursor(10, 80);
  M5.display.println(&timeinfo, "%A, %B %d %Y");
  M5.display.update();
}

void setup() {
  M5.begin();
  Serial.begin(115200);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi");
  
  // Initialisation de l'horloge NTP
  configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org");
}

void loop() {
  printLocalTime();
  delay(10000); // Mettre à jour toutes les 10 secondes
}

← Retour à la fiche