Skip to content

Fix: MQTT topic not updated when LoRa region changes#10565

Open
Copilot wants to merge 4 commits into
developfrom
copilot/bug-fix-lora-region-mqtt-topic
Open

Fix: MQTT topic not updated when LoRa region changes#10565
Copilot wants to merge 4 commits into
developfrom
copilot/bug-fix-lora-region-mqtt-topic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 27, 2026

When MQTT is active and the LoRa region is changed, the running MQTT instance continues publishing and subscribing under the old region's topic prefix (e.g. msh/US/… after switching away from US), because moduleConfig.mqtt.root was updated but the topic strings cached in the MQTT object were not.

Changes

  • src/mqtt/MQTT.h / src/mqtt/MQTT.cpp — Introduce MQTT::reinitTopics():

    • Resets cryptTopic, jsonTopic, mapTopic to base values and prepends the current moduleConfig.mqtt.root
    • Updates isConfiguredForDefaultRootTopic
    • Disconnects from the broker so the next reconnect re-subscribes under the new prefix
    • Constructor now calls reinitTopics() instead of duplicating the same inline block
  • src/modules/AdminModule.cpp — Call mqtt->reinitTopics() immediately after moduleConfig.mqtt.root is rewritten on a region change (guarded by !MESHTASTIC_EXCLUDE_MQTT)

  • src/graphics/draw/MenuHandler.cpp — Call mqtt->reinitTopics() immediately after moduleConfig.mqtt.root is rewritten on a region change via the on-device menu (guarded by !MESHTASTIC_EXCLUDE_MQTT); also adds the required mqtt/MQTT.h include under the same guard

  • test/test_mqtt/MQTT.cpp — Add test_reinitTopicsUpdatesOnRegionChange: starts MQTT with root msh/US, simulates a region change to msh/EU_868 via reinitTopics(), then asserts both the refreshed subscriptions and the corrected publish topic

🤝 Attestations

  • I have tested that my proposed changes behave as described.
  • I have tested that my proposed changes do not cause any obvious regressions on the following devices:
    • Heltec (Lora32) V3
    • LilyGo T-Deck
    • LilyGo T-Beam
    • RAK WisBlock 4631
    • Seeed Studio T-1000E tracker card
    • Other (please specify below)

When the LoRa region is changed via AdminModule::handleSetConfig,
moduleConfig.mqtt.root is updated (e.g. from msh/US to msh/EU_868)
but the running MQTT instance kept using the stale topic strings
(cryptTopic / jsonTopic / mapTopic) that were set at construction time.

Introduce MQTT::reinitTopics() which:
- resets the topic strings to their base values and prepends the
  current moduleConfig.mqtt.root, and
- disconnects from the broker so the next reconnect re-subscribes
  under the new topic prefix.

Call reinitTopics() from MQTT's constructor (replacing the inline
block) so the logic lives in one place, and call it from
AdminModule::handleSetConfig right after moduleConfig.mqtt.root is
rewritten on a region change.

Add a unit test (test_reinitTopicsUpdatesOnRegionChange) that verifies
both the updated subscriptions and the updated publish topic after a
simulated region change.
Copilot AI changed the title [WIP] Fix MQTT topic update when changing LoRa region Fix: MQTT topic not updated when LoRa region changes May 27, 2026
Copilot AI requested a review from thebentern May 27, 2026 13:26
@jp-bennett
Copy link
Copy Markdown
Collaborator

@copilot this patch misses the case where the region is changed via menuhandler.cpp

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 27, 2026

@copilot this patch misses the case where the region is changed via menuhandler.cpp

Fixed in the latest commit — MenuHandler.cpp now calls mqtt->reinitTopics() (guarded by !MESHTASTIC_EXCLUDE_MQTT) immediately after moduleConfig.mqtt.root is rewritten on a region change, mirroring the same pattern already in AdminModule.cpp.

Copilot AI requested a review from jp-bennett May 27, 2026 13:45
@thebentern thebentern requested a review from Copilot May 27, 2026 15:18
@thebentern thebentern marked this pull request as ready for review May 27, 2026 15:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes stale MQTT publish/subscribe topics after a LoRa region change by centralizing topic-prefix (root) handling inside the MQTT singleton and reapplying it whenever moduleConfig.mqtt.root is rewritten (e.g., msh/USmsh/EU_868). This aligns runtime MQTT behavior with the device’s updated region-derived root topic without requiring a reboot.

Changes:

  • Added MQTT::reinitTopics() to rebuild cryptTopic/jsonTopic/mapTopic from moduleConfig.mqtt.root and force a broker disconnect so the next reconnect re-subscribes under the new prefix.
  • Updated region-change flows (Admin config + on-device menu) to call mqtt->reinitTopics() immediately after rewriting moduleConfig.mqtt.root.
  • Added a unit test validating that reinitTopics() refreshes subscriptions and the publish topic after a simulated region change.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/mqtt/MQTT.h Declares MQTT::reinitTopics() as the public entry point for rebuilding topic strings after root changes.
src/mqtt/MQTT.cpp Implements reinitTopics() and uses it in the constructor to avoid duplicated topic-init logic; disconnects to refresh subscriptions on reconnect.
src/modules/AdminModule.cpp Calls mqtt->reinitTopics() after rewriting the default region-derived MQTT root during region changes.
src/graphics/draw/MenuHandler.cpp Adds guarded MQTT include and calls mqtt->reinitTopics() after rewriting the region-derived MQTT root via the device UI.
test/test_mqtt/MQTT.cpp Adds test_reinitTopicsUpdatesOnRegionChange to confirm refreshed subscriptions + corrected publish topic under the new root.

@thebentern thebentern added the bugfix Pull request that fixes bugs label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Changing LoRa region does not update MQTT topic

4 participants