I’ve been working on a remote control for my cargobot, more on those little teasers soon, and I’ve been having an annoying problem with something that should’ve been simple.
The Problem?
Switching it on.
The controller is running on a Sparkfun ESP32 Thing Plus which has an ESP32-S2 at it’s core. It has an EN (enable) pin that if tied to ground, should put the chip in deep sleep and it does this a treat. I expected that when it’s switched back on, however, that it would carry on as normal. When I switch it back on, lights come back on, as does the GNSS receiver, the Wii Nunchuck breakout, and the serial port. It’s definitely running, except for the screen.
I’m not quite sure what to do to fix this so the screen come back on, I have discovered a nuclear workaround though.
The ESP32 has lots of cool features, one of them is the watchdog timer. Once set, this listens for a command to reset the timer, if it isn’t reset within a certain period it will reboot the ESP32, assuming that something has gone wrong.
The Fix/Workaround
Relatively simple in the end, but a bit of trial and error to get it working for me.
Add this import:#include <esp_task_wdt.h>
This definition:#define WDT_TIMEOUT 5
Add this to setup(), somewhere near the top seems more reliable:esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts<br>esp_task_wdt_add(NULL); //add current thread to WDT watch
Finally, add this to loop():esp_task_wdt_reset();
What this does is create a watchdog timer with a timeout of 5 seconds, ties it to the main thread, and if the reset function isn’t called within the timeout resets the board. One thing to note, if you’re using more than one of the cores on the ESP32, you’ll need to call esp_task_wdt_add
on the appropriate thread.
It’s not ideal for my use case as it means there’s a delay waking from sleep. It does mean that I can charge the remote without it being switched on though as the battery and charge circuit are connected to the Thing Plus board.
Hope this helps someone, if not future me, and stay tuned for more info in what I’ve been up to over the last, erm, six months…