// /0978/ESP32_max7219_clock_Double/ESP32_max7219_clock_Double.ino ; update: 2024/02/16 18:31 //ESP32 Dev Module // #include #include // **** #include // for struct timeval #include // for sntp_sync_status #include "RTClib.h" #include #include #include //#include //#include //For Time Based Application #include "GF4x8p.h" // 4x8 font #include "GF3x5p.h" // 3x5 font //GF3x8p #include "GF5x8p.h" // 5x8 font //Wi-Fi #define SSID "ax50anotherZ" #define WIFIKEY "rock#tang@50Gpgw" ///#define JST 3600 * 9 //tzOffset ///#define tzOffset JST //32400 = 3600 * 9 #define TIMEZONE "JST-9" #define DSPZONE0 0 // #define DSPZONE1 1 // #define DSPZONE2 2 // #define RTC_SCL 22 // Wire SCLのGPIOは 22 #define RTC_SDA 21 // Wire SDAのGPIOは 21 //MAX7219 #define MAX_DEVICES 8 // eight modules #define CLK_PIN 27 #define DATA_PIN 12 #define CS_PIN 14 //#define PA_CENTER 1 #define SPEED_TIME 25 //Small numbers are faster. Zero is the fastest. #define SPEED_TIMESLOW 70 //Small numbers are faster. Zero is the fastest. #define HARDWARE_TYPE MD_MAX72XX::FC16_HW #define PAUSE_TIME_L 1200 //1200ms for month/day long pause #define PAUSE_TIME_S 300 //300ms for month/day short pause const char* ntpServer1 = "ntp.nict.jp"; const char* ntpServer2 = "ntp.jst.mfeed.ad.jp"; const char* ntpServer3 = "time.google.com"; static struct tm timeinfo; static char buf[30], buf2[40]; //static char bufD[15], bufT[9]; // "yyyy mm/dd aaa" + 1 = 15, "hh:mm:ss" + 1 = 9 static const uint8_t intensity = MAX_INTENSITY/15;//Darkest //LEDの輝度を調整0-15、0=MAX static char bufS[3], bufT[7]; // bufS = Sec, 'ss' + 1 = 3, bufT = HourMinute, 'hhmm" + 1 = 5 static int wsec=-1; const int ctimeout = 5; //Wait WiFi Connection static int itimeout = ctimeout; static int wifienable = 1; //WiFi 接続有効/無効(1:OK, -1:NG) static char* wifistate="Alive";//"Alive" or "Down" static int timeset = -1;// 時間正常(-1:異常/1:正常) static int rtcenable = -1;// RTC状態(-1:異常/1:正常) static int fixed = -1;// set Time static int movetolocal = -1;// set Time to Local rtc // RTCオブジェクト RTC_DS1307 rtc; // Hardware SPI connection //MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES); MD_Parola mx = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES); // Callback function (get's called when time adjusts via NTP) void timeavailable(struct timeval *t) { Serial.println("Got time adjustment from NTP!"); } void getRTCtime() { DateTime now = rtc.now(); timeinfo.tm_year = now.year()-1900; timeinfo.tm_mon = now.month()-1; timeinfo.tm_mday = now.day(); timeinfo.tm_hour = now.hour(); timeinfo.tm_min = now.minute(); timeinfo.tm_sec = now.second(); // sprintf(buf, "set RTC{%04d %02d/%02d %02d:%02d:%02d}", // now.year(), now.month(), now.day(), // now.hour(), now.minute(), now.second() ); // timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday, // timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec+1); } void setup() { Serial.begin(115200);//115200 delay(1000); wifistate="????"; //WiFi Connection Setting WiFi.begin(SSID, WIFIKEY); while (WiFi.status() != WL_CONNECTED) { itimeout--; if(itimeout < 1) { wifienable = -1; // WIFI_REASON_TDLS_PEER_UNREACHABLE; wifistate="Down"; break; } else { wifistate="Alive"; Serial.print("."); delay(500); wifienable = 1; } } Serial.println("\n"); /// configTime( tzOffset, 0, ntpServer1, ntpServer2); if (!rtc.begin()) { Serial.println("123:Couldn't find RTC"); while(1) { Serial.print("_"); delay(500); }; rtcenable = -1; } else rtcenable = 1;//意図的にコメント if(wifienable == 1) { //init and get the time sntp_set_time_sync_notification_cb( timeavailable ); // NTPサーバを用いた時刻合わせ関数 configTzTime(TIMEZONE, ntpServer1, ntpServer2, ntpServer3); // esp32 api compatibility // 時刻待ち合せ while (!getLocalTime(&timeinfo)) { Serial.print("+"); delay(500); } Serial.println(" 141:Setting _ _ _ RTC Time "); delay(1000); if(fixed==-1 && timeinfo.tm_year+1900 > 2020 && rtc.now().year() < timeinfo.tm_year+1900) { rtc.adjust(DateTime(timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec)); rtcenable = 1; fixed = 1; Serial.println("151:set RTC"); } } else { Serial.println("155:WiFi is Down "); getRTCtime();// ################## Serial.println(buf); } Serial.println("160:Run setup"); if(timeinfo.tm_year != 70) { //disconnect WiFi as it's no longer needed delay(500); // wait for WiFi.disconnect(true, true); WiFi.mode(WIFI_OFF); Serial.println("166:WiFi_Off\n"); } // Matrix Display Setting mx.begin(3); // 3 zones mx.setZone(DSPZONE2, 4, 7); //00011111 <- Zone 2 for Year, month/day, weekday display mx.setZone(DSPZONE1, 1, 3); //00011111 <- Zone 1 for HM display mx.setZone(DSPZONE0, 0, 0); //11100000 <- Zone 0 for Sec display /// mx.setFont(DSPZONE2,GF4x8p);// **** mx.setFont(DSPZONE2,GF3x5p); mx.setFont(DSPZONE1,GF4x8p);//GF5x8p); mx.setFont(DSPZONE0,GF3x5p);//GF3x8p mx.setIntensity(intensity); //Darkest } void loop() { if(wifienable == 1) { if(fixed==-1 && timeinfo.tm_year+1900 > 2020 && rtc.now().year() < timeinfo.tm_year+1900) { rtc.adjust(DateTime(timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec)); rtcenable = 1; fixed = 1; Serial.println("187:set RTC"); } time_t epTime = time(NULL); ///timeClient.getEpochTime(); timeinfo = *localtime(&epTime); } else { getRTCtime();// ################## Serial.println(buf); } mx.displayAnimate(); //Zone 2 Date // if (mx.getZoneStatus(DSPZONE2)) { // strftime(buf2, sizeof(buf2), " %Y %m/%d %A", &timeinfo); // mx.displayZoneText(DSPZONE2, buf2, PA_LEFT, SPEED_TIMESLOW, 0, PA_SCROLL_LEFT, PA_NO_EFFECT); // mx.setPause(DSPZONE2, PAUSE_TIME_L); // } if (mx.getZoneStatus(DSPZONE2)) { strftime(buf2, sizeof(buf2), " %y.%m.%d", &timeinfo); mx.displayZoneText(DSPZONE2, buf2, PA_LEFT, SPEED_TIMESLOW, 0, PA_PRINT, PA_NO_EFFECT); mx.setPause(DSPZONE2); } //Zone 1 Time // if (mx.getZoneStatus(0) || mx.getZoneStatus(1)) { if (mx.getZoneStatus(DSPZONE1)) { strftime(bufT, sizeof(bufT), "%H:%M:", &timeinfo); // %T: hh:mm:ss mx.displayZoneText(DSPZONE1, bufT, PA_RIGHT, SPEED_TIME, 0, PA_PRINT, PA_NO_EFFECT); mx.displayReset(DSPZONE1); } //Zone 0 Time if (mx.getZoneStatus(DSPZONE0)) { strftime(bufS, sizeof(bufS), "%S", &timeinfo); // %T: hh:mm:ss mx.displayZoneText(DSPZONE0, bufS, PA_RIGHT, SPEED_TIME, 0, PA_PRINT, PA_NO_EFFECT); mx.displayReset(DSPZONE0); } delay(50); //Don't remove this delay, and don't make it too small }