Merge branch 'dht22' into temp_sensor
This commit is contained in:
commit
2d4dc652c2
56
temp_sensor/include/Dht22Sensor.h
Normal file
56
temp_sensor/include/Dht22Sensor.h
Normal 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);
|
||||
}
|
||||
};
|
||||
@ -9,5 +9,8 @@ public:
|
||||
}
|
||||
|
||||
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() {}
|
||||
};
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#define TEMP_PIN A2
|
||||
|
||||
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,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
|
||||
|
||||
@ -1,23 +1,25 @@
|
||||
#include <Arduino.h>
|
||||
#include "Tmp36Sensor.h"
|
||||
#include <TinyPower.h>
|
||||
#include <SoftwareSerial_Tiny.h>
|
||||
#include "Tmp36Sensor.h"
|
||||
#include "Dht22Sensor.h"
|
||||
|
||||
#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 SENDER PIN_B2
|
||||
|
||||
int readTemp();
|
||||
|
||||
TempSensor &sensor = *(new Tmp36Sensor(TEMP_SENSOR));
|
||||
#if !DHT
|
||||
TempSensor &tempSensor = *(new Tmp36Sensor(TEMP_SENSOR));
|
||||
#else
|
||||
TempSensor& tempSensor = *(new Dht22Sensor(TEMP_SENSOR));
|
||||
#endif
|
||||
|
||||
volatile int counter = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
sensor.setup();
|
||||
tempSensor.setup();
|
||||
|
||||
TinySwitch::setup(SENDER);
|
||||
TinyPower::setup();
|
||||
@ -25,18 +27,31 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (counter % SEND_VCC_INTERVAL == 0) {
|
||||
sensor.sendTempAndVoltage(sensor.readTemp());
|
||||
counter = 0;
|
||||
} else if (counter % SEND_INTERVAL == 0) {
|
||||
sensor.sendTemp(sensor.readTemp());
|
||||
static bool retry = false;
|
||||
|
||||
if (retry || (counter % SEND_INTERVAL == 0)) {
|
||||
bool success = tempSensor.readTemp();
|
||||
if (success) {
|
||||
if (counter % SEND_VCC_INTERVAL == 0) {
|
||||
tempSensor.sendValues(true);
|
||||
counter = 0;
|
||||
} else {
|
||||
tempSensor.sendValues();
|
||||
}
|
||||
|
||||
tempSensor.turnOffSensor();
|
||||
}
|
||||
if (retry && !success) {
|
||||
tempSensor.turnOffSensor();
|
||||
}
|
||||
retry = !retry && !success;
|
||||
}
|
||||
TinyPower::sleep();
|
||||
}
|
||||
|
||||
ISR(PCINT0_vect) {
|
||||
}
|
||||
|
||||
ISR(WDT_vect) {
|
||||
counter++;
|
||||
if (((counter + 1) % SEND_INTERVAL == 0) || ((counter + 1) % SEND_VCC_INTERVAL == 0)) {
|
||||
tempSensor.turnOnSensor();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user