Skip to content

Conversation

@KipK
Copy link
Collaborator

@KipK KipK commented May 16, 2025

Refactored mqtt feature.

  • made it a microTask
  • Mongoose MQTT client callbacks are set up to call methods of the Mqtt instance (using lambdas to capture this).
  • Error Handling in attemptConnection: The _mqttclient.connect() now has a non-blocking callback for success. If connect() itself returns false, it means the connection attempt couldn't even be initiated (e.g., invalid parameters before network activity). This case is now handled by calling onMqttDisconnect. ( should fix MQTT doesn't reconnect after network outage, reboot required #969 )
  • checkAndPublishUpdates() method consolidates the logic for periodically checking if any state (claims, config, etc.) has changed and needs to be published. The publishInitialState() is called upon connection to send the current state of all relevant items.
  • notify...Changed() methods. These should be called by the respective modules (Config, EvseManager, Manual, Scheduler, Limit) when their data changes, allowing MqttTask to publish the updates promptly if connected, or upon next connection. This is more event-driven than purely polling versions in its own loop, though the loop still serves as a fallback and for connection management.
    TODO: use notify_xxx_changed() methods in related modules and remove mqtt versions check

- made it a microTask
- Mongoose MQTT client callbacks are set up to call methods of the Mqtt instance (using lambdas to capture this).
- Error Handling in attemptConnection: The _mqttclient.connect() now has a non-blocking callback for success. If connect() itself returns false, it means the connection attempt couldn't even be initiated (e.g., invalid parameters before network activity). This case is now handled by calling onMqttDisconnect. ( should fix OpenEVSE#969 )
-  checkAndPublishUpdates() method consolidates the logic for periodically checking if any state (claims, config, etc.) has changed and needs to be published. The publishInitialState() is called upon connection to send the current state of all relevant items.
- notify...Changed() methods. These should be called by the respective modules (Config, EvseManager, Manual, Scheduler, Limit) when their data changes, allowing MqttTask to publish the updates promptly if connected, or upon next connection. This is more event-driven than purely polling versions in its own loop, though the loop still serves as a fallback and for connection management. TODO: use this in related modules and remove mqtt versions check
@KipK
Copy link
Collaborator Author

KipK commented May 16, 2025

Seems there's a problem with compiling the divert_sim:
Error: ../src/mqtt.h:6:10: fatal error: MongooseMqttClient.h: No such file or directory

I couldn't check why yet.

@KipK
Copy link
Collaborator Author

KipK commented May 16, 2025

@jeremy, just tested with #964, seems all good too.

@KipK
Copy link
Collaborator Author

KipK commented May 16, 2025

I have set the mqtt microtask loop to 50ms
#define MQTT_LOOP_INTERVAL 50

What do you think is the most appropriate value?

@jeremypoulter jeremypoulter requested a review from Copilot June 3, 2025 22:10

This comment was marked as outdated.

@jeremypoulter jeremypoulter requested a review from Copilot June 11, 2025 20:56
jeremypoulter
jeremypoulter previously approved these changes Jun 11, 2025

This comment was marked as outdated.

Co-authored-by: Copilot <[email protected]>
@jeremypoulter jeremypoulter requested a review from Copilot June 11, 2025 21:04
Copy link
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

Refactors the MQTT feature into a MicroTasks-based Mqtt class and replaces legacy global mqtt_* calls with mqtt. instance methods.

  • Converts mqtt.h to define an Mqtt task class and removes C-style extern functions.
  • Updates all code sites to use mqtt.publishXxx() and mqtt.isConnected() instead of mqtt_* globals.
  • Cleans up legacy includes, adjusts setup/loop in main.cpp, and fixes a typo in override error messages.

Reviewed Changes

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

Show a summary per file
File Description
src/web_server.cpp Swapped global mqtt_* calls for mqtt.publishOverride(), fixed typo, remove stale TODO
src/scheduler.cpp Replaced mqtt_publish_schedule() with mqtt.publishSchedule()
src/mqtt.h Converted to Mqtt class, removed externs, missing ArduinoJson include
src/main.cpp Added mqtt.begin(), removed mqtt_loop(), switched to mqtt.publishData()
src/current_shaper.h Removed unused #include "mqtt.h"
src/app_config_mqtt.h Removed unused #include "mqtt.h"
src/app_config_mode.h Removed unused #include "mqtt.h"
src/app_config.cpp Swapped mqtt_connected() and mqtt_restart() for mqtt.isConnected() and mqtt.restartConnection()
Comments suppressed due to low confidence (3)

src/mqtt.h:4

  • The header declares publishData(JsonDocument &data) but doesn't include <ArduinoJson.h>, which is needed for JsonDocument.
#include <MongooseMqttClient.h>

src/mqtt.h:63

  • The comment says the handler is made static, but handleMqttMessage is a non-static member. Update or remove the outdated comment.
// Made static because MongooseMqttClient might need a C-style function pointer

src/web_server.cpp:668

  • This TODO and commented-out call is now stale. Either implement the new mqtt.publishLimit() call or remove the comment to keep the code clean.
// todo: mqtt_publish_limit();  // update limit props to mqtt

@jeremypoulter
Copy link
Collaborator

Gave this a test and looks to work, @KipK do you think it is good to merge?

@KipK
Copy link
Collaborator Author

KipK commented Jun 12, 2025

Gave this a test and looks to work, @KipK do you think it is good to merge?

It runs nows for few weeks without any trouble and fix the mqtt reconnection bug. Looks all good to me

@KipK KipK marked this pull request as ready for review June 12, 2025 07:59
@jeremypoulter jeremypoulter merged commit dca9e4b into OpenEVSE:master Jun 12, 2025
23 checks passed
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.

MQTT doesn't reconnect after network outage, reboot required

2 participants