Use the Watchdog timer to force a proper hard restart

This commit is contained in:
Owen 2020-10-29 08:51:24 +01:00
parent 99e957b3f9
commit 22c834e796
2 changed files with 17 additions and 5 deletions

View File

@ -29,6 +29,7 @@
// Functions from the main .ino // Functions from the main .ino
extern void flashLED(int flashtime); extern void flashLED(int flashtime);
extern void setLamp(int newVal); extern void setLamp(int newVal);
extern void hard_restart();
// External variables declared in the main .ino // External variables declared in the main .ino
extern char myName[]; extern char myName[];
@ -629,7 +630,7 @@ static esp_err_t cmd_handler(httpd_req_t *req){
Serial.print('.'); Serial.print('.');
} }
Serial.printf(" Thats all folks!\n\n"); Serial.printf(" Thats all folks!\n\n");
ESP.restart(); hard_restart();
} }
else { else {
res = -1; res = -1;

View File

@ -1,4 +1,6 @@
#include "esp_camera.h" #include "esp_camera.h"
#include <esp_int_wdt.h>
#include <esp_task_wdt.h>
#include <WiFi.h> #include <WiFi.h>
#include <DNSServer.h> #include <DNSServer.h>
#include "src/parsebytes.h" #include "src/parsebytes.h"
@ -187,6 +189,15 @@ void flashLED(int flashtime) {
#endif #endif
} }
// ESP.restart() is very 'soft', it can leave the camera hardware
// improperly initialised; resulting in boot loops.
// This replacement uses a watchdog to force a hard restart after 1 second.
void hard_restart() {
esp_task_wdt_init(1,true);
esp_task_wdt_add(NULL);
while(true);
}
// Lamp Control // Lamp Control
void setLamp(int newVal) { void setLamp(int newVal) {
if (newVal != -1) { if (newVal != -1) {
@ -448,9 +459,9 @@ void setup() {
} else { } else {
delay(100); // need a delay here or the next serial o/p gets missed delay(100); // need a delay here or the next serial o/p gets missed
Serial.println("Halted: Camera sensor failed to initialise"); Serial.println("Halted: Camera sensor failed to initialise");
Serial.println("Will reboot to try again in 10s\n"); Serial.println("Will reboot to try again in 5s\n");
delay(10000); delay(4000);
ESP.restart(); hard_restart();
} }
sensor_t * s = esp_camera_sensor_get(); sensor_t * s = esp_camera_sensor_get();