mpaper_ntp_time_sync.ino

M5Stack M5Paper · File : mpaper_ntp_time_sync.ino · Language : C++ · Use case : Synchroniser l'horloge interne avec un serveur NTP et afficher l'heure locale.

Getting precise time via NTP and displaying it formatted on the e-Ink screen.

Hardware: M5Paper connected to Wi-Fi. Software: WiFi.h and time.h libraries. Execution: Replace Wi-Fi credentials. Code connects, syncs clock with pool.ntp.org, and displays updated local time periodically.

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
}

← Back to the board