English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

StickC RTC Real-Time Clock

APIs and example programs related to the M5StickC RTC.

Example Program

Build Requirements

  • M5Stack board package version >= 3.3.8
  • Board selection = M5StickC
  • M5Unified library version >= 0.2.18
  • M5GFX library version >= 0.2.24
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#if defined(ARDUINO)
#define WIFI_SSID     "your-ssid"
#define WIFI_PASSWORD "your-password"
#define NTP_TIMEZONE  "UTC-8"
#define NTP_SERVER1   "0.pool.ntp.org"
#define NTP_SERVER2   "1.pool.ntp.org"
#define NTP_SERVER3   "2.pool.ntp.org"

#include <WiFi.h>

#if __has_include(<esp_sntp.h>)
#include <esp_sntp.h>
#define SNTP_ENABLED 1
#elif __has_include(<sntp.h>)
#include <sntp.h>
#define SNTP_ENABLED 1
#endif

#endif

#ifndef SNTP_ENABLED
#define SNTP_ENABLED 0
#endif

#include <M5Unified.h>

void setup(void)
{
    auto cfg = M5.config();
    M5.begin(cfg);
    Serial.begin(115200);

    M5.Display.setRotation(1);
    M5.Display.setTextColor(TFT_GREEN);
    M5.Display.setTextFont(1);
    M5.Display.setTextSize(1);
    M5.Display.setTextDatum(top_left);
    M5.Display.setTextWrap(false);
    M5.Display.clear();

    if (!M5.Rtc.isEnabled()) {
        Serial.println("RTC not found.");
        M5.Display.println("RTC not found.");
        for (;;) {
            vTaskDelay(500);
        }
    }

    Serial.println("RTC found.");

    M5.Display.println("WiFi connecting...");
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print('.');
        delay(500);
    }
    Serial.println("\r\nWiFi Connected.");
    M5.Display.println("WiFi connected.");

    configTzTime(NTP_TIMEZONE, NTP_SERVER1, NTP_SERVER2, NTP_SERVER3);

#if SNTP_ENABLED
    while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
        Serial.print('.');
        delay(1000);
    }
#else
    delay(1600);
    struct tm timeInfo;
    while (!getLocalTime(&timeInfo, 1000)) {
        Serial.print('.');
    }
#endif

    Serial.println("\r\nNTP Connected.");
    M5.Display.println("NTP connected.");

    time_t t = time(nullptr) + 1;
    while (t > time(nullptr)) {
        delay(10);
    }

    M5.Rtc.setDateTime(gmtime(&t));
}

void loop(void)
{
    static constexpr const char* const wd[7] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    };

    delay(500);

    auto dt = M5.Rtc.getDateTime();
    Serial.printf("RTC   UTC  :%04d/%02d/%02d (%s)  %02d:%02d:%02d\r\n",
                  dt.date.year, dt.date.month, dt.date.date,
                  wd[dt.date.weekDay], dt.time.hours, dt.time.minutes,
                  dt.time.seconds);

    time_t t = time(nullptr);

    auto utc = gmtime(&t);
    Serial.printf("ESP32 UTC  :%04d/%02d/%02d (%s)  %02d:%02d:%02d\r\n",
                  utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday,
                  wd[utc->tm_wday], utc->tm_hour, utc->tm_min, utc->tm_sec);

    auto local = localtime(&t);
    Serial.printf("ESP32 %s:%04d/%02d/%02d (%s)  %02d:%02d:%02d\r\n",
                  NTP_TIMEZONE, local->tm_year + 1900, local->tm_mon + 1,
                  local->tm_mday, wd[local->tm_wday], local->tm_hour,
                  local->tm_min, local->tm_sec);

    M5.Display.clear();

    M5.Display.setCursor(0, 0);
    M5.Display.print("RTC UTC");
    M5.Display.setCursor(0, 12);
    M5.Display.printf("%04d/%02d/%02d (%s) %02d:%02d:%02d",
                      dt.date.year, dt.date.month, dt.date.date,
                      wd[dt.date.weekDay], dt.time.hours, dt.time.minutes,
                      dt.time.seconds);

    M5.Display.setCursor(0, 24);
    M5.Display.print("ESP32 UTC");
    M5.Display.setCursor(0, 36);
    M5.Display.printf("%04d/%02d/%02d (%s) %02d:%02d:%02d",
                      utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday,
                      wd[utc->tm_wday], utc->tm_hour, utc->tm_min,
                      utc->tm_sec);

    M5.Display.setCursor(0, 48);
    M5.Display.printf("ESP32 %s", NTP_TIMEZONE);
    M5.Display.setCursor(0, 60);
    M5.Display.printf("%04d/%02d/%02d (%s) %02d:%02d:%02d",
                      local->tm_year + 1900, local->tm_mon + 1,
                      local->tm_mday, wd[local->tm_wday], local->tm_hour,
                      local->tm_min, local->tm_sec);
}

API

This example uses the RTC class in M5Unified. The RTC functionality uses RTC8563_Class from M5Unified. For more related APIs, refer to the documentation below:

Page Tools
PDF
On This Page