From 733ca426a4fd9a23ec3235e973e0f16935b1068e Mon Sep 17 00:00:00 2001 From: Holger Fleischmann Date: Sun, 15 Nov 2020 11:36:06 +0100 Subject: [PATCH] trying to fix mqtt stops to work after a few hours --- include/ingest.h | 1 + src/ingest.cpp | 13 ++++++++++++- src/main.cpp | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/ingest.h b/include/ingest.h index cc4a371..f62c9e9 100644 --- a/include/ingest.h +++ b/include/ingest.h @@ -4,6 +4,7 @@ #include "GeigerData.h" void initIngest(); +void loopIngest(); void deinitIngest(); void ingestToThingspeak(GeigerData &geigerData, uint16_t intervalSamples); void ingestToMqtt(GeigerData &geigerData, uint16_t intervalSamples); diff --git a/src/ingest.cpp b/src/ingest.cpp index e35e104..82e0f63 100644 --- a/src/ingest.cpp +++ b/src/ingest.cpp @@ -50,6 +50,14 @@ void initIngest() connectWiFi(); } +void loopIngest() +{ + if (mqttClient.connected()) + { + mqttClient.loop(); + } +} + void deinitIngest() { if (WiFi.status() == WL_CONNECTED) @@ -182,6 +190,9 @@ void ingestToMqtt(GeigerData &geigerData, uint16_t intervalSamples) sprintf(payload, "{\"pulses\":%lu, \"cpm\":%lu,\"uSph\":%.3f,\"secs\":%d}", pulses, cpm, uSph, (int)intervalSamples); - mqttClient.publish(mqttTopic, payload); + if (!mqttClient.publish(mqttTopic, payload)) + { + mqttClient.disconnect(); + } } } diff --git a/src/main.cpp b/src/main.cpp index 1ab2cb7..e950eab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,6 +93,7 @@ void loop() if (wifiSwitchOn()) { + loopIngest(); ingestCountdown--; if (ingestCountdown <= 0) {