//sntp_sync_notification.c #include //#include "time.h" // 削除 #include // 追加 for struct timeval #include // 追加 for sntp_sync_status const char* ssid = "xxxxxx"; // 自宅のWiFi UserID を設定 const char* password = "xxxxxx"; // 自宅のWiFi Password を設定 const char* ntpServer1 = "ntp.nict.jp"; const char* ntpServer2 = "time.google.com"; const char* ntpServer3 = "ntp.jst.mfeed.ad.jp"; const long gmtOffset_sec = 9 * 3600; const int daylightOffset_sec = 0; int itimeout = 1000; //Wait WiFi Connection void printLocalTime() // CPU 内蔵の RTC の時刻表示 { struct tm timeinfo; if(!getLocalTime(&timeinfo)){ // CPU 内蔵の RTC の時刻を取得 Serial.println("Failed to obtain time"); return; } Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");// RTC の時刻表示 // 17:32:27.842 -> Monday, January 08 2024 17:32:27 } // Callback function (get's called when time adjusts via NTP) void timeavailable(struct timeval *t) { Serial.println("Got time adjustment from NTP!"); } void setup() { Serial.begin(115200); //connect to WiFi Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED ) { itimeout--; if(itimeout < 1) WIFI_REASON_TDLS_PEER_UNREACHABLE; else { Serial.print("."); delay(500); } } Serial.println(" CONNECTED"); //init and get the time sntp_set_time_sync_notification_cb( timeavailable ); configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2, ntpServer3);// CPU 内蔵の RTC に時刻を設定 printLocalTime();// 時刻表示 //disconnect WiFi as it's no longer needed WiFi.disconnect(true);// NTP の参照は完了、WiFi 停止 WiFi.mode(WIFI_OFF); // WiFi切断 } void loop() { delay(1000); printLocalTime();// 時刻表示 }