esp_clock: disable WiFi for 1 hour

This commit is contained in:
Nicu Hodos 2020-05-13 21:28:28 +02:00
parent 611124d166
commit b1952bebe4

View File

@ -39,6 +39,8 @@ byte displayHours[24] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4,
bool blinkColon = false; // Track colon status to blink once/sec
int status = WL_IDLE_STATUS;
bool shouldUpdate = true;
String currentSSID;
String currentPsk;
ESP8266WiFiMulti wifiMulti;
WiFiUDP ntpUDP;
@ -55,6 +57,8 @@ void setup() {
delay(500);
}
Serial.println("Connected to network.");
currentSSID = WiFi.SSID();
currentPsk = WiFi.psk();
printWiFiStatus(); // Display WiFi status data
setupOTA();
@ -74,6 +78,7 @@ void loop() {
minutes = timeClient.getMinutes();
seconds = timeClient.getSeconds();
Serial.println(timeClient.getFormattedTime());
WiFi.forceSleepBegin();
} else {
incrementTime();
}
@ -82,14 +87,28 @@ void loop() {
}
void incrementTime() {
if (++seconds == 60) {
if (++seconds >= 60) {
seconds = 0;
if (++minutes == 60) {
minutes = 0;
Serial.println("Minutes set to zero - should query NTP on next loop()");
if (++hours == 24) hours = 0;
}
if (((minutes % 5) == 0)) shouldUpdate = true;
if ((minutes == 0)) {
WiFi.forceSleepWake();
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
for (int i = 0; i < 4; i++) {
Serial.println("Reconnecting to WiFi netowrk...");
delay(1000);
seconds++;
}
if (WiFi.status() == WL_CONNECTED) {
shouldUpdate = true;
} else {
WiFi.forceSleepBegin();
}
printWiFiStatus();
}
}
}
@ -105,6 +124,9 @@ void displayTime() {
// which can look confusing. Go in and explicitly add these zeros.
if (displayHour == 0) {
clockDisplay.writeDigitNum(1, 0);
if (minutes < 10) {
clockDisplay.writeDigitNum(3, 0);
}
}
// Blink the colon by flipping its value every loop iteration
@ -117,41 +139,27 @@ void displayTime() {
}
void setupOTA() {
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void printWiFiStatus() {