reveil.ino

M5Stack Core (ESP32) — Basic / Gray family (K001 / K002) · File : reveil.ino · Language : Arduino · Use case : Programmer une alarme sonore à heure fixe

The SD sketch's NTP clock becomes an alarm: current time compared to the 07:30 slot, audio alarm on the speaker, clean stop on button A. The NTP-offset snooze shows the price of no RTC.

Hardware required

An M5Stack Core Basic/Gray (ESP32 in an enclosure with a 320×240 TFT, A/B/C buttons, speaker, microSD slot, 110 mAh battery). Wi-Fi for time; no component.

Wiring

No wiring. If Wi-Fi drops, the clock freezes: the alarm stops firing (fail-safe).

How it works

configTime drives the system clock via SNTP; the loop compares tm_hour/tm_min. wasPressed debounces the stop. The snooze reconfigures the offset to push the time back 10 minutes. In the Arduino IDE (esp32 package), board « M5Stack-Core-ESP32 »; install the « M5Stack » (or M5Unified) library from the Library Manager, then Upload.

Going further

On the M5StickC Plus2 (internal RTC) the alarm survives power cuts; here, add a battery-backed standalone buzzer.

reveil.ino

// Reveil NTP : sonne a 7 h 30, arret par bouton A, snooze 10 min par B.
#include <M5Stack.h>
#include <WiFi.h>
#include <time.h>

const char *SSID = "votre-wifi", *CLE = "votre-cle";
bool sonne = false;

void setup() {
  M5.begin(true, false, true, true);
  WiFi.begin(SSID, CLE);
  configTime(3600 * 2, 0, "pool.ntp.org");
  M5.Lcd.setTextSize(2);
}

void loop() {
  M5.update();
  struct tm t;
  time_t maintenant = time(nullptr);
  localtime_r(&maintenant, &t);
  M5.Lcd.setCursor(10, 60);
  M5.Lcd.printf("%02d:%02d:%02d", t.tm_hour, t.tm_min, t.tm_sec);

  if (!sonne && t.tm_hour == 7 && t.tm_min == 30) sonne = true;
  if (sonne) {
    M5.Speaker.tone(1200, 200);
    M5.Lcd.setTextColor(TFT_RED);
    M5.Lcd.drawString("REVEIL !", 100, 130, 3);
    if (M5.BtnA.wasPressed()) { sonne = false; M5.Speaker.mute(); }
    if (M5.BtnB.wasPressed()) {                   // snooze 10 min
      sonne = false;
      configTime(3600 * 2 - 600, 0, "pool.ntp.org");  // trick : heure -10 min
    }
  }
  delay(200);
}

← Back to the board