Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luebbe committed Aug 20, 2023
2 parents 2685980 + b26b861 commit cea20ef
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/AdcNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 2 additions & 4 deletions src/BME280Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/DHT22Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/DS18B20Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
22 changes: 16 additions & 6 deletions src/SensorNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions src/SensorNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit cea20ef

Please sign in to comment.