move send logic outside loop

This commit is contained in:
Nicu Hodos 2022-01-01 16:42:36 +01:00
parent 4c3f7ca417
commit 1beeb485c9
4 changed files with 37 additions and 29 deletions

View File

@ -1,24 +1,24 @@
#pragma once #pragma once
#include <DHT.h> #include <DHT.h>
#include <TemperatureSensor.h> #include <HumiditySensor.h>
#include "TempSensor.h" #include "TempSensor.h"
#define TEMP_POSITIVE PIN_B3 #define TEMP_POSITIVE PIN_B3
#define DHT_PIN PIN_B4 #define DHT_PIN PIN_B4
struct DhtValues { struct DhtValues {
int temperature; int temperature;
int humidity; int humidity;
bool success;
}; };
class Dht22Sensor : public TempSensor<DhtValues> { class Dht22Sensor : public TempSensor, public HumiditySensor {
DHT dht = DHT(DHT_PIN, DHT22); DHT dht = DHT(DHT_PIN, DHT22);
DhtValues values;
public: public:
Dht22Sensor(short id) : Dht22Sensor(short id) :
TempSensor(id) { TempSensor(id), HumiditySensor(id) {
} }
void setup() override { void setup() override {
@ -28,13 +28,18 @@ public:
delay(2000); delay(2000);
} }
DhtValues readTemp() override { bool readTemp() override {
DhtValues dhtValues;
float temp = dht.readTemperature(); float temp = dht.readTemperature();
float humid = dht.readHumidity(); float humid = dht.readHumidity();
dhtValues.success = !isnan(temp) && !isnan(humid); bool success = !isnan(temp) && !isnan(humid);
dhtValues.temperature = roundf(temp * 10); values.temperature = roundf(temp * 10);
dhtValues.humidity = roundf(humid * 10); values.humidity = roundf(humid * 10);
return dhtValues; return success;
}
void sendValues(bool voltage) override {
voltage ? sendTempAndVoltage(values.temperature) : sendTemp(values.temperature);
delay(100);
sendHumidity(values.humidity);
} }
}; };

View File

@ -1,15 +1,14 @@
#pragma once #pragma once
#include <TemperatureSensor.h> #include <TemperatureSensor.h>
#include <HumiditySensor.h>
template <class T> class TempSensor : public TemperatureSensor {
class TempSensor : public TemperatureSensor, public HumiditySensor {
public: public:
TempSensor(short id) : TempSensor(short id) :
TemperatureSensor(id), HumiditySensor(id) { TemperatureSensor(id) {
} }
virtual void setup() = 0; virtual void setup() = 0;
virtual T readTemp() = 0; virtual bool readTemp() = 0;
virtual void sendValues(bool voltage = false) = 0;
}; };

View File

@ -5,7 +5,8 @@
#define TEMP_POSITIVE PIN_B3 #define TEMP_POSITIVE PIN_B3
#define TEMP_PIN A2 #define TEMP_PIN A2
class Tmp36Sensor : public TempSensor<int> { class Tmp36Sensor : public TempSensor {
int temperature;
public: public:
Tmp36Sensor(short id) : Tmp36Sensor(short id) :
TempSensor(id) { TempSensor(id) {
@ -17,7 +18,7 @@ public:
digitalWrite(TEMP_POSITIVE, LOW); digitalWrite(TEMP_POSITIVE, LOW);
} }
int readTemp() override { bool readTemp() override {
digitalWrite(TEMP_POSITIVE, HIGH); digitalWrite(TEMP_POSITIVE, HIGH);
delay(10); delay(10);
int reading = analogRead(TEMP_PIN); int reading = analogRead(TEMP_PIN);
@ -25,6 +26,11 @@ public:
float voltage = reading * (1100 / 1024.0); float voltage = reading * (1100 / 1024.0);
float temperatureC = (voltage - 500) / 10; float temperatureC = (voltage - 500) / 10;
return roundf(temperatureC * 10); temperature = roundf(temperatureC * 10);
return true;
}
void sendValues(bool voltage) override {
voltage ? sendTempAndVoltage(temperature) : sendTemp(temperature);
} }
}; };

View File

@ -12,7 +12,7 @@ DhtValues readTemp();
void turnOnDht(); void turnOnDht();
void turnOffDht(); void turnOffDht();
TempSensor<DhtValues> &tempSensor = *(new Dht22Sensor(TEMP_SENSOR)); TempSensor &tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
volatile int counter = 0; volatile int counter = 0;
@ -29,23 +29,21 @@ void loop() {
static bool retry = false; static bool retry = false;
if (retry || (counter % SEND_INTERVAL == 0)) { if (retry || (counter % SEND_INTERVAL == 0)) {
DhtValues values = tempSensor.readTemp(); bool success = tempSensor.readTemp();
if (values.success) { if (success) {
if (counter % SEND_VCC_INTERVAL == 0) { if (counter % SEND_VCC_INTERVAL == 0) {
tempSensor.sendTempAndVoltage(values.temperature); tempSensor.sendValues(true);
counter = 0; counter = 0;
} else { } else {
tempSensor.sendTemp(values.temperature); tempSensor.sendValues();
} }
delay(100);
tempSensor.sendHumidity(values.humidity);
turnOffDht(); turnOffDht();
} }
if (retry && !values.success) { if (retry && !success) {
turnOffDht(); turnOffDht();
} }
retry = !retry && !values.success; retry = !retry && !success;
} }
TinyPower::sleep(); TinyPower::sleep();
} }