move send logic outside loop
This commit is contained in:
parent
4c3f7ca417
commit
1beeb485c9
@ -1,24 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <DHT.h>
|
||||
#include <TemperatureSensor.h>
|
||||
#include <HumiditySensor.h>
|
||||
#include "TempSensor.h"
|
||||
|
||||
#define TEMP_POSITIVE PIN_B3
|
||||
#define DHT_PIN PIN_B4
|
||||
|
||||
struct DhtValues {
|
||||
int temperature;
|
||||
int humidity;
|
||||
bool success;
|
||||
int temperature;
|
||||
int humidity;
|
||||
};
|
||||
|
||||
class Dht22Sensor : public TempSensor<DhtValues> {
|
||||
class Dht22Sensor : public TempSensor, public HumiditySensor {
|
||||
DHT dht = DHT(DHT_PIN, DHT22);
|
||||
DhtValues values;
|
||||
|
||||
public:
|
||||
Dht22Sensor(short id) :
|
||||
TempSensor(id) {
|
||||
TempSensor(id), HumiditySensor(id) {
|
||||
}
|
||||
|
||||
void setup() override {
|
||||
@ -28,13 +28,18 @@ public:
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
DhtValues readTemp() override {
|
||||
DhtValues dhtValues;
|
||||
bool readTemp() override {
|
||||
float temp = dht.readTemperature();
|
||||
float humid = dht.readHumidity();
|
||||
dhtValues.success = !isnan(temp) && !isnan(humid);
|
||||
dhtValues.temperature = roundf(temp * 10);
|
||||
dhtValues.humidity = roundf(humid * 10);
|
||||
return dhtValues;
|
||||
bool success = !isnan(temp) && !isnan(humid);
|
||||
values.temperature = roundf(temp * 10);
|
||||
values.humidity = roundf(humid * 10);
|
||||
return success;
|
||||
}
|
||||
|
||||
void sendValues(bool voltage) override {
|
||||
voltage ? sendTempAndVoltage(values.temperature) : sendTemp(values.temperature);
|
||||
delay(100);
|
||||
sendHumidity(values.humidity);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,15 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <TemperatureSensor.h>
|
||||
#include <HumiditySensor.h>
|
||||
|
||||
template <class T>
|
||||
class TempSensor : public TemperatureSensor, public HumiditySensor {
|
||||
class TempSensor : public TemperatureSensor {
|
||||
public:
|
||||
TempSensor(short id) :
|
||||
TemperatureSensor(id), HumiditySensor(id) {
|
||||
TemperatureSensor(id) {
|
||||
}
|
||||
|
||||
virtual void setup() = 0;
|
||||
virtual T readTemp() = 0;
|
||||
virtual bool readTemp() = 0;
|
||||
virtual void sendValues(bool voltage = false) = 0;
|
||||
};
|
||||
|
||||
@ -5,7 +5,8 @@
|
||||
#define TEMP_POSITIVE PIN_B3
|
||||
#define TEMP_PIN A2
|
||||
|
||||
class Tmp36Sensor : public TempSensor<int> {
|
||||
class Tmp36Sensor : public TempSensor {
|
||||
int temperature;
|
||||
public:
|
||||
Tmp36Sensor(short id) :
|
||||
TempSensor(id) {
|
||||
@ -17,7 +18,7 @@ public:
|
||||
digitalWrite(TEMP_POSITIVE, LOW);
|
||||
}
|
||||
|
||||
int readTemp() override {
|
||||
bool readTemp() override {
|
||||
digitalWrite(TEMP_POSITIVE, HIGH);
|
||||
delay(10);
|
||||
int reading = analogRead(TEMP_PIN);
|
||||
@ -25,6 +26,11 @@ public:
|
||||
|
||||
float voltage = reading * (1100 / 1024.0);
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
@ -12,7 +12,7 @@ DhtValues readTemp();
|
||||
void turnOnDht();
|
||||
void turnOffDht();
|
||||
|
||||
TempSensor<DhtValues> &tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
|
||||
TempSensor &tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
|
||||
|
||||
volatile int counter = 0;
|
||||
|
||||
@ -29,23 +29,21 @@ void loop() {
|
||||
static bool retry = false;
|
||||
|
||||
if (retry || (counter % SEND_INTERVAL == 0)) {
|
||||
DhtValues values = tempSensor.readTemp();
|
||||
if (values.success) {
|
||||
bool success = tempSensor.readTemp();
|
||||
if (success) {
|
||||
if (counter % SEND_VCC_INTERVAL == 0) {
|
||||
tempSensor.sendTempAndVoltage(values.temperature);
|
||||
tempSensor.sendValues(true);
|
||||
counter = 0;
|
||||
} else {
|
||||
tempSensor.sendTemp(values.temperature);
|
||||
tempSensor.sendValues();
|
||||
}
|
||||
delay(100);
|
||||
tempSensor.sendHumidity(values.humidity);
|
||||
|
||||
turnOffDht();
|
||||
}
|
||||
if (retry && !values.success) {
|
||||
if (retry && !success) {
|
||||
turnOffDht();
|
||||
}
|
||||
retry = !retry && !values.success;
|
||||
retry = !retry && !success;
|
||||
}
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user