From bf67e8b2b78a46082f28f053319adef458c7830f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Fri, 12 Apr 2024 11:14:27 +0200 Subject: [PATCH] DallasSensor lastSentValue FIX --- lib/patrix/data.cpp | 7 ++++--- lib/patrix/sensors/DallasSensor.h | 15 +++++++++++---- src/Fermenter.cpp | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/patrix/data.cpp b/lib/patrix/data.cpp index 79c4540..6205816 100644 --- a/lib/patrix/data.cpp +++ b/lib/patrix/data.cpp @@ -1,6 +1,7 @@ #include "data.h" #include "ArduinoJson.h" #include "mqtt.h" +#include "wifi.h" #define ENTRY_COUNT 1500 @@ -31,12 +32,12 @@ bool dataAdd(const char *name, const time_t timestamp, const double value) { } void dataLoop() { - if (dataCount == 0) { + if (dataCount == 0 || !isTimeSet()) { return; } JsonDocument json; - json["timestamp"] = dataRead->timestamp; - json["putDouble"] = dataRead->value; + json["timestamp"] = correctTime(dataRead->timestamp); + json["value"] = dataRead->value; if (mqttPublishData(dataRead->name, json)) { dataRead = (dataRead - data + 1) % ENTRY_COUNT + data; dataCount--; diff --git a/lib/patrix/sensors/DallasSensor.h b/lib/patrix/sensors/DallasSensor.h index 700b5ee..55e7812 100644 --- a/lib/patrix/sensors/DallasSensor.h +++ b/lib/patrix/sensors/DallasSensor.h @@ -21,7 +21,11 @@ private: const time_t timeoutSec; - const time_t minIntervalSec; + const time_t minIntervalMillis; + + double lastSentValue = NAN; + + unsigned long lastSentMillis = 0; double lastValue = NAN; @@ -33,13 +37,13 @@ private: public: - DallasSensor(Dallas &sensors, const uint64_t address, const char *name, const double valueThreshold, const time_t timeoutSec, const time_t minIntervalSec) : + DallasSensor(Dallas &sensors, const uint64_t address, const char *name, const double valueThreshold, const time_t timeoutSec, const time_t minIntervalMillis) : sensors(sensors), address(address), name(name), valueThreshold(valueThreshold), timeoutSec(timeoutSec), - minIntervalSec(minIntervalSec) { + minIntervalMillis(minIntervalMillis) { // - } @@ -52,8 +56,11 @@ public: if (sensors.isConverted()) { const double value = sensors.read(address); const time_t timestamp = sensors.getTimestamp(); - const bool doPublish = !isnan(value) && (abs(lastValue - value) >= valueThreshold || now - lastTimestamp >= minIntervalSec); + const unsigned long millisNow=millis(); + const bool doPublish = !isnan(value) && (abs(lastSentValue - value) >= valueThreshold || millisNow - lastSentMillis >= minIntervalMillis); if (doPublish) { + lastSentValue = value; + lastSentMillis = millisNow; dataAdd(name, timestamp, value); } lastValue = value; diff --git a/src/Fermenter.cpp b/src/Fermenter.cpp index 7da52b9..c762593 100644 --- a/src/Fermenter.cpp +++ b/src/Fermenter.cpp @@ -21,7 +21,7 @@ Dallas dallas(SENSOR_GPIO); -DallasSensor sensor(dallas, 0x3D0417C1D740FF28, "sensor", 0.5, 5, 60); +DallasSensor sensor(dallas, 0x3D0417C1D740FF28, "sensor", 0.1, 5, 60 * 1000); ArduPID pid;