Skip to content

Commit 82a6732

Browse files
committed
Don't start MQTT with no networking
The previous version of MQTT was only called when there was a network connection. This was lost in the refactor leading to a race condition on starting up between the network connecting and MQTT starting.
1 parent dca9e4b commit 82a6732

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/mqtt.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
Mqtt mqtt(evse); // global instance
1818

19-
Mqtt::Mqtt(EvseManager &evseManager) :
19+
Mqtt::Mqtt(EvseManager &evseManager) :
2020
MicroTasks::Task(),
2121
_evse(&evseManager)
2222
{
@@ -84,7 +84,7 @@ unsigned long Mqtt::loop(MicroTasks::WakeReason reason) {
8484
}
8585

8686
// Manage connection state
87-
if (config_mqtt_enabled() && !_mqttclient.connected() && !_connecting) {
87+
if (net.isConnected() && config_mqtt_enabled() && !_mqttclient.connected() && !_connecting) {
8888
long now = millis();
8989
if (now > _nextMqttReconnectAttempt) {
9090
_nextMqttReconnectAttempt = now + MQTT_CONNECT_TIMEOUT;
@@ -99,9 +99,9 @@ unsigned long Mqtt::loop(MicroTasks::WakeReason reason) {
9999
checkAndPublishUpdates();
100100
}
101101
}
102-
103102

104103
Profile_End(Mqtt_loop, 5);
104+
//return config_mqtt_enabled() ? MQTT_LOOP_INTERVAL : MicroTask.Infinate;
105105
return MQTT_LOOP_INTERVAL;
106106
}
107107

@@ -110,7 +110,7 @@ void Mqtt::attemptConnection() {
110110
return;
111111
}
112112
_connecting = true;
113-
DBUGLN("Mqtt attempting connection...");
113+
DBUGF("MQTT attempting connection... (%s)\n", net.isConnected() ? "connected" : "not connected");
114114

115115
String mqtt_host = mqtt_server + ":" + String(mqtt_port);
116116
DBUGF("MQTT Connecting to... %s://%s", MQTT_MQTT == config_mqtt_protocol() ? "mqtt" : "mqtts", mqtt_host.c_str());
@@ -211,7 +211,7 @@ void Mqtt::subscribeTopics() {
211211
_mqttclient.subscribe(mqtt_live_pwr); yield();
212212
}
213213
}
214-
214+
215215
// Vehicle data
216216
if (mqtt_vehicle_soc != "") { _mqttclient.subscribe(mqtt_vehicle_soc); yield(); }
217217
if (mqtt_vehicle_range != "") { _mqttclient.subscribe(mqtt_vehicle_range); yield(); }
@@ -240,7 +240,7 @@ void Mqtt::publishInitialState() {
240240
_overrideVersion = manual.getVersion() == 0 ? 1 : manual.getVersion() -1;
241241
_scheduleVersion = scheduler.getVersion() == 0 ? 1 : scheduler.getVersion() -1;
242242
_limitVersion = limit.getVersion() == 0 ? 1 : limit.getVersion() -1;
243-
243+
244244
checkAndPublishUpdates(); // This will now publish everything
245245
}
246246

@@ -421,6 +421,7 @@ bool Mqtt::isConnected() {
421421
void Mqtt::restartConnection() {
422422
DBUGLN("MQTT restart requested");
423423
_mqttRestartTime = millis() + 50; // Schedule restart in the near future in loop
424+
MicroTask.wakeTask(this); // Ensure loop runs to handle the restart
424425
}
425426

426427
void Mqtt::publishData(JsonDocument &data) {
@@ -573,4 +574,4 @@ void Mqtt::notifyConfigChanged() {
573574
_configVersion = config_version();
574575
if (isConnected()) publishConfig();
575576
}
576-
}
577+
}

0 commit comments

Comments
 (0)