From fc174632721577d786e251fe76a4d710f1903fd9 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 25 Dec 2021 15:58:00 +0100 Subject: [PATCH 1/9] add support for humidity --- gateway/src/gateway.cpp | 7 +++++++ libraries/Tiny/ContactSensor.h | 1 - libraries/Tiny/HumiditySensor.h | 20 ++++++++++++++++++++ libraries/Tiny/TempSensor.h | 1 - libraries/Tiny/Tiny.h | 5 ++++- 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 libraries/Tiny/HumiditySensor.h diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index c2a6bae..9fcba96 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -72,6 +72,13 @@ void readRcSwitch(JsonDocument& jsonDoc) { sensor["voltage"] = GET_VCC(value); return; } + if (GET_TYPE(value) == SensorType::HUMIDITY) { + JsonObject sensor = jsonDoc.createNestedObject("sensor"); + sensor["id"] = ID(value); + sensor["humidity"] = GET_HUMIDITY(value); + sensor["voltage"] = GET_VCC(value); + return; + } if (GET_TYPE(value) == SensorType::CONTACT) { JsonObject sensor = jsonDoc.createNestedObject("contact"); sensor["id"] = ID(value); diff --git a/libraries/Tiny/ContactSensor.h b/libraries/Tiny/ContactSensor.h index c6fdf5b..367e5a5 100644 --- a/libraries/Tiny/ContactSensor.h +++ b/libraries/Tiny/ContactSensor.h @@ -1,7 +1,6 @@ #pragma once #include -#include class ContactSensor: public TinySensor { SensorType sensorType = CONTACT; diff --git a/libraries/Tiny/HumiditySensor.h b/libraries/Tiny/HumiditySensor.h new file mode 100644 index 0000000..24dd903 --- /dev/null +++ b/libraries/Tiny/HumiditySensor.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +class HumiditySensor : public TinySensor { + SensorType sensorType = HUMIDITY; + +public: + HumiditySensor(short id, short senderPin) : + TinySensor(id, senderPin) { + } + + void sendHumidityAndVoltage(int humidity) { + sendInfo(ID(id) | VCC(readVcc()) | HUMIDITY(humidity) | TYPE(sensorType)); + } + + void sendHumidity(int humidity) { + sendInfo(ID(id) | HUMIDITY(humidity) | TYPE(sensorType)); + } +}; diff --git a/libraries/Tiny/TempSensor.h b/libraries/Tiny/TempSensor.h index 0120ac5..86cc2fd 100644 --- a/libraries/Tiny/TempSensor.h +++ b/libraries/Tiny/TempSensor.h @@ -1,7 +1,6 @@ #pragma once #include -#include class TempSensor : public TinySensor { SensorType sensorType = TEMPERATURE; diff --git a/libraries/Tiny/Tiny.h b/libraries/Tiny/Tiny.h index c644c75..ad7dcca 100644 --- a/libraries/Tiny/Tiny.h +++ b/libraries/Tiny/Tiny.h @@ -4,14 +4,17 @@ #define STATE(value) ((value & 0x1) << 5) #define VCC(value) ((value & 0x1FFF) << 6) #define TEMP(value) (((unsigned long)value & 0x1FF) << 19) +#define HUMIDITY(value) (((unsigned long)value & 0x7F) << 19) #define TYPE(value) (((unsigned long)value & 0xF) << 28) #define GET_TYPE(value) (((unsigned long)value >> 28) & 0xF) #define GET_TEMP(value) (((unsigned long)value >> 19) & 0x1FF) +#define GET_HUMIDITY(value) (((unsigned long)value >> 19) & 0x7F) #define GET_VCC(value) ((value >> 6) & 0x1FFF) #define GET_STATE(value) ((value >> 5) & 0x1) -typedef enum SensorType { +enum SensorType { + HUMIDITY = 5, TEMPERATURE = 6, CONTACT = 7 }; From 632c6cc63f3906ed9373ce91185e6cc1cb0d19d8 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 25 Dec 2021 15:34:00 +0100 Subject: [PATCH 2/9] switch to DHT22 --- temp_sensor/platformio.ini | 18 ++++----- temp_sensor/src/temp_sensor.cpp | 65 ++++++++++++++------------------- 2 files changed, 36 insertions(+), 47 deletions(-) diff --git a/temp_sensor/platformio.ini b/temp_sensor/platformio.ini index c5ce189..6e96aaa 100644 --- a/temp_sensor/platformio.ini +++ b/temp_sensor/platformio.ini @@ -12,15 +12,15 @@ platform = atmelavr board = attiny85 framework = arduino -lib_extra_dirs = - ../libraries - ~/Arduino/libraries +lib_deps = + adafruit/Adafruit Unified Sensor @ ^1.1.4 + adafruit/DHT sensor library@^1.4.3 +lib_extra_dirs = + ../libraries + ~/Arduino/libraries upload_protocol = stk500v1 -; each flag in a new line -upload_flags = - -P$UPLOAD_PORT - -b$UPLOAD_SPEED - -; edit these lines +upload_flags = + -P$UPLOAD_PORT + -b$UPLOAD_SPEED upload_port = /dev/ttyACM0 upload_speed = 19200 diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index 3ab2e14..044431d 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -1,39 +1,35 @@ #include #include +#include #include -#include +#include -#define DEBUG 0 #define SEND_INTERVAL (int)(5*60/8) #define SEND_VCC_INTERVAL (int)(60*60/8) // Pins #define TEMP_POSITIVE PIN_B3 #define SENDER PIN_B2 -#define TEMP_PIN A2 +#define DHT_PIN PIN_B4 -#if DEBUG -#define RxD PIN_B3 -#define TxD PIN_B4 -SoftwareSerial AttinySerial(RxD, TxD); -#endif +struct DhtValues { + int temperature; + int humidity; +}; +DhtValues readTemp(); -int readTemp(); - -TempSensor sensor = TempSensor(TEMP_SENSOR, SENDER); +TempSensor tempSensor = TempSensor(TEMP_SENSOR, SENDER); +HumiditySensor humidSensor = HumiditySensor(TEMP_SENSOR, SENDER); +DHT dht(DHT_PIN, DHT22); volatile int counter = 0; void setup() { -#if DEBUG - AttinySerial.begin(9600); - AttinySerial.println("starting..."); -#endif - sensor.setup(); - analogReference(INTERNAL); + tempSensor.setup(); pinMode(TEMP_POSITIVE, OUTPUT); - digitalWrite(TEMP_POSITIVE, LOW); + digitalWrite(TEMP_POSITIVE, HIGH); + dht.begin(); TinyPower::setup(); TinyPower::enableWdt(WDTO_8S); @@ -41,35 +37,28 @@ void setup() { void loop() { if (counter % SEND_VCC_INTERVAL == 0) { - sensor.sendTempAndVoltage(readTemp()); + DhtValues values = readTemp(); + tempSensor.sendTempAndVoltage(values.temperature); + delay(100); + humidSensor.sendHumidity(values.humidity); counter = 0; } else if (counter % SEND_INTERVAL == 0) { - sensor.sendTemp(readTemp()); + DhtValues values = readTemp(); + tempSensor.sendTemp(values.temperature); + delay(100); + humidSensor.sendHumidity(values.humidity); } TinyPower::sleep(); } -int readTemp() { +DhtValues readTemp() { digitalWrite(TEMP_POSITIVE, HIGH); delay(10); - int reading = analogRead(TEMP_PIN); + DhtValues dhtValues; + dhtValues.temperature = roundf(dht.readTemperature() * 10); + dhtValues.humidity = roundf(dht.readHumidity() * 10); digitalWrite(TEMP_POSITIVE, LOW); -#if DEBUG - AttinySerial.println(reading); -#endif - - float voltage = reading * (1100 / 1024.0); -#if DEBUG - AttinySerial.print(voltage); - AttinySerial.println(" mili volts"); -#endif - - float temperatureC = (voltage - 500) / 10; -#if DEBUG - AttinySerial.print(temperatureC); - AttinySerial.println(" degrees C"); -#endif - return roundf(temperatureC * 10); + return dhtValues; } ISR(PCINT0_vect) { From a9f8598a63ecf959bb2fd953fc3de939e5a7303a Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 28 Dec 2021 18:59:11 +0100 Subject: [PATCH 3/9] gateway: update platformio board after upgrade --- gateway/platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway/platformio.ini b/gateway/platformio.ini index b1bf759..7a54706 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -10,7 +10,7 @@ [env:pro-mini] platform = atmelavr -board = pro16MHzatmega328 +board = miniatmega328 framework = arduino lib_extra_dirs = ../libraries From f5b7b24ceae8de8c00d4b936928a53da1b765238 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 28 Dec 2021 19:02:15 +0100 Subject: [PATCH 4/9] split TinySensor into TinySwitch --- libraries/Tiny/ContactSensor.h | 4 ++-- libraries/Tiny/HumiditySensor.h | 4 ++-- libraries/Tiny/TempSensor.h | 4 ++-- libraries/Tiny/TinySensor.h | 18 ++++-------------- libraries/Tiny/TinySwitch.h | 16 ++++++++++++++++ libraries/TinyPower/TinyPower.h | 28 ++++++++++++++-------------- temp_sensor/src/temp_sensor.cpp | 6 +++--- 7 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 libraries/Tiny/TinySwitch.h diff --git a/libraries/Tiny/ContactSensor.h b/libraries/Tiny/ContactSensor.h index 367e5a5..dd3e702 100644 --- a/libraries/Tiny/ContactSensor.h +++ b/libraries/Tiny/ContactSensor.h @@ -6,8 +6,8 @@ class ContactSensor: public TinySensor { SensorType sensorType = CONTACT; public: - ContactSensor(short id, short senderPin) : - TinySensor(id, senderPin) { + ContactSensor(short id) : + TinySensor(id) { } void sendStateAndVoltage(bool state) { diff --git a/libraries/Tiny/HumiditySensor.h b/libraries/Tiny/HumiditySensor.h index 24dd903..44a7db1 100644 --- a/libraries/Tiny/HumiditySensor.h +++ b/libraries/Tiny/HumiditySensor.h @@ -6,8 +6,8 @@ class HumiditySensor : public TinySensor { SensorType sensorType = HUMIDITY; public: - HumiditySensor(short id, short senderPin) : - TinySensor(id, senderPin) { + HumiditySensor(short id) : + TinySensor(id) { } void sendHumidityAndVoltage(int humidity) { diff --git a/libraries/Tiny/TempSensor.h b/libraries/Tiny/TempSensor.h index 86cc2fd..0a072af 100644 --- a/libraries/Tiny/TempSensor.h +++ b/libraries/Tiny/TempSensor.h @@ -6,8 +6,8 @@ class TempSensor : public TinySensor { SensorType sensorType = TEMPERATURE; public: - TempSensor(short id, short senderPin) : - TinySensor(id, senderPin) { + TempSensor(short id) : + TinySensor(id) { } void sendTempAndVoltage(int temp) { diff --git a/libraries/Tiny/TinySensor.h b/libraries/Tiny/TinySensor.h index e1a6b90..8864fa5 100644 --- a/libraries/Tiny/TinySensor.h +++ b/libraries/Tiny/TinySensor.h @@ -1,13 +1,13 @@ #pragma once -#include +#include "TinySwitch.h" #include "Tiny.h" +using TinySwitch::sendInfo; + class TinySensor { protected: short id; - short senderPin; - RCSwitch mySwitch = RCSwitch(); long readVcc() { // Read 1.1V reference against AVcc @@ -32,18 +32,8 @@ protected: return result; // Vcc in millivolts } - void sendInfo(unsigned long value) { - mySwitch.send(value, 32); - } - public: - TinySensor(short id, short senderPin) { + TinySensor(short id) { this->id = id; - this->senderPin = senderPin; - } - - void setup() { - mySwitch.enableTransmit(senderPin); - mySwitch.setProtocol(2); } }; diff --git a/libraries/Tiny/TinySwitch.h b/libraries/Tiny/TinySwitch.h new file mode 100644 index 0000000..c5d39c9 --- /dev/null +++ b/libraries/Tiny/TinySwitch.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace TinySwitch { + RCSwitch mySwitch = RCSwitch(); + + void sendInfo(unsigned long value) { + mySwitch.send(value, 32); + } + + void setup(short senderPin) { + mySwitch.enableTransmit(senderPin); + mySwitch.setProtocol(2); + } +} diff --git a/libraries/TinyPower/TinyPower.h b/libraries/TinyPower/TinyPower.h index 80c955a..6ed48c5 100644 --- a/libraries/TinyPower/TinyPower.h +++ b/libraries/TinyPower/TinyPower.h @@ -14,26 +14,26 @@ class TinyPower { public: static void setup() { - set_sleep_mode(SLEEP_MODE_PWR_DOWN); - enable_pin_interrupts(); + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + enable_pin_interrupts(); } - static void sleep(byte pin = PCINT0) { - PCMSK |= _BV(pin); // Use PB0 as interrupt pin - adc_disable(); + static void sleep(byte pin = -1) { + if (pin >= 0) PCMSK |= _BV(pin); // Use PB0 as interrupt pin + adc_disable(); - sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) - sleep_bod_disable(); - sei(); // Enable interrupts + sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) + sleep_bod_disable(); + sei(); // Enable interrupts - sleep_cpu(); // sleep + sleep_cpu(); // sleep - sleep_disable(); // Clear SE bit - cli(); // Disable interrupts - PCMSK &= ~_BV(pin); // Turn off PB0 as interrupt pin - adc_enable(); + sleep_disable(); // Clear SE bit + cli(); // Disable interrupts + if (pin >= 0) PCMSK &= ~_BV(pin); // Turn off PB0 as interrupt pin + adc_enable(); - sei(); // Enable interrupts + sei(); // Enable interrupts } static void enableWdt(byte time) { diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index 044431d..d0d2ccd 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -18,19 +18,19 @@ struct DhtValues { }; DhtValues readTemp(); -TempSensor tempSensor = TempSensor(TEMP_SENSOR, SENDER); -HumiditySensor humidSensor = HumiditySensor(TEMP_SENSOR, SENDER); +TempSensor tempSensor = TempSensor(TEMP_SENSOR); +HumiditySensor humidSensor = HumiditySensor(TEMP_SENSOR); DHT dht(DHT_PIN, DHT22); volatile int counter = 0; void setup() { - tempSensor.setup(); pinMode(TEMP_POSITIVE, OUTPUT); digitalWrite(TEMP_POSITIVE, HIGH); dht.begin(); + TinySwitch::setup(SENDER); TinyPower::setup(); TinyPower::enableWdt(WDTO_8S); } From 5c5c74485482dd0397f4f79f3a45245b4dad93a9 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 28 Dec 2021 19:06:16 +0100 Subject: [PATCH 5/9] optimise data sending --- libraries/Tiny/Tiny.h | 16 ++++++++-------- temp_sensor/src/temp_sensor.cpp | 27 ++++++++++++++------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/libraries/Tiny/Tiny.h b/libraries/Tiny/Tiny.h index ad7dcca..949bdd0 100644 --- a/libraries/Tiny/Tiny.h +++ b/libraries/Tiny/Tiny.h @@ -1,17 +1,17 @@ #pragma once #define ID(value) (value & 0x1F) -#define STATE(value) ((value & 0x1) << 5) -#define VCC(value) ((value & 0x1FFF) << 6) -#define TEMP(value) (((unsigned long)value & 0x1FF) << 19) -#define HUMIDITY(value) (((unsigned long)value & 0x7F) << 19) +#define VCC(value) ((value & 0x1FFF) << 5) +#define TEMP(value) (((unsigned long)value & 0x2FF) << 18) +#define HUMIDITY(value) (((unsigned long)value & 0x2FF) << 18) +#define STATE(value) ((value & 0x1) << 27) #define TYPE(value) (((unsigned long)value & 0xF) << 28) #define GET_TYPE(value) (((unsigned long)value >> 28) & 0xF) -#define GET_TEMP(value) (((unsigned long)value >> 19) & 0x1FF) -#define GET_HUMIDITY(value) (((unsigned long)value >> 19) & 0x7F) -#define GET_VCC(value) ((value >> 6) & 0x1FFF) -#define GET_STATE(value) ((value >> 5) & 0x1) +#define GET_STATE(value) ((value >> 27) & 0x1) +#define GET_TEMP(value) (((unsigned long)value >> 18) & 0x2FF) +#define GET_HUMIDITY(value) (((unsigned long)value >> 18) & 0x2FF) +#define GET_VCC(value) ((value >> 5) & 0x1FFF) enum SensorType { HUMIDITY = 5, diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index d0d2ccd..5e674f2 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -29,6 +29,7 @@ void setup() { pinMode(TEMP_POSITIVE, OUTPUT); digitalWrite(TEMP_POSITIVE, HIGH); dht.begin(); + delay(2000); TinySwitch::setup(SENDER); TinyPower::setup(); @@ -36,15 +37,14 @@ void setup() { } void loop() { - if (counter % SEND_VCC_INTERVAL == 0) { + if (counter % SEND_INTERVAL == 0) { DhtValues values = readTemp(); - tempSensor.sendTempAndVoltage(values.temperature); - delay(100); - humidSensor.sendHumidity(values.humidity); - counter = 0; - } else if (counter % SEND_INTERVAL == 0) { - DhtValues values = readTemp(); - tempSensor.sendTemp(values.temperature); + if (counter % SEND_VCC_INTERVAL == 0) { + tempSensor.sendTempAndVoltage(values.temperature); + counter = 0; + } else { + tempSensor.sendTemp(values.temperature); + } delay(100); humidSensor.sendHumidity(values.humidity); } @@ -52,18 +52,19 @@ void loop() { } DhtValues readTemp() { - digitalWrite(TEMP_POSITIVE, HIGH); - delay(10); DhtValues dhtValues; dhtValues.temperature = roundf(dht.readTemperature() * 10); dhtValues.humidity = roundf(dht.readHumidity() * 10); digitalWrite(TEMP_POSITIVE, LOW); + pinMode(DHT_PIN, OUTPUT); + digitalWrite(DHT_PIN, LOW); return dhtValues; } -ISR(PCINT0_vect) { -} - ISR(WDT_vect) { counter++; + if (((counter + 1) % SEND_INTERVAL == 0) || ((counter + 1) % SEND_VCC_INTERVAL == 0)) { + pinMode(DHT_PIN, INPUT_PULLUP); + digitalWrite(TEMP_POSITIVE, HIGH); + } } From 16626ec38ff98f18b40acd046bca899e8df47b11 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 30 Dec 2021 20:26:42 +0100 Subject: [PATCH 6/9] retry once if temp reading fails --- temp_sensor/src/temp_sensor.cpp | 57 +++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index 5e674f2..1a527bc 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -4,8 +4,8 @@ #include #include -#define SEND_INTERVAL (int)(5*60/8) -#define SEND_VCC_INTERVAL (int)(60*60/8) +#define SEND_INTERVAL 37 // 37*8s = ~5min +#define SEND_VCC_INTERVAL (SEND_INTERVAL*6) // every half hour // Pins #define TEMP_POSITIVE PIN_B3 @@ -15,8 +15,11 @@ struct DhtValues { int temperature; int humidity; + bool success; }; DhtValues readTemp(); +void turnOnDht(); +void turnOffDht(); TempSensor tempSensor = TempSensor(TEMP_SENSOR); HumiditySensor humidSensor = HumiditySensor(TEMP_SENSOR); @@ -37,34 +40,54 @@ void setup() { } void loop() { - if (counter % SEND_INTERVAL == 0) { + static bool retry = false; + + if (retry || (counter % SEND_INTERVAL == 0)) { DhtValues values = readTemp(); - if (counter % SEND_VCC_INTERVAL == 0) { - tempSensor.sendTempAndVoltage(values.temperature); - counter = 0; - } else { - tempSensor.sendTemp(values.temperature); + if (values.success) { + if (counter % SEND_VCC_INTERVAL == 0) { + tempSensor.sendTempAndVoltage(values.temperature); + counter = 0; + } else { + tempSensor.sendTemp(values.temperature); + } + delay(100); + humidSensor.sendHumidity(values.humidity); + + turnOffDht(); } - delay(100); - humidSensor.sendHumidity(values.humidity); + if (retry && !values.success) { + turnOffDht(); + } + retry = !retry && !values.success; } TinyPower::sleep(); } DhtValues readTemp() { DhtValues dhtValues; - dhtValues.temperature = roundf(dht.readTemperature() * 10); - dhtValues.humidity = roundf(dht.readHumidity() * 10); - digitalWrite(TEMP_POSITIVE, LOW); - pinMode(DHT_PIN, OUTPUT); - digitalWrite(DHT_PIN, LOW); + 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)) { - pinMode(DHT_PIN, INPUT_PULLUP); - digitalWrite(TEMP_POSITIVE, HIGH); + turnOnDht(); } } + +void turnOnDht() { + pinMode(DHT_PIN, INPUT_PULLUP); + digitalWrite(TEMP_POSITIVE, HIGH); +} + +void turnOffDht() { + digitalWrite(TEMP_POSITIVE, LOW); + pinMode(DHT_PIN, OUTPUT); + digitalWrite(DHT_PIN, LOW); +} From 47cf9b57f0fe8b1d911caf437eb70b81238b4f5f Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 1 Jan 2022 16:07:45 +0100 Subject: [PATCH 7/9] move temperature sensor code inside its own class --- .../{TempSensor.h => TemperatureSensor.h} | 4 +- temp_sensor/include/Dht22Sensor.h | 40 +++++++++++++++++++ temp_sensor/include/TempSensor.h | 15 +++++++ temp_sensor/include/Tmp36Sensor.h | 30 ++++++++++++++ temp_sensor/src/temp_sensor.cpp | 34 +++------------- 5 files changed, 92 insertions(+), 31 deletions(-) rename libraries/Tiny/{TempSensor.h => TemperatureSensor.h} (79%) create mode 100644 temp_sensor/include/Dht22Sensor.h create mode 100644 temp_sensor/include/TempSensor.h create mode 100644 temp_sensor/include/Tmp36Sensor.h 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)) { From 4bb1afb8385ea15415adcbc018a35d4d8c73fe14 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 1 Jan 2022 16:42:36 +0100 Subject: [PATCH 8/9] move send logic outside loop --- temp_sensor/include/Dht22Sensor.h | 29 +++++++++++++++++------------ temp_sensor/include/TempSensor.h | 9 ++++----- temp_sensor/include/Tmp36Sensor.h | 12 +++++++++--- temp_sensor/src/temp_sensor.cpp | 16 +++++++--------- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/temp_sensor/include/Dht22Sensor.h b/temp_sensor/include/Dht22Sensor.h index a76f570..b45055c 100644 --- a/temp_sensor/include/Dht22Sensor.h +++ b/temp_sensor/include/Dht22Sensor.h @@ -1,24 +1,24 @@ #pragma once #include -#include +#include #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 { +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); } }; diff --git a/temp_sensor/include/TempSensor.h b/temp_sensor/include/TempSensor.h index 7ac483b..35e70df 100644 --- a/temp_sensor/include/TempSensor.h +++ b/temp_sensor/include/TempSensor.h @@ -1,15 +1,14 @@ #pragma once #include -#include -template -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; }; diff --git a/temp_sensor/include/Tmp36Sensor.h b/temp_sensor/include/Tmp36Sensor.h index 7dea566..f5a8671 100644 --- a/temp_sensor/include/Tmp36Sensor.h +++ b/temp_sensor/include/Tmp36Sensor.h @@ -5,7 +5,8 @@ #define TEMP_POSITIVE PIN_B3 #define TEMP_PIN A2 -class Tmp36Sensor : public TempSensor { +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); } }; diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index 7ac6fa2..7456c86 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -12,7 +12,7 @@ DhtValues readTemp(); void turnOnDht(); void turnOffDht(); -TempSensor &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(); } From 11f410442afa50de830f90a2f94ee7c74b65524b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 1 Jan 2022 16:45:57 +0100 Subject: [PATCH 9/9] move turn on/off logic inside sensor --- temp_sensor/include/Dht22Sensor.h | 11 +++++++++++ temp_sensor/include/TempSensor.h | 2 ++ temp_sensor/src/temp_sensor.cpp | 23 ++++------------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/temp_sensor/include/Dht22Sensor.h b/temp_sensor/include/Dht22Sensor.h index b45055c..2cb3144 100644 --- a/temp_sensor/include/Dht22Sensor.h +++ b/temp_sensor/include/Dht22Sensor.h @@ -42,4 +42,15 @@ public: delay(100); sendHumidity(values.humidity); } + + void turnOnSensor() override { + pinMode(DHT_PIN, INPUT_PULLUP); + digitalWrite(TEMP_POSITIVE, HIGH); + } + + void turnOffSensor() override { + digitalWrite(TEMP_POSITIVE, LOW); + pinMode(DHT_PIN, OUTPUT); + digitalWrite(DHT_PIN, LOW); + } }; diff --git a/temp_sensor/include/TempSensor.h b/temp_sensor/include/TempSensor.h index 35e70df..34c86d0 100644 --- a/temp_sensor/include/TempSensor.h +++ b/temp_sensor/include/TempSensor.h @@ -11,4 +11,6 @@ public: virtual void setup() = 0; virtual bool readTemp() = 0; virtual void sendValues(bool voltage = false) = 0; + virtual void turnOnSensor() {} + virtual void turnOffSensor() {} }; diff --git a/temp_sensor/src/temp_sensor.cpp b/temp_sensor/src/temp_sensor.cpp index 7456c86..10c82c7 100644 --- a/temp_sensor/src/temp_sensor.cpp +++ b/temp_sensor/src/temp_sensor.cpp @@ -8,11 +8,7 @@ // Pins #define SENDER PIN_B2 -DhtValues readTemp(); -void turnOnDht(); -void turnOffDht(); - -TempSensor &tempSensor = *(new Dht22Sensor(TEMP_SENSOR)); +TempSensor& tempSensor = *(new Dht22Sensor(TEMP_SENSOR)); volatile int counter = 0; @@ -38,10 +34,10 @@ void loop() { tempSensor.sendValues(); } - turnOffDht(); + tempSensor.turnOffSensor(); } if (retry && !success) { - turnOffDht(); + tempSensor.turnOffSensor(); } retry = !retry && !success; } @@ -51,17 +47,6 @@ void loop() { ISR(WDT_vect) { counter++; if (((counter + 1) % SEND_INTERVAL == 0) || ((counter + 1) % SEND_VCC_INTERVAL == 0)) { - turnOnDht(); + tempSensor.turnOnSensor(); } } - -void turnOnDht() { - pinMode(DHT_PIN, INPUT_PULLUP); - digitalWrite(TEMP_POSITIVE, HIGH); -} - -void turnOffDht() { - digitalWrite(TEMP_POSITIVE, LOW); - pinMode(DHT_PIN, OUTPUT); - digitalWrite(DHT_PIN, LOW); -}