working separation of dht22 into own class

This commit is contained in:
interfisch 2021-11-02 18:51:55 +01:00
parent 2353fea4f6
commit 24fe2af8b7
5 changed files with 106 additions and 57 deletions

View File

@ -1,47 +1,25 @@
// Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed // Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
#ifndef Adafruit_Sensor_H #include "sensor_dht22.h"
#include <Adafruit_Sensor.h> //required for dht library
#define Adafruit_Sensor_H
#endif
#include <DHT.h>
#ifndef SENSORDATA_H #ifndef SENSOR_DHT22_temperature_minchange
#include "sensordata.h" #define SENSOR_DHT22_temperature_minchange 0.2
#define SENSORDATA_H #endif
#ifndef SENSOR_DHT22_temperature_senddelaymax
#define SENSOR_DHT22_temperature_senddelaymax 300000
#endif
#ifndef SENSOR_DHT22_temperature_readdelay
#define SENSOR_DHT22_temperature_readdelay 10000
#endif #endif
#ifndef HOMIE_H #ifndef SENSOR_DHT22_humidity_minchange
#include <Homie.h> #define SENSOR_DHT22_humidity_minchange 2.0
#define HOMIE_H #endif
#ifndef SENSOR_DHT22_humidity_senddelaymax
#define SENSOR_DHT22_humidity_senddelaymax 300000
#endif
#ifndef SENSOR_DHT22_humidity_readdelay
#define SENSOR_DHT22_humidity_readdelay 10000
#endif #endif
class Sensor_DHT22
{
private:
DHT *dht;
HomieNode *sensorNode; //reference to HomieNode
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
void loop_DHT22_temperature();
void loop_DHT22_humidity();
public:
Sensor_DHT22(int pin);
void init();
void setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void setSettings_Humidity(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void loop();
};
Sensor_DHT22::Sensor_DHT22(int pin) Sensor_DHT22::Sensor_DHT22(int pin)
{ {
@ -82,7 +60,7 @@ void Sensor_DHT22::advertise(HomieNode& p_sensorNode)
sensorNode->advertise("humidity"); sensorNode->advertise("humidity");
} }
void Sensor_DHT22::loop() void Sensor_DHT22::sensorloop()
{ {
loop_DHT22_temperature(); loop_DHT22_temperature();
loop_DHT22_humidity(); loop_DHT22_humidity();
@ -110,7 +88,7 @@ void Sensor_DHT22::loop_DHT22_temperature()
#ifndef SENSOR_BMP180 #ifndef SENSOR_BMP180
sensorNode->setProperty("temperature").send(String(d.value)); sensorNode->setProperty("temperature").send(String(d.value));
Homie.getLogger() << "temperature " << ": " << d.vlaue << endl; Homie.getLogger() << "temperature " << ": " << d.value << endl;
#else #else
sensorNode->setProperty("temperature_dht").send(String(d.value)); sensorNode->setProperty("temperature_dht").send(String(d.value));
Homie.getLogger() << "temperature_dht " << ": " << d.value << endl; Homie.getLogger() << "temperature_dht " << ": " << d.value << endl;
@ -149,3 +127,4 @@ void Sensor_DHT22::loop_DHT22_humidity()
d.lastsent=millis(); d.lastsent=millis();
} }
} }

