Skip to content

Improve MQTT and WiFi resilience - #84

Merged
floatplane merged 1 commit into
floatplane:mainfrom
Allram:improve-mqtt-wifi-resilience
Jul 27, 2026
Merged

Improve MQTT and WiFi resilience#84
floatplane merged 1 commit into
floatplane:mainfrom
Allram:improve-mqtt-wifi-resilience

Conversation

@Allram

@Allram Allram commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

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_SIZE is 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 via beginPublish()), but regular publishes need the buffer.

Reconnect to MQTT with exponential backoff. mqttConnect() previously looped over five blocking connect() attempts, and was re-invoked every second (MQTT_RETRY_INTERVAL_MS). Each connect() 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 in loop() ("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 sets setAutoReconnect(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 and WiFi.setSleep(false) on ESP32.

Mask all password fields in logConfig(). Only ap_pwd was masked; mqtt_pwd, login_password, and ota_pwd were logged in cleartext — and with MQTT logging enabled they end up on the debug topic at the broker.

Testing

  • pio test -e test-native passes
  • pio run -e WEMOS_D1_Mini builds clean; src/ is clang-format-17 clean
  • Flashed on a WEMOS D1 Mini connected to a live Mitsubishi unit — connects to WiFi/MQTT and controls the unit as expected

🤖 Generated with Claude Code

- 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 floatplane left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@floatplane
floatplane merged commit 9f6b7d2 into floatplane:main Jul 27, 2026
14 checks passed
@Allram

Allram commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator Author

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 👌

@floatplane

Copy link
Copy Markdown
Owner

@Allram that's so cool, glad it's working for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants