pdf-icon

Arduino入門

2. デバイス&サンプル

6. アプリケーション

Dial RTC 実時間時計

Dial RTC(Real-Time Clock)に関連する API とサンプルプログラム。

サンプルプログラム

ビルド要件

  • M5Stack ボードマネージャーバージョン >= 3.2.2
  • ボードオプション = M5Dial
  • M5Dial ライブラリバージョン >= 1.0.3
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
#define WIFI_SSID     "********"
#define WIFI_PASSWORD "********"

#define NTP_TIMEZONE "UTC-8"  // POSIX standard, in which "UTC+0" is UTC London, "UTC-8" is UTC+8 Beijing, "UTC+5" is UTC-5 New York
#define NTP_SERVER1  "0.pool.ntp.org"
#define NTP_SERVER2  "1.pool.ntp.org"
#define NTP_SERVER3  "2.pool.ntp.org"
#include <WiFi.h>

// Different versions of the framework have different SNTP header file names and availability.
#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

#ifndef SNTP_ENABLED
#define SNTP_ENABLED 0
#endif

#include <M5Dial.h>

static constexpr const char* const wd[7] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

void setup() {
  M5Dial.begin();
  M5Dial.Display.setFont(&fonts::FreeMono9pt7b);
  M5Dial.Display.setCursor(0, 60);
  M5Dial.Display.println("RTC Test");

  if (!M5Dial.Rtc.isEnabled()) {
    M5Dial.Display.println("RTC not found");
    while (true) {
      delay(500);
    }
  }
  M5Dial.Display.println("RTC found");

  M5Dial.Display.print("WiFi: ");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    M5Dial.Display.print(".");
    delay(500);
  }
  M5Dial.Display.println("\nWiFi connected");

  configTzTime(NTP_TIMEZONE, NTP_SERVER1, NTP_SERVER2, NTP_SERVER3);

  M5Dial.Display.print("NTP: ");
#if SNTP_ENABLED
  while (sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) {
    M5Dial.Display.print(".");
    delay(500);
  }
#else
  struct tm timeInfo;
  while (!getLocalTime(&timeInfo, 1000)) {
    M5Dial.Display.print(".");
    delay(500);
  }
#endif
  M5Dial.Display.println("\nNTP connected");

  time_t t = time(nullptr) + 1;  // Advance one second
  while (t > time(nullptr))
    ;  // Synchronization in seconds
  M5Dial.Rtc.setDateTime(gmtime(&t));

  delay(1000);
  M5Dial.Display.clear();
  M5Dial.Display.setCursor(70, 20);
  M5Dial.Display.println("RTC Test");
}

void loop() {
  auto dt = M5Dial.Rtc.getDateTime();
  M5Dial.Display.setCursor(40, 50);
  M5Dial.Display.printf("RTC   UTC  :");
  M5Dial.Display.setCursor(40, 65);
  M5Dial.Display.printf("%04d/%02d/%02d(%s)", dt.date.year, dt.date.month, dt.date.date, wd[dt.date.weekDay]);
  M5Dial.Display.setCursor(40, 80);
  M5Dial.Display.printf("%02d:%02d:%02d", dt.time.hours, dt.time.minutes, dt.time.seconds);

  // ESP32 internal timer
  auto t = time(nullptr);

  {
    auto tm = gmtime(&t);  // for UTC
    M5Dial.Display.setCursor(40, 110);
    M5Dial.Display.printf("ESP32 UTC  :");
    M5Dial.Display.setCursor(40, 125);
    M5Dial.Display.printf("%04d/%02d/%02d(%s)", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday]);
    M5Dial.Display.setCursor(40, 140);
    M5Dial.Display.printf("%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
  }

  {
    auto tm = localtime(&t);  // for local timezone
    M5Dial.Display.setCursor(40, 170);
    M5Dial.Display.printf("ESP32 %s:", NTP_TIMEZONE);
    M5Dial.Display.setCursor(40, 185);
    M5Dial.Display.printf("%04d/%02d/%02d(%s)", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, wd[tm->tm_wday]);
    M5Dial.Display.setCursor(40, 200);
    M5Dial.Display.printf("%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
  }

  delay(500);
}

プログラム内に接続する Wi-Fi 名称とパスワード、および現地のタイムゾーンを記入し、アップロードボタンをクリックすると、実時間時計が表示されます。

注意
プログラム内のタイムゾーン定義 NTP_TIMEZONE は POSIX 標準に従っており、一般的な人間が読みやすい形式とは逆になります。例えば、"UTC+0" は UTC ロンドン、"UTC-8" は UTC+8 北京、"UTC+5" は UTC-5 ニューヨークを意味します。

API

M5Dial ライブラリは M5Unified ライブラリを基盤としており、RTC 時計機能は M5Unified 内の RTC8563_Class を利用しています。詳細な API は以下のドキュメントを参照してください:

On This Page