48
include/sensor_dht22.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef SENSOR_DHT22_H
#define SENSOR_DHT22_H
#ifndef Adafruit_Sensor_H
#include <Adafruit_Sensor.h> //required for dht library
#define Adafruit_Sensor_H
#endif
#include <DHT.h>
#include "sensordata.h"
#ifndef HOMIE_H
#include <Homie.h>
#define HOMIE_H
#endif
class Sensor_DHT22
{
private:
DHT *dht;
HomieNode *sensorNode; //reference to HomieNode
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
public:
Sensor_DHT22(int pin);
void loop_DHT22_temperature();
void loop_DHT22_humidity();
void init();
void setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void setSettings_Humidity(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

View File

@ -1,4 +1,7 @@
#ifndef SENSORDATA_H
#define SENSORDATA_H
struct sensordata struct sensordata
{ {
unsigned long lastreadtime=0; unsigned long lastreadtime=0;
@ -9,3 +12,7 @@ struct sensordata
unsigned long lastsent=0; unsigned long lastsent=0;
unsigned long senddelaymax=1000*60*5; //maximum time until current value is send unsigned long senddelaymax=1000*60*5; //maximum time until current value is send
}; };
#endif

View File

@ -8,10 +8,10 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
# Flash upload with platformio run -t upload --environment sensorespx # Flash upload with: pio run -t upload --environment sensorespx
[platformio] [platformio]
#For Config upload comment in data_dir line and flash with platformio run -t uploadfs --environment sensorespx #For Config upload comment in data_dir line and flash with: pio run -t uploadfs --environment sensorespx
#data_dir=data_sensoresp0 #data_dir=data_sensoresp0
#data_dir=data_sensoresp1 #data_dir=data_sensoresp1
#data_dir=data_sensoresp2 #data_dir=data_sensoresp2
@ -19,6 +19,7 @@
#data_dir=data_sensoresp4 #data_dir=data_sensoresp4
#data_dir=data_sensoresp5 #data_dir=data_sensoresp5
#data_dir=data_sensoresp3dprinter #data_dir=data_sensoresp3dprinter
data_dir=data_sensoresptest
#Outdoor #Outdoor
@ -66,7 +67,28 @@ build_flags =
lib_deps = lib_deps =
Adafruit BMP085 Library@1.1.0 Adafruit BMP085 Library@1.1.0
https://github.com/adafruit/Adafruit_TCS34725 https://github.com/adafruit/Adafruit_TCS34725#1.3.5
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
Homie@3.0.0
#Test
[env:sensoresptest]
platform = espressif8266 @ 2.5.0
board = d1_mini
framework = arduino
monitor_port = /dev/ttyUSB0
monitor_speed = 115200
build_flags =
-D SENSOR_DHT22
-D SENSOR_DHT22_PIN=D7
-D SENSOR_DHT22_temperature_minchange=0.2
-D SENSOR_DHT22_humidity_minchange=2.0
lib_deps =
DHT sensor library@1.3.10
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
Homie@3.0.0 Homie@3.0.0
@ -83,11 +105,7 @@ build_flags =
-D SENSOR_DHT22 -D SENSOR_DHT22
-D SENSOR_DHT22_PIN=D7 -D SENSOR_DHT22_PIN=D7
-D SENSOR_DHT22_temperature_minchange=0.2 -D SENSOR_DHT22_temperature_minchange=0.2
-D SENSOR_DHT22_temperature_senddelaymax=300000
-D SENSOR_DHT22_temperature_readdelay=10000
-D SENSOR_DHT22_humidity_minchange=2.0 -D SENSOR_DHT22_humidity_minchange=2.0
-D SENSOR_DHT22_humidity_senddelaymax=300000
-D SENSOR_DHT22_humidity_readdelay=10000
-D SENSOR_BMP180 -D SENSOR_BMP180
-D dataBMP180_temperature_minchange=0.2 -D dataBMP180_temperature_minchange=0.2

View File

@ -21,10 +21,7 @@
#define STATUSNODE #define STATUSNODE
#ifndef SENSORDATA_H
#include "sensordata.h" #include "sensordata.h"
#define SENSORDATA_H
#endif
@ -1286,7 +1283,7 @@ void loopHandler() {
checkESPStatus(); checkESPStatus();
#ifdef SENSOR_DHT22 #ifdef SENSOR_DHT22
sensor_dht22.loop(); sensor_dht22.sensorloop();
#endif #endif
#ifdef SENSOR_BMP180 #ifdef SENSOR_BMP180