diff --git a/M5StickCPlus_FactoryTest2022.ino b/M5StickCPlus_FactoryTest2022.ino index 59ad858..499716a 100644 --- a/M5StickCPlus_FactoryTest2022.ino +++ b/M5StickCPlus_FactoryTest2022.ino @@ -554,26 +554,44 @@ RTC_TimeTypeDef time; M5.Rtc.GetTime(&time); - Disbuff.setTextSize(4); - Disbuff.setCursor(6, 25); - Disbuff.setTextColor(TFT_WHITE); + // Disbuff.setTextSize(4); + // Disbuff.setCursor(6, 25); + // Disbuff.setTextColor(TFT_GREEN); while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) { Disbuff.fillRect(0, 0, 240, 135, Disbuff.color565(0, 0, 0)); M5.Rtc.GetTime(&time); Disbuff.setTextSize(4); - Disbuff.setTextColor(TFT_WHITE); + Disbuff.setTextColor(TFT_GREENYELLOW); Disbuff.setCursor(25, 50); Disbuff.printf("%02d:%02d:%02d", time.Hours, time.Minutes, time.Seconds); - Disbuff.fillRect(0, 0, 240, 25, Disbuff.color565(20, 20, 20)); + Disbuff.fillRect(0, 0, 240, 35, Disbuff.color565(20, 20, 20)); Disbuff.setTextSize(2); - Disbuff.drawString("BMP8563 RTC Time", 26, 5, 1); + Disbuff.setTextColor(TFT_WHITE); + Disbuff.drawString("BMP8563 RTC Time", 26, 15, 1); + Disbuff.setTextSize(2); + Disbuff.setCursor(6, 90); + Disbuff.setTextColor(TFT_YELLOW); + Disbuff.println("Press B to sync RTC from NTP"); Displaybuff(); M5.update(); checkAXPPress(); delay(100); } + if (M5.BtnB.isPressed()) + { + // RTC sync from NTP + // Wifi → NTP → update RTC → WifiOff + wifi_setup(); + ntp_setup(); + wifi_down(); + + Init_ESPNOW(); // ESPNOWの初期化 + + M5.update(); + DisplayRTC(); // あまりよろしくないが、やっぱり時刻同期を確認したいので + } while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) { M5.update(); diff --git a/wifi.ino b/wifi.ino new file mode 100644 index 0000000..9ff2a70 --- /dev/null +++ b/wifi.ino @@ -0,0 +1,84 @@ +#include +#include + +const char *ssid = "miura2g"; +const char *password = "jikken2022"; +const char *ntpserver = "ntp.nict.jp"; + +void wifi_setup() +{ + M5.Lcd.fillScreen(ORANGE); + M5.Lcd.setCursor(10, 50, 4); + + WiFi.begin(ssid, password); // 接続開始 + while (WiFi.status() != WL_CONNECTED) + { // 接続中... + M5.Beep.tone(2000); + delay(200); + M5.Beep.mute(); + delay(300); + M5.Lcd.print("."); + } + // 接続完了!! + M5.Beep.tone(4000); + M5.Lcd.fillScreen(GREEN); + M5.Lcd.setCursor(0, 8, 4); + M5.Lcd.setTextColor(BLACK, GREEN); + M5.Lcd.print(" Wifi Connected!\n "); + String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス + M5.Lcd.println(gotip); + + // MACアドレスを表示 + uint8_t mac[6]; + char macchar[100]; + esp_read_mac(mac, ESP_MAC_WIFI_STA); + sprintf(macchar, " %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + String macstr = macchar; + M5.Lcd.println(macstr); + + delay(1500); + M5.Beep.mute(); +} +void wifi_down() +{ + WiFi.disconnect(); + WiFi.mode(WIFI_OFF); +} + +struct tm localTime; +void ntp_setup() +{ + configTime(9 * 3600, 0, ntpserver); + + delay(1000); + + while (localTime.tm_year < 80) { + getLocalTime(&localTime); + M5.Beep.tone(1000); + delay(200); + M5.Beep.mute(); + delay(300); + M5.Lcd.print("."); + } + M5.Beep.tone(2000); + + setRTCfromLT(localTime); +} + +void setRTCfromLT(struct tm lt) { + RTC_DateTypeDef DateStruct; + DateStruct.Year = lt.tm_year + 1900; + DateStruct.Month = lt.tm_mon + 1; + DateStruct.Date = lt.tm_mday; + DateStruct.WeekDay = lt.tm_wday; + M5.Rtc.SetData(&DateStruct); + + RTC_TimeTypeDef TimeStruct; + TimeStruct.Hours = lt.tm_hour; + TimeStruct.Minutes = lt.tm_min; + TimeStruct.Seconds = lt.tm_sec+1; + M5.Rtc.SetTime(&TimeStruct); + M5.Lcd.print(" RTC updated!\n "); + delay(1500); + M5.Beep.mute(); +} \ No newline at end of file