1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5StickC Plus2 で Wi-Fi に接続し、NTP を使って時計を作ってみた

Posted at

概要

前の記事では画面表示やボタン押下時の処理などチュートリアルをまとめました。
今回はM5StickC Plus2 を Wi-Fi に接続し、インターネットから現在時刻を取得(NTP同期)することで、リアルタイムで動作する時計を作ってみました。

作ってみたこと

M5StickC Plus2 は RTC(リアルタイムクロック)を搭載していますが、起動時に正しい時刻を設定するには Wi-Fi を利用して NTP(Network Time Protocol)サーバーから時刻を取得するのが便利です。

今回のプログラムでは、

  1. Wi-Fi に接続してインターネットから時刻を取得
  2. 取得した時刻を RTC に設定
  3. Aボタンを押すと時計を表示
  4. 省電力のため Wi-Fi を切断

といった動作をするようにしました。

実装(Wi-Fi & NTP 版)

#include <M5StickCPlus2.h>
#include <WiFi.h>
#include <time.h>

// Wi-Fi 設定(自分のWi-Fi情報に変更)
const char* ssid     = "YOUR_WIFI_SSID";   // Wi-FiのSSID
const char* password = "YOUR_WIFI_PASS";   // Wi-Fiのパスワード

// NTPサーバー設定
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 9 * 3600;  // 日本時間(UTC+9)
const int   daylightOffset_sec = 0;    // 夏時間なし

unsigned long lastUpdate = 0; // 最後の更新時刻
const unsigned long updateInterval = 1000; // 1秒間隔
bool showClock = false; // 時計を表示するフラグ

void setup() {
    M5.begin();
    M5.Lcd.setRotation(1);
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setTextColor(WHITE);
    M5.Lcd.setTextSize(2);

    // 起動時のメッセージ
    M5.Lcd.setCursor(10, 30);
    M5.Lcd.println("Connecting to Wi-Fi...");

    // Wi-Fi 接続
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        M5.Lcd.print(".");
    }
    M5.Lcd.println("\nWi-Fi Connected!");

    // NTPで時刻を同期
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

    struct tm timeInfo;
    if (!getLocalTime(&timeInfo)) {
        M5.Lcd.println("Failed to obtain time");
        return;
    }

    // RTCの初期化
    M5.Rtc.begin();

    // NTPで取得した時刻をRTCに設定
    m5::rtc_date_t date = {timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday}; 
    m5::rtc_time_t timeData = {timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec};
    M5.Rtc.setDate(&date);
    M5.Rtc.setTime(&timeData);

    // Wi-Fi切断(省電力のため)
    WiFi.disconnect(true);
    WiFi.mode(WIFI_OFF);
}

void loop() {
    M5.update();

    // Aボタンが押されたら時計を表示
    if (M5.BtnA.wasPressed()) {
        showClock = true;
        M5.Lcd.fillScreen(BLACK);
    }

    // 時計を表示する場合、1秒ごとに更新
    if (showClock && (millis() - lastUpdate >= updateInterval)) {
        lastUpdate = millis();

        m5::rtc_date_t date;
        m5::rtc_time_t timeData;

        M5.Rtc.getDate(&date);
        M5.Rtc.getTime(&timeData);

        M5.Lcd.fillScreen(BLACK);
        M5.Lcd.setCursor(10, 30);
        M5.Lcd.printf("%04d/%02d/%02d", date.year, date.month, date.date);

        M5.Lcd.setCursor(10, 60);
        M5.Lcd.printf("%02d:%02d:%02d", timeData.hours, timeData.minutes, timeData.seconds);
    }
}

動作の仕組み

起動時の処理

  • Wi-Fi に接続して NTP サーバーから時刻を取得
  • RTC に時刻を設定
  • 省電力のため Wi-Fi を切断

Aボタンが押されたとき

  • 時計表示モードに切り替え

時計表示モード

  • RTC から時刻を取得し、1秒ごとに画面を更新

ポイント

Wi-Fi で時刻を取得

  • configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); → NTP サーバーを使って正しい時間を取得
  • getLocalTime(&timeInfo); → 取得した時刻を RTC に保存

RTC に時刻を設定

  • M5.Rtc.setDate()M5.Rtc.setTime() を使用
  • RTC に保存された時刻は電源オフ後も保持可能

省電力のため Wi-Fi を切断

  • WiFi.disconnect(true);
  • WiFi.mode(WIFI_OFF); → 起動時のみ Wi-Fi を利用

まとめ

  • M5StickC Plus2 を Wi-Fi に接続して NTP で時刻を取得
  • RTC に保存し、電源が切れても時刻を保持
  • Aボタンで時計表示、1秒ごとに画面更新
  • 省電力のため Wi-Fi は起動時のみ使用

この方法なら、M5StickC Plus2 を Wi-Fi なしでも時計として利用できます!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?