Browse Source

working separation of dht22 into own class

master
interfisch 3 months ago
parent
commit
24fe2af8b7
  1. 67
      include/sensor_dht22.cpp
  2. 48
      include/sensor_dht22.h
  3. 9
      include/sensordata.h
  4. 32
      platformio.ini
  5. 7
      src/main.cpp

67
src/sensor_dht22.cpp → 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 <Adafruit_Sensor.h> //required for dht library
#define Adafruit_Sensor_H
#endif
#include <DHT.h>
#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 HOMIE_H
#include <Homie.h>
#define HOMIE_H
#ifndef SENSOR_DHT22_temperature_senddelaymax
#define SENSOR_DHT22_temperature_senddelaymax 300000
#endif
#ifndef SENSOR_DHT22_temperature_readdelay
#define SENSOR_DHT22_temperature_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();
};
#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
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();
}
}
}

48
include/sensor_dht22.h

@ -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

9
src/sensordata.h → 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
};
};
#endif

32
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

7
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

Loading…
Cancel
Save