41 lines
871 B
C++
41 lines
871 B
C++
#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;
|
|
}
|
|
};
|