I recently set up my ESP32 with 4 relays using the instructions and code from the 4_4_Multiple_Web_Pages section of the book Build Web Servers ESP32. The instructions were great and my ESP32 and relays work as described. The only issue I have is when the ESP32 restarts after a power outage, the initial state of all the outputs is on. That could be a problem if equipment or lights start without my knowledge. Would you please suggest ways to adjust the code to put all the outputs to the off state upon startup of the ESP32. Thank you very much.
In the setup section, after the pin mode statements, set all outputs to off. Sorry, I don’t have that particular book so I don’t have the code to look at.
Hi Thomas.
Alternatively, you may want to save the GPIOs state permanently whenever there is a change.
If you want to store small amounts of data, you can use the Preferences library.
We’ve just published a tutorial about that today: https://randomnerdtutorials.com/esp32-save-data-permanently-preferences/
In this tutorial, there’s a simple example showing how to “remember” the last GPIO state after a reset.
Alternatively, you can create a file in SPIFFS where you save a JSON variable with the GPIOs states whenever there’s a change. Then, you need to add some code at the beginning to read that file and set all GPIOs to their previous states.
I hope this helps.
Regards,
Sara
Thank you very much. Both suggestion were good. I solved my own problem last night by adding code in the setup to to set the output pins to the proper state like that suggested by Steve.
Thank you very much. Both suggestion were good. I solved my own problem last night by adding code in the setup to to set the output pins to the proper state like that suggested by Steve.