diff --git a/include/mqttclient.h b/include/mqttclient.h index 1e6d36e8..9bb90364 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -10,6 +10,7 @@ #define MQTT_OUTTOPIC "paxcounter_out/" #define MQTT_PORT 1883 #define MQTT_SERVER "broker.hivemq.com" +#define MQTT_RETRYSEC 10 // retry reconnect every 10 seconds extern TaskHandle_t mqttTask; extern PubSubClient mqttClient; diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 80e009bf..bfd11319 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -97,13 +97,12 @@ void mqtt_client_task(void *param) { } else goto reconnect; - } else { // not connected, thus re-enqueue the undelivered message + } else { - reconnect: - - mqtt_enqueuedata(&msg); - delay(10000); // attempt to reconnect to MQTT server + reconnect: + mqtt_enqueuedata(&msg); // postpone the undelivered message + delay(MQTT_RETRYSEC * 1000); mqtt_connect(MQTT_SERVER, MQTT_PORT); } } // while(1) @@ -151,11 +150,14 @@ void mqtt_enqueuedata(MessageBuffer_t *message) { ESP_LOGW(TAG, "MQTT sendqueue is full"); } -void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); } - void mqtt_callback(char *topic, byte *payload, unsigned int length) { - ESP_LOGD(TAG, "MQTT %d byte(s) received", length); - rcommand(payload, length); + String s = ""; + for (int i = 0; i < length; i++) + s += (char)payload[i]; + ESP_LOGD(TAG, "MQTT: Received %u byte(s) of payload [%s]", length, s); + // rcommand(payload, length); } +void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); } + #endif // HAS_MQTT \ No newline at end of file