pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Air Quality Real-Time Clock

Air Quality RTC clock related API and example program.

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 #include <WiFi.h> #include "time.h" #include <M5Unified.h> const char* ssid = "YOUR WIFI SSID NAME"; const char* password = "YOUR WIFI PASSWORD"; const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 0; const int daylightOffset_sec = 8 * 3600; void setup(){ // —— Initialize M5Unified auto cfg = M5.config(); cfg.serial_baudrate = 115200; // Keep serial output enabled M5.begin(cfg); // Initialize display, RTC, buttons, etc. // —— Initial screen layout ——  M5.Display.fillScreen(TFT_WHITE); M5.Display.setTextSize(2); M5.Display.setTextColor(TFT_BLACK); // —— Connect to Wi-Fi ——  Serial.printf("Connecting to %s\n", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected."); // —— NTP time synchronization ——  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); Serial.print("Syncing time"); #if SNTP_ENABLED while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) { delay(500); Serial.print("."); } #else delay(1500); #endif Serial.println(" done."); // —— Disconnect Wi-Fi to save power ——  WiFi.disconnect(true); WiFi.mode(WIFI_OFF); } void loop(){ M5.update(); // Refresh button/touch/RTC status struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); return; } Serial.println(&timeinfo, "%Y/%m/%d %H:%M:%S"); M5.Display.fillRect(0, 0, 240, 48, TFT_WHITE); M5.Display.setCursor(0, 0); M5.Display.printf("%04d/%02d/%02d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday); M5.Display.setCursor(0, 24); M5.Display.printf("%02d:%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); delay(1000); } 

Once uploaded, you’ll see the following effect:

API

The Air Quality RTC clock section uses the RTC8563_Class from the M5Unified library. For more related APIs, refer to the documentation below: