Merge branch 'dht22' into temp_sensor

This commit is contained in:
Nicu Hodos 2022-01-01 16:52:23 +01:00
commit 13fa62a9c3
5 changed files with 108 additions and 28 deletions

View File

@ -0,0 +1,56 @@
#pragma once
#include <DHT.h>
#include <HumiditySensor.h>
#include "TempSensor.h"
#define TEMP_POSITIVE PIN_B3
#define DHT_PIN PIN_B4
struct DhtValues {
int temperature;
int humidity;
};
class Dht22Sensor : public TempSensor, public HumiditySensor {
DHT dht = DHT(DHT_PIN, DHT22);
DhtValues values;
public:
Dht22Sensor(short id) :
TempSensor(id), HumiditySensor(id) {
}
void setup() override {
pinMode(TEMP_POSITIVE, OUTPUT);
digitalWrite(TEMP_POSITIVE, HIGH);
dht.begin();
delay(2000);
}
bool readTemp() override {
float temp = dht.readTemperature();
float humid = dht.readHumidity();
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);
}
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);
}
};

View File

@ -9,5 +9,8 @@ public:
} }
virtual void setup() = 0; virtual void setup() = 0;
virtual int readTemp() = 0; virtual bool readTemp() = 0;
virtual void sendValues(bool voltage = false) = 0;
virtual void turnOnSensor() {}
virtual void turnOffSensor() {}
}; };

View File

@ -6,6 +6,7 @@
#define TEMP_PIN A2 #define TEMP_PIN A2
class Tmp36Sensor : public TempSensor { 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,15 +12,15 @@
platform = atmelavr platform = atmelavr
board = attiny85 board = attiny85
framework = arduino framework = arduino
lib_deps =
adafruit/Adafruit Unified Sensor @ ^1.1.4
adafruit/DHT sensor library@^1.4.3
lib_extra_dirs = lib_extra_dirs =
../libraries ../libraries
~/Arduino/libraries ~/Arduino/libraries
upload_protocol = stk500v1 upload_protocol = stk500v1
; each flag in a new line
upload_flags = upload_flags =
-P$UPLOAD_PORT -P$UPLOAD_PORT
-b$UPLOAD_SPEED -b$UPLOAD_SPEED
; edit these lines
upload_port = /dev/ttyACM0 upload_port = /dev/ttyACM0
upload_speed = 19200 upload_speed = 19200

View File

@ -1,23 +1,25 @@
#include <Arduino.h> #include <Arduino.h>
#include "Tmp36Sensor.h"
#include <TinyPower.h> #include <TinyPower.h>
#include <SoftwareSerial_Tiny.h> #include "Tmp36Sensor.h"
#include "Dht22Sensor.h"
#define SEND_INTERVAL (int)(5*60/8) #define SEND_INTERVAL 37 // 37*8s = ~5min
#define SEND_VCC_INTERVAL (int)(60*60/8) #define SEND_VCC_INTERVAL (SEND_INTERVAL*6) // every half hour
// Pins // Pins
#define SENDER PIN_B2 #define SENDER PIN_B2
int readTemp(); #if !DHT
TempSensor &tempSensor = *(new Tmp36Sensor(TEMP_SENSOR));
TempSensor &sensor = *(new Tmp36Sensor(TEMP_SENSOR)); #else
TempSensor& tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
#endif
volatile int counter = 0; volatile int counter = 0;
void setup() { void setup() {
sensor.setup(); tempSensor.setup();
TinySwitch::setup(SENDER); TinySwitch::setup(SENDER);
TinyPower::setup(); TinyPower::setup();
@ -25,18 +27,31 @@ void setup() {
} }
void loop() { void loop() {
static bool retry = false;
if (retry || (counter % SEND_INTERVAL == 0)) {
bool success = tempSensor.readTemp();
if (success) {
if (counter % SEND_VCC_INTERVAL == 0) { if (counter % SEND_VCC_INTERVAL == 0) {
sensor.sendTempAndVoltage(sensor.readTemp()); tempSensor.sendValues(true);
counter = 0; counter = 0;
} else if (counter % SEND_INTERVAL == 0) { } else {
sensor.sendTemp(sensor.readTemp()); tempSensor.sendValues();
}
tempSensor.turnOffSensor();
}
if (retry && !success) {
tempSensor.turnOffSensor();
}
retry = !retry && !success;
} }
TinyPower::sleep(); TinyPower::sleep();
} }
ISR(PCINT0_vect) {
}
ISR(WDT_vect) { ISR(WDT_vect) {
counter++; counter++;
if (((counter + 1) % SEND_INTERVAL == 0) || ((counter + 1) % SEND_VCC_INTERVAL == 0)) {
tempSensor.turnOnSensor();
}
} }