Bugfix/workaround for an issue with reboots when WiFi disconnects #298
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
These are changes I made in effort to eliminate controller crashes when WiFi goes down and back up in a while.
I managed to replicate the issue described in #276. I disabled WiFi on my router for ~5 secs and then enabled it again. My router is MikroTik cAP ac. diyBMS config: no TFT, MQTT enabled, no influx and no HA export.
The stack trace suggests that the ESP tried to dereference null pointer - already freed memory, somewhere outside the diyBMS application. I noticed the
ShutdownAllNetworkServices()
call when WiFi is disconnected. Perhaps there is some kind of race condition when the resources are being freed and in the same time the ESP manages to connect back to the WiFi quickly and tries to send an event to already non-existing instance.Anyway, I removed the
ShutdownAllNetworkServices()
call and make sure that the network services like HTTPD, mDNS and MQTT are started just once. I think there is no need to stop the services upon disconnect (and even when IP addr has changed), they will continue operating once the WiFi is back (for instance, the mqtt client has reconnect_timeout_ms option and the auto reconnect is enabled by default. HTTP for web server is client-initiated, so that should be good too).I run some tests on a firmware with the proposed changes and was unable to replicate the issue again. Short WiFi dropouts, 1h disconnect... it hadn't crashed and once the WiFi was enabled, it got connected.