From b26b8615d42db666b07997e9bb06a8abcfff23ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=BCbbe=20Onken?= Date: Sun, 20 Aug 2023 16:46:16 +0200 Subject: [PATCH] Give all sensor nodes an individual random start delay so that they are not all read at the same time. --- src/AdcNode.cpp | 5 +++-- src/BME280Node.cpp | 6 ++---- src/DHT22Node.cpp | 4 ++-- src/DS18B20Node.cpp | 4 ++-- src/SensorNode.cpp | 22 ++++++++++++++++------ src/SensorNode.hpp | 11 ++++++----- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/AdcNode.cpp b/src/AdcNode.cpp index df5e1fe..24099f9 100644 --- a/src/AdcNode.cpp +++ b/src/AdcNode.cpp @@ -126,7 +126,8 @@ void AdcNode::beforeHomieSetup() void AdcNode::setup() { - printCaption(); - Homie.getLogger() << cIndent << F("Send interval: ") << _sendInterval / 1000 << " s" << endl; + SensorNode::setup(); + + Homie.getLogger() << cIndent << F("Reading interval: ") << readInterval() / 1000UL << F("s") << endl; _sensorFound = true; } diff --git a/src/BME280Node.cpp b/src/BME280Node.cpp index 0529852..94658c1 100644 --- a/src/BME280Node.cpp +++ b/src/BME280Node.cpp @@ -116,16 +116,14 @@ void BME280Node::onReadyToOperate() void BME280Node::setup() { - printCaption(); + SensorNode::setup(); if (bme.begin(_i2cAddress)) { _sensorFound = true; - Homie.getLogger() << cIndent << F("found. Reading interval: ") << readInterval() / 1000UL << F(" s") << endl; - // Parameters taken from the weather station monitoring example (advancedsettings.ino) in - // the Adafruit BME280 library bme.setSampling(Adafruit_BME280::MODE_FORCED, _tempSampling, _pressSampling, _humSampling, _filter); bme.setTemperatureCompensation(_temperatureOffset->get()); + Homie.getLogger() << cIndent << F("found. Reading interval: ") << readInterval() / 1000UL << F("s") << endl; } else { diff --git a/src/DHT22Node.cpp b/src/DHT22Node.cpp index 6e184af..c69d0a5 100644 --- a/src/DHT22Node.cpp +++ b/src/DHT22Node.cpp @@ -85,12 +85,12 @@ void DHT22Node::takeMeasurement() void DHT22Node::setup() { - printCaption(); - Homie.getLogger() << cIndent << F("Reading interval: ") << readInterval() / 1000UL << F(" s") << endl; + SensorNode::setup(); if (dht) { dht->begin(); _sensorFound = true; + Homie.getLogger() << cIndent << F("Reading interval: ") << readInterval() / 1000UL << F("s") << endl; } } diff --git a/src/DS18B20Node.cpp b/src/DS18B20Node.cpp index 1d106c1..c166cf7 100644 --- a/src/DS18B20Node.cpp +++ b/src/DS18B20Node.cpp @@ -80,13 +80,13 @@ void DS18B20Node::onReadyToOperate() void DS18B20Node::setup() { - printCaption(); + SensorNode::setup(); if (dallasTemp) { dallasTemp->begin(); _sensorFound = (dallasTemp->getDS18Count() > 0); Homie.getLogger() << cIndent << F("Found ") << dallasTemp->getDS18Count() << " sensors." << endl - << cIndent << F("Reading interval: ") << readInterval() / 1000UL << F(" s") << endl; + << cIndent << F("Reading interval: ") << readInterval() / 1000UL << F("s") << endl; } } diff --git a/src/SensorNode.cpp b/src/SensorNode.cpp index c5c43da..77edadc 100644 --- a/src/SensorNode.cpp +++ b/src/SensorNode.cpp @@ -15,8 +15,8 @@ SensorNode::SensorNode(const char *id, const char *name, const char *type, : BaseNode(id, name, type), _readInterval(readInterval), _sendInterval(sendInterval), - _lastReadTime(0), - _lastSendTime(0) + _nextReadTime(0), + _nextSendTime(0) { } @@ -64,22 +64,32 @@ bool SensorNode::sensorFound() return _sensorFound; } +void SensorNode::setup() +{ + printCaption(); + // Give all sensors a random start delay from one to five seconds, + // so they are not all read at the same time. + long randomDelay = random(1000, 5000); + _nextReadTime = randomDelay; + _nextSendTime = randomDelay; +} + void SensorNode::loop() { if (sensorFound()) { unsigned long now = millis(); - if (now - _lastReadTime >= readInterval() || _lastReadTime == 0) + if (now >= _nextReadTime) { takeMeasurement(); - _lastReadTime = now; + _nextReadTime = now + readInterval(); } - if (now - _lastSendTime >= _sendInterval || _lastSendTime == 0) + if (now >= _nextSendTime) { send(); - _lastSendTime = now; + _nextSendTime = now + sendInterval(); if (_onDataSent != NULL) { _onDataSent(); diff --git a/src/SensorNode.hpp b/src/SensorNode.hpp index 80262f7..18f27c7 100644 --- a/src/SensorNode.hpp +++ b/src/SensorNode.hpp @@ -22,8 +22,8 @@ class SensorNode : public BaseNode unsigned long _readInterval; unsigned long _sendInterval; - unsigned long _lastReadTime; - unsigned long _lastSendTime; + unsigned long _nextReadTime; + unsigned long _nextSendTime; bool _sensorFound = false; const float cMinHumid = 0.0; @@ -34,20 +34,21 @@ class SensorNode : public BaseNode float computeAbsoluteHumidity(float tempCelsius, float percentHumidity); float computeDewpoint(float tempCelsius, float percentHumidity); - virtual unsigned long readInterval(); - virtual unsigned long sendInterval(); - virtual void send() = 0; virtual bool sensorFound(); virtual void takeMeasurement() = 0; virtual void loop() override; + virtual void setup() override; public: explicit SensorNode(const char *id, const char *name, const char *type, const int readInterval = READ_INTERVAL, const int sendInterval = SEND_INTERVAL); + virtual unsigned long readInterval(); + virtual unsigned long sendInterval(); + void SetOnDataSent(TOnDataSent OnDataSent) { _onDataSent = OnDataSent;