From 24fe2af8b76cc88b2c7deb63bf67e345861953fb Mon Sep 17 00:00:00 2001 From: Fisch Date: Tue, 2 Nov 2021 18:51:55 +0100 Subject: [PATCH] working separation of dht22 into own class --- {src => include}/sensor_dht22.cpp | 67 +++++++++++-------------------- include/sensor_dht22.h | 48 ++++++++++++++++++++++ {src => include}/sensordata.h | 9 ++++- platformio.ini | 32 +++++++++++---- src/main.cpp | 7 +--- 5 files changed, 106 insertions(+), 57 deletions(-) rename {src => include}/sensor_dht22.cpp (72%) create mode 100644 include/sensor_dht22.h rename {src => include}/sensordata.h (86%) diff --git a/src/sensor_dht22.cpp b/include/sensor_dht22.cpp similarity index 72% rename from src/sensor_dht22.cpp rename to include/sensor_dht22.cpp index aee9ad5..0526f2f 100644 --- a/src/sensor_dht22.cpp +++ b/include/sensor_dht22.cpp @@ -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 -#ifndef Adafruit_Sensor_H - #include //required for dht library - #define Adafruit_Sensor_H -#endif -#include +#include "sensor_dht22.h" -#ifndef SENSORDATA_H -#include "sensordata.h" -#define SENSORDATA_H +#ifndef SENSOR_DHT22_temperature_minchange + #define SENSOR_DHT22_temperature_minchange 0.2 +#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 -#ifndef HOMIE_H - #include - #define HOMIE_H +#ifndef SENSOR_DHT22_humidity_minchange + #define SENSOR_DHT22_humidity_minchange 2.0 +#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 - - - - - -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) { @@ -82,10 +60,10 @@ void Sensor_DHT22::advertise(HomieNode& p_sensorNode) sensorNode->advertise("humidity"); } -void Sensor_DHT22::loop() +void Sensor_DHT22::sensorloop() { - loop_DHT22_temperature(); - loop_DHT22_humidity(); + loop_DHT22_temperature(); + loop_DHT22_humidity(); } @@ -110,7 +88,7 @@ void Sensor_DHT22::loop_DHT22_temperature() #ifndef SENSOR_BMP180 sensorNode->setProperty("temperature").send(String(d.value)); - Homie.getLogger() << "temperature " << ": " << d.vlaue << endl; + Homie.getLogger() << "temperature " << ": " << d.value << endl; #else sensorNode->setProperty("temperature_dht").send(String(d.value)); Homie.getLogger() << "temperature_dht " << ": " << d.value << endl; @@ -148,4 +126,5 @@ void Sensor_DHT22::loop_DHT22_humidity() } d.lastsent=millis(); } -} \ No newline at end of file +} + diff --git a/include/sensor_dht22.h b/include/sensor_dht22.h new file mode 100644 index 0000000..3631e12 --- /dev/null +++ b/include/sensor_dht22.h @@ -0,0 +1,48 @@ +#ifndef SENSOR_DHT22_H +#define SENSOR_DHT22_H + +#ifndef Adafruit_Sensor_H + #include //required for dht library + #define Adafruit_Sensor_H +#endif +#include + + +#include "sensordata.h" + +#ifndef HOMIE_H + #include + #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 + diff --git a/src/sensordata.h b/include/sensordata.h similarity index 86% rename from src/sensordata.h rename to include/sensordata.h index 8dcf4aa..8bd28f3 100644 --- a/src/sensordata.h +++ b/include/sensordata.h @@ -1,4 +1,7 @@ +#ifndef SENSORDATA_H +#define SENSORDATA_H + struct sensordata { unsigned long lastreadtime=0; @@ -8,4 +11,8 @@ struct sensordata float value=0; unsigned long lastsent=0; unsigned long senddelaymax=1000*60*5; //maximum time until current value is send -}; \ No newline at end of file +}; + + +#endif + diff --git a/platformio.ini b/platformio.ini index 05d7337..ad4fa37 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,10 +8,10 @@ ; Please visit documentation for the other options and examples ; 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] -#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_sensoresp1 #data_dir=data_sensoresp2 @@ -19,6 +19,7 @@ #data_dir=data_sensoresp4 #data_dir=data_sensoresp5 #data_dir=data_sensoresp3dprinter +data_dir=data_sensoresptest #Outdoor @@ -66,7 +67,28 @@ build_flags = lib_deps = 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 Homie@3.0.0 @@ -83,11 +105,7 @@ build_flags = -D SENSOR_DHT22 -D SENSOR_DHT22_PIN=D7 -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_senddelaymax=300000 - -D SENSOR_DHT22_humidity_readdelay=10000 -D SENSOR_BMP180 -D dataBMP180_temperature_minchange=0.2 diff --git a/src/main.cpp b/src/main.cpp index 5b1e4d0..3105b74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,10 +21,7 @@ #define STATUSNODE -#ifndef SENSORDATA_H - #include "sensordata.h" - #define SENSORDATA_H -#endif +#include "sensordata.h" @@ -1286,7 +1283,7 @@ void loopHandler() { checkESPStatus(); #ifdef SENSOR_DHT22 - sensor_dht22.loop(); + sensor_dht22.sensorloop(); #endif #ifdef SENSOR_BMP180