- // NTP Clock. (c)2022 @logic_star Allrights reserved.
- //#define IMAGE_FROM_SD //SDカードからイメージを読み込む場合は有効化要
- //#define CORE2 //CORE2を使用する場合は有効化要
- #ifndef CORE2
- #include <M5Stack.h>
- #else
- #include <M5Core2.h>
- #endif
- #include <WiFi.h>
- #include <time.h>
- #ifndef IMAGE_FROM_SD
- #include "image.h"
- #endif
- // Wifi 関連定義
- const char* ssid = "SSID"; //WiFi APのSSID
- const char* password = "PASSWORD"; //WiFi APのPassword
- const char* ntpServer = "ntp.nict.jp";
- const long gmtOffset_sec = 9 * 3600;
- const int daylightOffset_sec = 0;
- WiFiClient client;
- // 時間関連
- struct tm timeinfo;
- uint8_t secLastReport = 0;
- // NTPによる時刻取得関数
- int ntp(){
- uint8_t wifi_retry_cnt;
- M5.Lcd.fillScreen(TFT_BLACK); //画面初期化
- M5.Lcd.setTextSize(1);
- M5.Lcd.setCursor(0, 0);
- M5.Lcd.printf("Connecting to %s\n", ssid);
- WiFi.begin(ssid, password); //WiFi接続開始
- wifi_retry_cnt = 20; //0.5秒×20=最大10秒で接続タイムアウト
- while (WiFi.status() != WL_CONNECTED){
- delay(500);
- M5.Lcd.printf("*"); //0.5秒毎に”*”を表示
- if(--wifi_retry_cnt == 0) {
- WiFi.disconnect(true); //タイムアウトでWiFiオフ
- WiFi.mode(WIFI_OFF);
- M5.Lcd.printf("\nCONNECTION FAIL"); //WiFi接続失敗表示
- return(false); //接続失敗でリターン
- }
- }
- M5.Lcd.printf("\nCONNECTED"); //WiFi接続成功表示
- configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); //NTPによる時刻取得
- if (!getLocalTime(&timeinfo)) {
- M5.Lcd.printf("/nFailed to obtain time"); //時刻取得失敗表示
- WiFi.disconnect(true); //時刻取得失敗でWiFiオフ
- WiFi.mode(WIFI_OFF);
- M5.Lcd.printf("/nFailed to obtain time"); //時刻取得失敗表示
- return (false); //時刻取得失敗でリターン
- }
- WiFi.disconnect(true); //WiFi切断
- WiFi.mode(WIFI_OFF); //WiFiオフ
- M5.Lcd.fillScreen(TFT_BLACK); //画面消去
- return (true); //時刻取得成功でリターン
- }
- //イメージデータ関連
- #ifdef IMAGE_FROM_SD //SDからイメージデータを読み込む場合
- //SDカード上のファイル名を定義
- const char* image_data[17] = {"/0.jpg", "/1.jpg", "/2.jpg", "/3.jpg", "/4.jpg",
- "/5.jpg", "/6.jpg", "/7.jpg", "/8.jpg", "/9.jpg",
- "/sun.jpg", "/mon.jpg", "/tue.jpg", "/wed.jpg", "/thu.jpg", "/fri.jpg", "/sat.jpg"};
- #else //image.hでインクルードしたイメージデータを使用する場合
- //各イメージデータの配列名を定義
- const unsigned char* image_data[17] = {image0, image1, image2, image3, image4,
- image5, image6, image7, image8, image9,
- sun, mon, tue, wed, thu, fri, sat};
- //各イメージデータのサイズを定義
- const uint32_t image_size[17] = {sizeof image0, sizeof image1, sizeof image2, sizeof image3, sizeof image4,
- sizeof image5, sizeof image6, sizeof image7, sizeof image8, sizeof image9,
- sizeof sun, sizeof mon, sizeof tue, sizeof wed, sizeof thu, sizeof fri, sizeof sat};
- #endif
- // x, yの位置にnumberの数字または曜日を表示
- void PutJpg(uint16_t x, uint16_t y, uint16_t number){
- #ifdef IMAGE_FROM_SD
- M5.Lcd.drawJpgFile(SD, image_data[number], x, y);
- #else
- M5.Lcd.drawJpg(image_data[number], image_size[number], x, y);
- #endif
- }
- // x, yの位置からnumberの数値をdigit桁で表示。文字間隔はx_offset
- void PutNum(uint16_t x, uint16_t y, uint16_t x_offset, uint8_t digit, uint16_t number){
- int temp = number;
- int loop;
- for(loop = digit ; loop > 0 ; loop--){
- PutJpg(x + x_offset * (digit - loop), y, temp / int(pow(10, (loop - 1))));
- temp = temp % int(pow(10, (loop - 1)));
- }
- }
- void setup() {
- M5.begin();
- M5.Lcd.fillScreen(TFT_BLACK); //M5Stackの画面初期化
- M5.Lcd.setTextFont(4);
- M5.Lcd.setTextSize(1);
- M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
- M5.Lcd.setTextDatum(TL_DATUM);
- if(ntp() == false) while(1); //時刻取得に失敗した場合は、動作停止
- }
- void loop() {
- getLocalTime(&timeinfo);
- if((timeinfo.tm_hour == 2)&&(timeinfo.tm_min == 0)&&(timeinfo.tm_sec == 0)) ntp(); //毎日午前2時に時刻取得。時刻取得に失敗しても動作継続
- if(secLastReport != timeinfo.tm_sec) { //秒が更新されたら、表示をupdate
- secLastReport = timeinfo.tm_sec;
- delay(10);
- M5.Lcd.setCursor(76, 14);
- M5.Lcd.printf("NTP Nixie Tube Clock");
- M5.Lcd.setCursor(0, 14);
- M5.Lcd.printf("DATE");
- PutNum(0, 36, 52, 2, timeinfo.tm_mon + 1); //月の表示
- PutNum(108, 36, 52, 2, timeinfo.tm_mday); //日の表示
- PutJpg(216, 63, timeinfo.tm_wday + 10); //曜日の表示
- M5.Lcd.setCursor(0, 136);
- M5.Lcd.printf("TIME");
- PutNum(0, 156, 52, 2, timeinfo.tm_hour); //時の表示
- PutNum(108, 156, 52, 2, timeinfo.tm_min); //分の表示
- PutNum(216, 156, 52, 2, timeinfo.tm_sec); //秒の表示
- }
- delay(100); //0.1秒ウェイト
- }
|