maintenance mode (improved error handling)

This commit is contained in:
cyberman54 2021-03-12 21:23:31 +01:00
parent e54c386e9e
commit da4fcc7ef1

View File

@ -11,6 +11,7 @@ void start_boot_menu(void) {
uint8_t mac[6]; uint8_t mac[6];
char clientId[20]; char clientId[20];
unsigned long timeout = millis();
// hash 6 byte MAC to 4 byte hash // hash 6 byte MAC to 4 byte hash
esp_eth_get_mac(mac); esp_eth_get_mac(mac);
@ -21,13 +22,15 @@ void start_boot_menu(void) {
const char *ssid = WIFI_SSID; const char *ssid = WIFI_SSID;
const char *password = WIFI_PASS; const char *password = WIFI_PASS;
// set runmode normal makes watchdog booting to production if triggered
RTC_runmode = RUNMODE_NORMAL; RTC_runmode = RUNMODE_NORMAL;
// setup watchdog, based on esp32 timer2 interrupt
hw_timer_t *timer = NULL; hw_timer_t *timer = NULL;
timer = timerBegin(2, 80, true); // timer 2, div 80, countup timer = timerBegin(2, 80, true); // timer 2, div 80, countup
timerAttachInterrupt(timer, &esp_restart, true); // callback device reset timerAttachInterrupt(timer, &esp_restart, true); // callback for device reset
timerAlarmWrite(timer, BOOTDELAY * 1000000, false); // set time in us timerAlarmWrite(timer, BOOTTIMEOUT * 1000000, false); // set time in us
timerAlarmEnable(timer); // enable interrupt timerAlarmEnable(timer); // enable watchdog
WebServer server(80); WebServer server(80);
@ -102,86 +105,91 @@ void start_boot_menu(void) {
// Connect to WiFi network // Connect to WiFi network
// workaround applied here to avoid WIFI_AUTH failure // workaround applied here to avoid WIFI_AUTH failure
// see https://github.com/espressif/arduino-esp32/issues/2501 // see https://github.com/espressif/arduino-esp32/issues/2501
// 1st try // 1st try
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.status() == WL_DISCONNECTED) { while (WiFi.status() == WL_DISCONNECTED) {
delay(500); if ((long)(millis() - timeout) > (BOOTDELAY * 1000))
esp_restart();
else
delay(500);
} }
// 2nd try // 2nd try
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); if ((long)(millis() - timeout) > (BOOTDELAY * 1000))
esp_restart();
else
delay(500);
} }
} }
MDNS.begin(host); MDNS.begin(host);
timerWrite(timer, 0); // reset timer (feed watchdog)
server.on("/", HTTP_GET, [&server, &loginMenu]() { server.on("/", HTTP_GET, [&server, &loginMenu]() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "keep-alive");
server.send(200, "text/html", loginMenu); server.send(200, "text/html", loginMenu);
}); });
server.on("/serverIndex", HTTP_GET, [&server, &serverIndex, &timer]() { server.on("/serverIndex", HTTP_GET, [&server, &serverIndex, &timer]() {
timerAlarmWrite(timer, BOOTTIMEOUT * 1000000, false); timerWrite(timer, 0); // reset timer (feed watchdog)
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "keep-alive");
server.send(200, "text/html", serverIndex); server.send(200, "text/html", serverIndex);
}); });
server.onNotFound([&server, &loginMenu]() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", loginMenu);
});
// handling uploading firmware file
server.on( server.on(
"/update", HTTP_POST, "/update", HTTP_POST,
[&server]() { [&server]() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
WiFi.disconnect(true); WiFi.disconnect(true);
if (!Update.hasError())
RTC_runmode = RUNMODE_POWERCYCLE;
esp_restart(); esp_restart();
}, },
// handling uploading firmware file
[&server, &timer]() { [&server, &timer]() {
bool success = false; bool success = false;
HTTPUpload &upload = server.upload(); HTTPUpload &upload = server.upload();
switch (upload.status) { // did we get a file name?
if (upload.filename != NULL) {
case UPLOAD_FILE_START: switch (upload.status) {
// start file transfer
ESP_LOGI(TAG, "Uploading %s", upload.filename.c_str());
success = Update.begin();
break;
case UPLOAD_FILE_WRITE: case UPLOAD_FILE_START:
// flashing firmware to ESP // start file transfer
success = (Update.write(upload.buf, upload.currentSize) == ESP_LOGI(TAG, "Uploading %s", upload.filename.c_str());
upload.currentSize); success = Update.begin();
break; break;
case UPLOAD_FILE_END: case UPLOAD_FILE_WRITE:
success = Update.end(true); // true to set the size to the current // flashing firmware to ESP
if (success) success = (Update.write(upload.buf, upload.currentSize) ==
ESP_LOGI(TAG, "Upload finished, %u bytes written", upload.currentSize);
upload.totalSize); break;
else
ESP_LOGE(TAG, "Upload failed, status=%d", upload.status);
break;
case UPLOAD_FILE_ABORTED: case UPLOAD_FILE_END:
default: success = Update.end(true); // true to set the size to the current
break; if (success)
ESP_LOGI(TAG, "Upload finished, %u bytes written",
upload.totalSize);
else
ESP_LOGE(TAG, "Upload failed, status=%d", upload.status);
break;
} // switch case UPLOAD_FILE_ABORTED:
default:
break;
if (!success) { } // switch
ESP_LOGE(TAG, "Error: %s", Update.errorString());
WiFi.disconnect(true); // don't boot to production if update failed
esp_restart(); if (!success) {
ESP_LOGE(TAG, "Error: %s", Update.errorString());
RTC_runmode = RUNMODE_POWERCYCLE;
}
} }
}); });
@ -194,6 +202,6 @@ void start_boot_menu(void) {
while (1) { while (1) {
server.handleClient(); server.handleClient();
delay(1); delay(2);
} }
} }