DallasSensor lastSentValue FIX

This commit is contained in:
Patrick Haßel 2024-04-12 11:14:27 +02:00
parent adb14378fc
commit bf67e8b2b7
3 changed files with 16 additions and 8 deletions

View File

@ -1,6 +1,7 @@
#include "data.h" #include "data.h"
#include "ArduinoJson.h" #include "ArduinoJson.h"
#include "mqtt.h" #include "mqtt.h"
#include "wifi.h"
#define ENTRY_COUNT 1500 #define ENTRY_COUNT 1500
@ -31,12 +32,12 @@ bool dataAdd(const char *name, const time_t timestamp, const double value) {
} }
void dataLoop() { void dataLoop() {
if (dataCount == 0) { if (dataCount == 0 || !isTimeSet()) {
return; return;
} }
JsonDocument json; JsonDocument json;
json["timestamp"] = dataRead->timestamp; json["timestamp"] = correctTime(dataRead->timestamp);
json["putDouble"] = dataRead->value; json["value"] = dataRead->value;
if (mqttPublishData(dataRead->name, json)) { if (mqttPublishData(dataRead->name, json)) {
dataRead = (dataRead - data + 1) % ENTRY_COUNT + data; dataRead = (dataRead - data + 1) % ENTRY_COUNT + data;
dataCount--; dataCount--;

View File

@ -21,7 +21,11 @@ private:
const time_t timeoutSec; const time_t timeoutSec;
const time_t minIntervalSec; const time_t minIntervalMillis;
double lastSentValue = NAN;
unsigned long lastSentMillis = 0;
double lastValue = NAN; double lastValue = NAN;
@ -33,13 +37,13 @@ private:
public: 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), sensors(sensors),
address(address), address(address),
name(name), name(name),
valueThreshold(valueThreshold), valueThreshold(valueThreshold),
timeoutSec(timeoutSec), timeoutSec(timeoutSec),
minIntervalSec(minIntervalSec) { minIntervalMillis(minIntervalMillis) {
// - // -
} }
@ -52,8 +56,11 @@ public:
if (sensors.isConverted()) { if (sensors.isConverted()) {
const double value = sensors.read(address); const double value = sensors.read(address);
const time_t timestamp = sensors.getTimestamp(); 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) { if (doPublish) {
lastSentValue = value;
lastSentMillis = millisNow;
dataAdd(name, timestamp, value); dataAdd(name, timestamp, value);
} }
lastValue = value; lastValue = value;

View File

@ -21,7 +21,7 @@
Dallas dallas(SENSOR_GPIO); 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; ArduPID pid;