//sntp_sync_ESP32_oled - (3) ; update: 2024/01/11 13:10 // #include #include // for struct timeval #include // for sntp_sync_status #include #include #include #include //漢字フォント用 #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // 使用しないので -1を設定する。 #define SCREEN_ADDRESS 0x3C // I2Cアドレスは 0x3C #define OLED_SCL 22 // I2C0で使用可能 SCLのGPは 22 #define OLED_SDA 21 // I2C0で使用可能 SDAのGPは 21 const char* ssid = "ax50anotherZ"; const char* password = "rock#tang@50Gpgw"; 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; const int ctimeout = 5; //Wait WiFi Connection static int itimeout = ctimeout; static int lastSec = -1; static int lastHour = -1; static int hourInit = -1; static int year = 0; static int wifidisable = 1; //WiFi 接続有効/無効(1:OK, -1:NG) //static int nextday = -1; // 曜日表示文字 static char *weekChar[7] = { "日", "月", "火", "水", "木", "金", "土" }; uint8_t font[8]; // フォント格納バッファ Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //表示制御にはAdafruit製 SSD1306を使用する。 //初期化時には I2C0を使用する (Wire) void printLocalTime() { struct tm timeinfo; static char buf[20]; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); startwifi(); //connect to WiFi } else { year = timeinfo.tm_year; // if (nextday != timeinfo.tm_mday) { // 1 日経過した // nextday = timeinfo.tm_mday; // wifidisable = 1; //WiFi 接続有効 // } if (lastHour != timeinfo.tm_hour) { // 1 時間経過した lastHour = timeinfo.tm_hour; if(hourInit != -1) { Serial.print("1 hour has passed"); //connect to WiFi 再度時間取得 if(wifidisable == 1) { //WiFi 接続有効 Serial.println(", Connect to NTP Server!"); startwifi(); } else { Serial.println(", startwifi() By Pass"); Serial.println(""); } } else hourInit = 0; } } year = timeinfo.tm_year; // 時刻が変わっているか判定 if (lastSec != timeinfo.tm_sec ) { lastSec = timeinfo.tm_sec; // 時刻が変わった時に描画する oled.clearDisplay(); //何か表示されている場合に備えて表示クリア // 時分秒の描画 sprintf(buf, "%2d", timeinfo.tm_hour); oled.setCursor(0, 46); //テキストの表示開始位置 0,46 oled.print(buf); //表示文字列 sprintf(buf, "%02d", timeinfo.tm_min); oled.setCursor(45, 46); //テキストの表示開始位置 45,46 oled.print(buf); //表示文字列 sprintf(buf, "%02d", timeinfo.tm_sec); oled.setCursor(88, 46); //テキストの表示開始位置 88,46 oled.print(buf); //表示文字列 // 年月日の描画 sprintf(buf, "%4d", timeinfo.tm_year + 1900); oled.setCursor(0, 2); //テキストの表示開始位置 0,2 oled.print(buf); //表示文字列 sprintf(buf, "%2d", timeinfo.tm_mon + 1); oled.setCursor(0, 24); //テキストの表示開始位置 0,24 oled.print(buf); //表示文字列 sprintf(buf, "%2d", timeinfo.tm_mday); oled.setCursor(46, 24); //テキストの表示開始位置 46,24 oled.print(buf); //表示文字列 // 曜日()の描画 getFontData(font, "("); oled.drawBitmap(96, 30, font,8,8,1); getFontData(font, ")"); oled.drawBitmap(116, 30, font,8,8,1); // 年月日時分秒の描画 // 日本語フォントをセット getFontData(font, "時"); oled.drawBitmap(28, 52, font,8,8,1); getFontData(font, "分"); oled.drawBitmap(72, 52, font,8,8,1); getFontData(font, "秒"); oled.drawBitmap(113, 52, font,8,8,1); getFontData(font, "年"); oled.drawBitmap(54, 8, font,8,8,1); getFontData(font, "月"); oled.drawBitmap(28, 30, font,8,8,1); getFontData(font, "日"); oled.drawBitmap(72, 30, font,8,8,1); getFontData(font, weekChar[timeinfo.tm_wday]); // 曜日表示文字 oled.drawBitmap(104, 30, font,8,8,1); /// oled.setRotation(2); //デフォルト位置から、180度回転 oled.display(); //バッファ転送(表示) } else { // Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); // 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); /// Wire.begin();// i2c_scanner /// Wire.setClock(400000); //I2C0で使用するGPは SDA = 0, SCL = 1 /// Wire.setSDA(OLED_SDA);//GP21 /// Wire.setSCL(OLED_SCL);//GP22 if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(SSD1306_WHITE); // text color # 色指定はできないが必要 oled.setCursor(0, 10); // position to display } void startwifi() { itimeout = ctimeout; //connect to WiFi Serial.printf("Connecting to %s ", ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { itimeout--; if(itimeout < 1) { wifidisable = -1; // WIFI_REASON_TDLS_PEER_UNREACHABLE; break; } else { Serial.print("."); delay(500); wifidisable = 1; } } Serial.println("\n"); if(wifidisable == 1) { Serial.println("has Connected "); //init and get the time sntp_set_time_sync_notification_cb( timeavailable ); Serial.println(" Setting _ _ _ RTC Time "); configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2, ntpServer3); } else Serial.println("WiFi is Down "); if(year != 70) { //disconnect WiFi as it's no longer needed delay(500); // wait for WiFi.disconnect(true); WiFi.mode(WIFI_OFF); Serial.println("WiFi_Off\n"); } } void loop() { printLocalTime(); }