pdf-icon

Arduino Quick Start

2. Devices & Examples

Air Quality Wakeup

APIs and example program for Air Quality sleep and wakeup functionality.

Example

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <M5Unified.h>
#include <esp_sleep.h>

void setup() {
  auto cfg = M5.config();
  M5.begin(cfg);                         // Initialize display, buttons, RTC, etc.

  M5.Display.fillScreen(TFT_WHITE);
  M5.Display.setTextSize(2);
  M5.Display.setTextColor(TFT_BLACK);
  M5.Display.setTextDatum(middle_center);

  esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  int cx = M5.Display.width()  / 2;
  int cy = M5.Display.height() / 2;
  if (cause == ESP_SLEEP_WAKEUP_TIMER) {
    M5.Display.drawString("Woken up!", cx, cy - 20);
    M5.Display.drawString("Press BtnA", cx, cy + 20);
  } else {
    M5.Display.drawString("Press BtnA", cx, cy - 20);
    M5.Display.drawString("to sleep 5s", cx, cy + 20);
  }
}

void loop() {
  M5.update();                           // Refresh button state

  // Press BtnA to enter 5-second timed sleep
  if (M5.BtnA.wasPressed()) {
    int cx = M5.Display.width()  / 2;
    int cy = M5.Display.height() / 2;
    M5.Display.fillScreen(TFT_WHITE);
    M5.Display.setTextDatum(middle_center);
    M5.Display.drawString("Sleeping...", cx, cy);
    delay(500);
    M5.Power.timerSleep(5);              // Wake up after 5 seconds
  }
}

After uploading, press the button to see the following effects:

API

The power section uses the Power_Class from the M5Unified library. For more related APIs, refer to the following documentation:

On This Page