move temperature sensor code inside its own class
This commit is contained in:
parent
cdb0711ba6
commit
4c3f7ca417
@ -2,11 +2,11 @@
|
||||
|
||||
#include <TinySensor.h>
|
||||
|
||||
class TempSensor : public TinySensor {
|
||||
class TemperatureSensor : public TinySensor {
|
||||
SensorType sensorType = TEMPERATURE;
|
||||
|
||||
public:
|
||||
TempSensor(short id) :
|
||||
TemperatureSensor(short id) :
|
||||
TinySensor(id) {
|
||||
}
|
||||
|
||||
40
temp_sensor/include/Dht22Sensor.h
Normal file
40
temp_sensor/include/Dht22Sensor.h
Normal file
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <DHT.h>
|
||||
#include <TemperatureSensor.h>
|
||||
#include "TempSensor.h"
|
||||
|
||||
#define TEMP_POSITIVE PIN_B3
|
||||
#define DHT_PIN PIN_B4
|
||||
|
||||
struct DhtValues {
|
||||
int temperature;
|
||||
int humidity;
|
||||
bool success;
|
||||
};
|
||||
|
||||
class Dht22Sensor : public TempSensor<DhtValues> {
|
||||
DHT dht = DHT(DHT_PIN, DHT22);
|
||||
|
||||
public:
|
||||
Dht22Sensor(short id) :
|
||||
TempSensor(id) {
|
||||
}
|
||||
|
||||
void setup() override {
|
||||
pinMode(TEMP_POSITIVE, OUTPUT);
|
||||
digitalWrite(TEMP_POSITIVE, HIGH);
|
||||
dht.begin();
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
DhtValues readTemp() override {
|
||||
DhtValues dhtValues;
|
||||
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;
|
||||
}
|
||||
};
|
||||
15
temp_sensor/include/TempSensor.h
Normal file
15
temp_sensor/include/TempSensor.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <TemperatureSensor.h>
|
||||
#include <HumiditySensor.h>
|
||||
|
||||
template <class T>
|
||||
class TempSensor : public TemperatureSensor, public HumiditySensor {
|
||||
public:
|
||||
TempSensor(short id) :
|
||||
TemperatureSensor(id), HumiditySensor(id) {
|
||||
}
|
||||
|
||||
virtual void setup() = 0;
|
||||
virtual T readTemp() = 0;
|
||||
};
|
||||
30
temp_sensor/include/Tmp36Sensor.h
Normal file
30
temp_sensor/include/Tmp36Sensor.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "TempSensor.h"
|
||||
|
||||
#define TEMP_POSITIVE PIN_B3
|
||||
#define TEMP_PIN A2
|
||||
|
||||
class Tmp36Sensor : public TempSensor<int> {
|
||||
public:
|
||||
Tmp36Sensor(short id) :
|
||||
TempSensor(id) {
|
||||
}
|
||||
|
||||
void setup() override {
|
||||
analogReference(INTERNAL);
|
||||
pinMode(TEMP_POSITIVE, OUTPUT);
|
||||
digitalWrite(TEMP_POSITIVE, LOW);
|
||||
}
|
||||
|
||||
int readTemp() override {
|
||||
digitalWrite(TEMP_POSITIVE, HIGH);
|
||||
delay(10);
|
||||
int reading = analogRead(TEMP_PIN);
|
||||
digitalWrite(TEMP_POSITIVE, LOW);
|
||||
|
||||
float voltage = reading * (1100 / 1024.0);
|
||||
float temperatureC = (voltage - 500) / 10;
|
||||
return roundf(temperatureC * 10);
|
||||
}
|
||||
};
|
||||
@ -1,38 +1,24 @@
|
||||
#include <Arduino.h>
|
||||
#include <TempSensor.h>
|
||||
#include <HumiditySensor.h>
|
||||
#include <TinyPower.h>
|
||||
#include <DHT.h>
|
||||
#include "Dht22Sensor.h"
|
||||
|
||||
#define SEND_INTERVAL 37 // 37*8s = ~5min
|
||||
#define SEND_VCC_INTERVAL (SEND_INTERVAL*6) // every half hour
|
||||
|
||||
// Pins
|
||||
#define TEMP_POSITIVE PIN_B3
|
||||
#define SENDER PIN_B2
|
||||
#define DHT_PIN PIN_B4
|
||||
|
||||
struct DhtValues {
|
||||
int temperature;
|
||||
int humidity;
|
||||
bool success;
|
||||
};
|
||||
DhtValues readTemp();
|
||||
void turnOnDht();
|
||||
void turnOffDht();
|
||||
|
||||
TempSensor tempSensor = TempSensor(TEMP_SENSOR);
|
||||
HumiditySensor humidSensor = HumiditySensor(TEMP_SENSOR);
|
||||
DHT dht(DHT_PIN, DHT22);
|
||||
TempSensor<DhtValues> &tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
|
||||
|
||||
volatile int counter = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
pinMode(TEMP_POSITIVE, OUTPUT);
|
||||
digitalWrite(TEMP_POSITIVE, HIGH);
|
||||
dht.begin();
|
||||
delay(2000);
|
||||
tempSensor.setup();
|
||||
|
||||
TinySwitch::setup(SENDER);
|
||||
TinyPower::setup();
|
||||
@ -43,7 +29,7 @@ void loop() {
|
||||
static bool retry = false;
|
||||
|
||||
if (retry || (counter % SEND_INTERVAL == 0)) {
|
||||
DhtValues values = readTemp();
|
||||
DhtValues values = tempSensor.readTemp();
|
||||
if (values.success) {
|
||||
if (counter % SEND_VCC_INTERVAL == 0) {
|
||||
tempSensor.sendTempAndVoltage(values.temperature);
|
||||
@ -52,7 +38,7 @@ void loop() {
|
||||
tempSensor.sendTemp(values.temperature);
|
||||
}
|
||||
delay(100);
|
||||
humidSensor.sendHumidity(values.humidity);
|
||||
tempSensor.sendHumidity(values.humidity);
|
||||
|
||||
turnOffDht();
|
||||
}
|
||||
@ -64,16 +50,6 @@ void loop() {
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
DhtValues readTemp() {
|
||||
DhtValues dhtValues;
|
||||
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;
|
||||
}
|
||||
|
||||
ISR(WDT_vect) {
|
||||
counter++;
|
||||
if (((counter + 1) % SEND_INTERVAL == 0) || ((counter + 1) % SEND_VCC_INTERVAL == 0)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user