move htu21d to class
This commit is contained in:
parent
ac81c74dbd
commit
05907eb59d
|
@ -0,0 +1,105 @@
|
||||||
|
//Connect SCL to D1, SDA to D2, GND and 3v3
|
||||||
|
#include "sensor_htu21d.h"
|
||||||
|
#include <Adafruit_HTU21DF.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Sensor_HTU21D::Sensor_HTU21D()
|
||||||
|
{
|
||||||
|
Adafruit_HTU21DF htu;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensor_HTU21D::init() //Things to be done during setup()
|
||||||
|
{
|
||||||
|
Serial.println("initializing HTU21D");
|
||||||
|
if (!htu->begin()) {
|
||||||
|
Serial.println("#ERROR: HTU21D init fail\n");
|
||||||
|
}else{
|
||||||
|
init_ok=true; //stays false if init failed, sensor will not be read in loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Also called during setup()
|
||||||
|
void Sensor_HTU21D::setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||||
|
{
|
||||||
|
data_temperature.minchange=minchange;
|
||||||
|
data_temperature.senddelaymax=senddelaymax;
|
||||||
|
data_temperature.readdelay=readdelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Also called during setup()
|
||||||
|
void Sensor_HTU21D::setSettings_Humidity(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||||
|
{
|
||||||
|
data_humidity.minchange=minchange;
|
||||||
|
data_humidity.senddelaymax=senddelaymax;
|
||||||
|
data_humidity.readdelay=readdelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Called during setup
|
||||||
|
void Sensor_HTU21D::advertise(HomieNode& p_sensorNode)
|
||||||
|
{
|
||||||
|
sensorNode = &p_sensorNode;
|
||||||
|
sensorNode->advertise("temperature_htu");
|
||||||
|
sensorNode->advertise("humidity_htu");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensor_HTU21D::sensorloop()
|
||||||
|
{
|
||||||
|
if (init_ok) {
|
||||||
|
loop_HTU21D_temperature();
|
||||||
|
loop_HTU21D_humidity();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Sensor_HTU21D::loop_HTU21D_temperature()
|
||||||
|
{
|
||||||
|
sensordata &d=data_temperature;
|
||||||
|
bool _changed=false;
|
||||||
|
|
||||||
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||||
|
d.value = htu->readTemperature();
|
||||||
|
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
||||||
|
_changed=true;
|
||||||
|
}
|
||||||
|
d.lastreadtime=millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||||
|
Serial.print("Sending HTU21D_temperature. reason=");
|
||||||
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||||
|
|
||||||
|
if (!(isnan(d.value) == 1)){ //success
|
||||||
|
sensorNode->setProperty("temperature_htu").send(String(d.value));
|
||||||
|
Homie.getLogger() << "temperature_htu " << ": " << d.value << endl;
|
||||||
|
d.lastsentvalue=d.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
d.lastsent=millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Sensor_HTU21D::loop_HTU21D_humidity()
|
||||||
|
{
|
||||||
|
sensordata &d=data_humidity;
|
||||||
|
bool _changed=false;
|
||||||
|
|
||||||
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||||
|
d.value = htu->readHumidity();
|
||||||
|
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
||||||
|
_changed=true;
|
||||||
|
}
|
||||||
|
d.lastreadtime=millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||||
|
Serial.print("Sending HTU21D_humidity. reason=");
|
||||||
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||||
|
|
||||||
|
if (!(isnan(d.value) == 1)){ //success
|
||||||
|
sensorNode->setProperty("humidity_htu").send(String(d.value));
|
||||||
|
Homie.getLogger() << "humidity_htu " << ": " << d.value << endl;
|
||||||
|
d.lastsentvalue=d.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
d.lastsent=millis();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
#ifndef SENSOR_HTU21D_H
|
||||||
|
#define SENSOR_HTU21D_H
|
||||||
|
|
||||||
|
#include "sensordata.h"
|
||||||
|
#include <Homie.h>
|
||||||
|
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_HTU21DF.h>
|
||||||
|
|
||||||
|
|
||||||
|
class Sensor_HTU21D
|
||||||
|
{
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Adafruit_HTU21DF *htu;
|
||||||
|
HomieNode *sensorNode; //reference to HomieNode
|
||||||
|
|
||||||
|
struct sensordata data_temperature; //struct values are changed in setup()
|
||||||
|
struct sensordata data_humidity; //struct values are changed in setup()
|
||||||
|
|
||||||
|
bool init_ok;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Sensor_HTU21D();
|
||||||
|
|
||||||
|
void loop_HTU21D_temperature();
|
||||||
|
void loop_HTU21D_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
|
||||||
|
|
|
@ -92,11 +92,20 @@ build_flags =
|
||||||
-D SENSOR_BMP180_temperature_minchange=0.2
|
-D SENSOR_BMP180_temperature_minchange=0.2
|
||||||
-D SENSOR_BMP180_pressure_minchange=0.5
|
-D SENSOR_BMP180_pressure_minchange=0.5
|
||||||
|
|
||||||
|
-D SENSOR_HTU21D
|
||||||
|
-D dataHTU21D_temperature_minchange=0.2
|
||||||
|
-D dataHTU21D_temperature_senddelaymax=1000*60*20
|
||||||
|
-D dataHTU21D_humidity_minchange=1.0
|
||||||
|
-D dataHTU21D_humidity_senddelaymax=1000*60*30
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
DHT sensor library@1.3.10
|
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
|
||||||
Adafruit BMP085 Library@1.1.0
|
Adafruit BMP085 Library@1.1.0
|
||||||
|
adafruit/Adafruit HTU21DF Library @ ^1.0.5
|
||||||
|
|
||||||
#Arbeitszimmer
|
#Arbeitszimmer
|
||||||
[env:sensoresp1]
|
[env:sensoresp1]
|
||||||
|
@ -296,7 +305,7 @@ build_flags =
|
||||||
-D dataHTU21D_temperature_minchange=0.2
|
-D dataHTU21D_temperature_minchange=0.2
|
||||||
-D dataHTU21D_temperature_senddelaymax=1000*60*20
|
-D dataHTU21D_temperature_senddelaymax=1000*60*20
|
||||||
-D dataHTU21D_humidity_minchange=1.0
|
-D dataHTU21D_humidity_minchange=1.0
|
||||||
-D dataHTU21D_humidity_senddelaymax=1000*60*10
|
-D dataHTU21D_humidity_senddelaymax=1000*60*30
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
135
src/main.cpp
135
src/main.cpp
|
@ -33,7 +33,7 @@
|
||||||
#define SENSOR_DHT22_temperature_minchange 0.2
|
#define SENSOR_DHT22_temperature_minchange 0.2
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_DHT22_temperature_senddelaymax
|
#ifndef SENSOR_DHT22_temperature_senddelaymax
|
||||||
#define SENSOR_DHT22_temperature_senddelaymax 300000
|
#define SENSOR_DHT22_temperature_senddelaymax 5*60*1000
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_DHT22_temperature_readdelay
|
#ifndef SENSOR_DHT22_temperature_readdelay
|
||||||
#define SENSOR_DHT22_temperature_readdelay 10000
|
#define SENSOR_DHT22_temperature_readdelay 10000
|
||||||
|
@ -43,7 +43,7 @@
|
||||||
#define SENSOR_DHT22_humidity_minchange 2.0
|
#define SENSOR_DHT22_humidity_minchange 2.0
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_DHT22_humidity_senddelaymax
|
#ifndef SENSOR_DHT22_humidity_senddelaymax
|
||||||
#define SENSOR_DHT22_humidity_senddelaymax 300000
|
#define SENSOR_DHT22_humidity_senddelaymax 10*60*1000
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_DHT22_humidity_readdelay
|
#ifndef SENSOR_DHT22_humidity_readdelay
|
||||||
#define SENSOR_DHT22_humidity_readdelay 10000
|
#define SENSOR_DHT22_humidity_readdelay 10000
|
||||||
|
@ -59,17 +59,17 @@
|
||||||
#define SENSOR_BMP180_temperature_minchange 0.2
|
#define SENSOR_BMP180_temperature_minchange 0.2
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_BMP180_temperature_senddelaymax
|
#ifndef SENSOR_BMP180_temperature_senddelaymax
|
||||||
#define SENSOR_BMP180_temperature_senddelaymax 300000
|
#define SENSOR_BMP180_temperature_senddelaymax 5*60*1000
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_BMP180_temperature_readdelay
|
#ifndef SENSOR_BMP180_temperature_readdelay
|
||||||
#define SENSOR_BMP180_temperature_readdelay 10000
|
#define SENSOR_BMP180_temperature_readdelay 10000
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef SENSOR_BMP180_pressure_minchange
|
#ifndef SENSOR_BMP180_pressure_minchange
|
||||||
#define SENSOR_BMP180_pressure_minchange 2.0
|
#define SENSOR_BMP180_pressure_minchange 0.5
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_BMP180_pressure_senddelaymax
|
#ifndef SENSOR_BMP180_pressure_senddelaymax
|
||||||
#define SENSOR_BMP180_pressure_senddelaymax 300000
|
#define SENSOR_BMP180_pressure_senddelaymax 30*60*1000
|
||||||
#endif
|
#endif
|
||||||
#ifndef SENSOR_BMP180_pressure_readdelay
|
#ifndef SENSOR_BMP180_pressure_readdelay
|
||||||
#define SENSOR_BMP180_pressure_readdelay 10000
|
#define SENSOR_BMP180_pressure_readdelay 10000
|
||||||
|
@ -77,18 +77,29 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_HTU21D
|
#ifdef SENSOR_HTU21D
|
||||||
//Connect SCL to D1, SDA to D2, GND and 3v3
|
#include "sensor_htu21d.cpp"
|
||||||
#ifndef WIRE_H
|
Sensor_HTU21D sensor_htu21d;
|
||||||
#include <Wire.h>
|
|
||||||
#define WIRE_H
|
#ifndef SENSOR_HTU21D_temperature_minchange
|
||||||
|
#define SENSOR_HTU21D_temperature_minchange 0.2
|
||||||
#endif
|
#endif
|
||||||
#include "Adafruit_HTU21DF.h"
|
#ifndef SENSOR_HTU21D_temperature_senddelaymax
|
||||||
bool htu21dinit_ok=false;
|
#define SENSOR_HTU21D_temperature_senddelaymax 300000
|
||||||
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
|
#endif
|
||||||
struct sensordata dataHTU21D_temperature; //struct values are changed in setup()
|
#ifndef SENSOR_HTU21D_temperature_readdelay
|
||||||
float value_temperatureHTU=0;
|
#define SENSOR_HTU21D_temperature_readdelay 10000
|
||||||
struct sensordata dataHTU21D_humidity; //struct values are changed in setup()
|
#endif
|
||||||
float value_humidityHTU=0;
|
|
||||||
|
#ifndef SENSOR_HTU21D_humidity_minchange
|
||||||
|
#define SENSOR_HTU21D_humidity_minchange 2.0
|
||||||
|
#endif
|
||||||
|
#ifndef SENSOR_HTU21D_humidity_senddelaymax
|
||||||
|
#define SENSOR_HTU21D_humidity_senddelaymax 10*60*1000
|
||||||
|
#endif
|
||||||
|
#ifndef SENSOR_HTU21D_humidity_readdelay
|
||||||
|
#define SENSOR_HTU21D_humidity_readdelay 10000
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_HS1101
|
#ifdef SENSOR_HS1101
|
||||||
|
@ -341,24 +352,9 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_HTU21D
|
#ifdef SENSOR_HTU21D
|
||||||
Serial.println("initializing htu21d");
|
sensor_htu21d.init();
|
||||||
if (!htu.begin()) {
|
sensor_htu21d.setSettings_Temperature(SENSOR_HTU21D_temperature_minchange,SENSOR_HTU21D_temperature_senddelaymax,SENSOR_HTU21D_temperature_readdelay);
|
||||||
Serial.println("#ERROR: HTU21D init fail\n");
|
sensor_htu21d.setSettings_Humidity(SENSOR_HTU21D_humidity_minchange,SENSOR_HTU21D_humidity_senddelaymax,SENSOR_HTU21D_humidity_readdelay);
|
||||||
}else{
|
|
||||||
htu21dinit_ok=true; //stays false if init failed, sensor will not be read in loop
|
|
||||||
}
|
|
||||||
#ifdef dataHTU21D_temperature_minchange
|
|
||||||
dataHTU21D_temperature.minchange=dataHTU21D_temperature_minchange;
|
|
||||||
#endif
|
|
||||||
#ifdef dataHTU21D_temperature_senddelaymax
|
|
||||||
dataHTU21D_temperature.senddelaymax=dataHTU21D_temperature_senddelaymax;
|
|
||||||
#endif
|
|
||||||
#ifdef dataHTU21D_humidity_minchange
|
|
||||||
dataHTU21D_humidity.minchange=dataHTU21D_humidity_minchange;
|
|
||||||
#endif
|
|
||||||
#ifdef dataHTU21D_humidity_senddelaymax
|
|
||||||
dataHTU21D_humidity.senddelaymax=dataHTU21D_humidity_senddelaymax;
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -557,6 +553,10 @@ void setup() {
|
||||||
sensor_bmp180.advertise(sensorNode);
|
sensor_bmp180.advertise(sensorNode);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef SENSOR_HTU21D
|
||||||
|
sensor_htu21d.advertise(sensorNode);
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_HS1101
|
#ifdef SENSOR_HS1101
|
||||||
#ifdef SENSOR_DHT22
|
#ifdef SENSOR_DHT22
|
||||||
sensorNode.advertise("humidity_hs1101");
|
sensorNode.advertise("humidity_hs1101");
|
||||||
|
@ -590,10 +590,7 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_HTU21D
|
|
||||||
sensorNode.advertise("temperature_htu");
|
|
||||||
sensorNode.advertise("humidity_htu");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SENSOR_MHZ19
|
#ifdef SENSOR_MHZ19
|
||||||
sensorNode.advertise("co2");
|
sensorNode.advertise("co2");
|
||||||
|
@ -645,63 +642,6 @@ void loop() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_HTU21D
|
|
||||||
void loop_HTU21D_temperature()
|
|
||||||
{
|
|
||||||
sensordata &d=dataHTU21D_temperature;
|
|
||||||
bool _changed=false;
|
|
||||||
|
|
||||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
||||||
value_temperatureHTU = htu.readTemperature();
|
|
||||||
if (fabs(d.lastsentvalue-value_temperatureHTU)>=d.minchange){
|
|
||||||
_changed=true;
|
|
||||||
}
|
|
||||||
d.lastreadtime=millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
||||||
Serial.print("Sending HTU21D_temperature. reason=");
|
|
||||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
||||||
checkESPStatus();
|
|
||||||
|
|
||||||
if (!(isnan(value_temperatureHTU) == 1)){ //success
|
|
||||||
sensorNode.setProperty("temperature_htu").send(String(value_temperatureHTU));
|
|
||||||
Homie.getLogger() << "temperature_htu " << ": " << value_temperatureHTU << endl;
|
|
||||||
d.lastsentvalue=value_temperatureHTU;
|
|
||||||
}
|
|
||||||
|
|
||||||
d.lastsent=millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop_HTU21D_humidity()
|
|
||||||
{
|
|
||||||
sensordata &d=dataHTU21D_humidity;
|
|
||||||
bool _changed=false;
|
|
||||||
|
|
||||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
||||||
value_humidityHTU = htu.readHumidity();
|
|
||||||
if (fabs(d.lastsentvalue-value_humidityHTU)>=d.minchange){
|
|
||||||
_changed=true;
|
|
||||||
}
|
|
||||||
d.lastreadtime=millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
||||||
Serial.print("Sending HTU21D_humidity. reason=");
|
|
||||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
||||||
checkESPStatus();
|
|
||||||
|
|
||||||
if (!(isnan(value_humidityHTU) == 1)){ //success
|
|
||||||
sensorNode.setProperty("humidity_htu").send(String(value_humidityHTU));
|
|
||||||
Homie.getLogger() << "humidity_htu " << ": " << value_humidityHTU << endl;
|
|
||||||
d.lastsentvalue=value_humidityHTU;
|
|
||||||
}
|
|
||||||
|
|
||||||
d.lastsent=millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SENSOR_HS1101
|
#ifdef SENSOR_HS1101
|
||||||
void loop_HS1101() {
|
void loop_HS1101() {
|
||||||
|
@ -1247,10 +1187,7 @@ void loopHandler() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_HTU21D
|
#ifdef SENSOR_HTU21D
|
||||||
if (htu21dinit_ok) {
|
sensor_htu21d.sensorloop();
|
||||||
loop_HTU21D_temperature();
|
|
||||||
loop_HTU21D_humidity();
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_HS1101
|
#ifdef SENSOR_HS1101
|
||||||
|
|
Loading…
Reference in New Issue