diff --git a/libraries/Tiny/TempSensor.h b/libraries/Tiny/TemperatureSensor.h similarity index 79% rename from libraries/Tiny/TempSensor.h rename to libraries/Tiny/TemperatureSensor.h index 0a072af..f3669e7 100644 --- a/libraries/Tiny/TempSensor.h +++ b/libraries/Tiny/TemperatureSensor.h @@ -2,11 +2,11 @@ #include -class TempSensor : public TinySensor { +class TemperatureSensor : public TinySensor { SensorType sensorType = TEMPERATURE; public: - TempSensor(short id) : + TemperatureSensor(short id) : TinySensor(id) { } diff --git a/temp_sensor/include/Dht22Sensor.h b/temp_sensor/include/Dht22Sensor.h new file mode 100644 index 0000000..a76f570 --- /dev/null +++ b/temp_sensor/include/Dht22Sensor.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#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 { + 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; + } +}; diff --git a/temp_sensor/include/TempSensor.h b/temp_sensor/include/TempSensor.h new file mode 100644 index 0000000..7ac483b --- /dev/null +++ b/temp_sensor/include/TempSensor.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +template +class TempSensor : public TemperatureSensor, public HumiditySensor { +public: + TempSensor(short id) : + TemperatureSensor(id), HumiditySensor(id) { + } + + virtual void setup() = 0; + virtual T readTemp() = 0; +}; diff --git a/temp_sensor/include/Tmp36Sensor.h b/temp_sensor/include/Tmp36Sensor.h new file mode 100644 index 0000000..7dea566 --- /dev/null +++ b/temp_sensor/include/Tmp36Sensor.h @@ -0,0 +1,30 @@ +#pragma once + +#include "TempSensor.h" + +#define TEMP_POSITIVE PIN_B3 +#define TEMP_PIN A2 + +class Tmp36Sensor : public TempSensor { +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); + } +}; diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index 1a527bc..7ac6fa2 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -1,38 +1,24 @@ #include -#include -#include #include -#include +#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 &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)) {