--- a/mqtt/mqtt_template.c Thu Aug 01 12:09:28 2024 +0200
+++ b/mqtt/mqtt_template.c Thu Aug 01 12:11:11 2024 +0200
@@ -62,6 +62,9 @@
/* condition to wakeup publish thread */
static int MQTT_any_pub_var_changed = 0;
+/* Keep track of connection state */ +static volatile int MQTT_is_disconnected = 1; /* mutex to keep incoming PLC data consistent */
static pthread_mutex_t MQTT_retrieve_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -116,6 +119,7 @@
LogWarning("ConnectionLost, reconnecting\\n");
if (pthread_mutex_lock(&MQTT_thread_wakeup_mutex) == 0){{
/* unblock publish thread so that it can reconnect */
+ MQTT_is_disconnected = 1; pthread_cond_signal(&MQTT_thread_wakeup);
pthread_mutex_unlock(&MQTT_thread_wakeup_mutex);
@@ -257,7 +261,10 @@
if (rc != MQTTCLIENT_SUCCESS) {{
+ MQTT_is_disconnected = 1; + MQTT_is_disconnected = 0; @@ -270,13 +277,22 @@
while((rc = pthread_mutex_lock(&MQTT_thread_wakeup_mutex)) == 0 && !MQTT_stop_thread){{
pthread_cond_wait(&MQTT_thread_wakeup, &MQTT_thread_wakeup_mutex);
- is_connected = MQTTClient_isConnected(client);
- do_publish = MQTT_any_pub_var_changed && is_connected;
+ if(MQTT_is_disconnected) + /* TODO growing retry delay */ + /* TODO max retry delay as config parameter */ + if (rc == MQTTCLIENT_SUCCESS) {{ + LogInfo("MQTT Reconnected\n"); + LogError("MQTT Reconnect Failed, return code %d\n", rc); + if(!MQTT_is_disconnected && MQTT_any_pub_var_changed) /* publish changes, and reset variable's state to UNCHANGED */
@@ -285,16 +301,6 @@
pthread_mutex_unlock(&MQTT_thread_wakeup_mutex);
- if (rc == MQTTCLIENT_SUCCESS) {{
- LogInfo("MQTT Reconnected\n");
- LogError("MQTT Reconnect Failed, return code %d\n", rc);
if(MQTT_stop_thread) break;
@@ -394,7 +400,7 @@
/* copy PLC_* variables to MQTT_*, and mark those who changed */
/* if any change detcted, unblock publish thread */
- if(MQTT_any_pub_var_changed){{
+ if(MQTT_any_pub_var_changed || MQTT_is_disconnected){{ pthread_cond_signal(&MQTT_thread_wakeup);
pthread_mutex_unlock(&MQTT_thread_wakeup_mutex);