I have done a bit more digging and it transpires that the saving of the changed value isn’t working. So, it isn’t the re-boot that is causing the problem, for now anyway.
Hi, thank you for your reply. Yes, it does work on the ESP32CAM. I didn’t think my dependencies would be relevant, so didn’t include them. They are:
If the example works, it proves that it is not due to the ESP32 Cam module. The error is probably in your code which you have not shown completely.
Please show a complete minimal example (including the platformio.ini) which makes the error reproducible.
What strikes me: You open Preferences first as read-only and later again as writable. I have never seen this before and have never used it that way myself.
Simply call prefs.begin("store"); once in setup() and don’t call prefs.end() (except you know the ESP32 will restart soon).
The complete platformio.ini is already posted. The code is essentially complete, in that the attempt to read is in setup() and I write after receiving the new value in an MQTT message.
I’ve tried:
Opening the preferences read-write at the outset and leaving it open afterwards.
Using this instance to write the changed value.
I call preferences.end() immediately after writing, as in the example.
I initiate the re-start (using ESP.restart() and it still fails to load the new value.
I tried that but no difference - or putting a delay between the putInt() and close().
Replacing my setup() completely with just the minimal code to write/read the preference revealed an error message saying my key was too long! It was originally 18 characters. Reducing it to three means my original code works!
Armed with this info, I found that there is a limit of 15 characters, but it is documented under NVS rather than Preferences.