Improve MQTT and WiFi resilience - #84
Conversation
- Grow the MQTT buffer from PubSubClient's 256-byte default, which the state payload could silently overflow with a long root topic or friendly name - Reconnect to MQTT with a single attempt per call and exponential backoff (mirroring the heat pump retry scheme) instead of five blocking attempts every second, which starved the heat pump sync and web server while the broker was down - Nudge WiFi to reconnect every 30s after a drop, keeping the reboot as a last resort after 5 minutes (resolves the TODO in loop()) - Disable WiFi modem power saving for fewer latency spikes and drops - Mask all password fields when logging config files, not just ap_pwd Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
floatplane
left a comment
There was a problem hiding this comment.
Thank you @Allram! Waiting for CI to complete and then will merge
I am not actively using this project anymore (moved house and now have Daikin units), so I'll add you as a collaborator so you can make changes without friction. Happy to transfer the project to you as well if you'd prefer!
Thanks for adding me as a collaborator. I run my own private fork with ota updates etc for friends, just wanted to add some changes upstream 👌 |
|
@Allram that's so cool, glad it's working for you! |
This PR bundles a few small resilience improvements for running MitsuQTT as a long-lived MQTT bridge. Happy to split it into separate PRs if you'd prefer.
Changes
Grow the MQTT buffer to 1024 bytes. PubSubClient's default
MQTT_MAX_PACKET_SIZEis 256 bytes, which the state topic payload (9 fields, ~180–200 bytes serialized) plus topic name sits right up against. With a longer root topic or friendly name,publish()starts returning false and state updates silently stop. Discovery messages were already safe (streamed viabeginPublish()), but regular publishes need the buffer.Reconnect to MQTT with exponential backoff.
mqttConnect()previously looped over five blockingconnect()attempts, and was re-invoked every second (MQTT_RETRY_INTERVAL_MS). Eachconnect()against an unreachable broker blocks for seconds, so while the broker was down the main loop was effectively starved — heat pump sync stalled and the web UI became unresponsive. It now makes a single attempt per call, and the caller retries with the same exponential backoff scheme already used for the heat pump connection (1s doubling to a max of ~4 minutes). The manual reconnect button on the status page resets the backoff so it still connects immediately.Try
WiFi.reconnect()before rebooting. Addresses the existing TODO inloop()("do we really need to reboot here?"): on connection loss the firmware now nudges the WiFi stack every 30 seconds, and keeps the reboot as a last resort after the existing 5-minute deadline. Also setssetAutoReconnect(true)explicitly.Disable modem power saving. The ESP8266's default modem sleep causes latency spikes and dropped packets with some access points (UniFi APs are a common report). The device is always mains-powered, so the ~tens of mW saved aren't worth it. Uses
WiFi.setSleepMode(WIFI_NONE_SLEEP)on ESP8266 andWiFi.setSleep(false)on ESP32.Mask all password fields in
logConfig(). Onlyap_pwdwas masked;mqtt_pwd,login_password, andota_pwdwere logged in cleartext — and with MQTT logging enabled they end up on the debug topic at the broker.Testing
pio test -e test-nativepassespio run -e WEMOS_D1_Minibuilds clean;src/is clang-format-17 clean🤖 Generated with Claude Code