Browse Source

move htu21d to class

master
interfisch 3 months ago
parent
commit
05907eb59d
  1. 105
      include/sensor_htu21d.cpp
  2. 39
      include/sensor_htu21d.h
  3. 11
      platformio.ini
  4. 135
      src/main.cpp

105
include/sensor_htu21d.cpp

@ -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();
}
}

39
include/sensor_htu21d.h

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

11
platformio.ini

@ -92,11 +92,20 @@ build_flags =
-D SENSOR_BMP180_temperature_minchange=0.2
-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 =
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
Adafruit BMP085 Library@1.1.0
adafruit/Adafruit HTU21DF Library @ ^1.0.5
#Arbeitszimmer
[env:sensoresp1]
@ -296,7 +305,7 @@ build_flags =
-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*10
-D dataHTU21D_humidity_senddelaymax=1000*60*30

135
src/main.cpp

@ -33,7 +33,7 @@
#define SENSOR_DHT22_temperature_minchange 0.2
#endif
#ifndef SENSOR_DHT22_temperature_senddelaymax
#define SENSOR_DHT22_temperature_senddelaymax 300000
#define SENSOR_DHT22_temperature_senddelaymax 5*60*1000
#endif
#ifndef SENSOR_DHT22_temperature_readdelay
#define SENSOR_DHT22_temperature_readdelay 10000
@ -43,7 +43,7 @@
#define SENSOR_DHT22_humidity_minchange 2.0
#endif
#ifndef SENSOR_DHT22_humidity_senddelaymax
#define SENSOR_DHT22_humidity_senddelaymax 300000
#define SENSOR_DHT22_humidity_senddelaymax 10*60*1000
#endif
#ifndef SENSOR_DHT22_humidity_readdelay
#define SENSOR_DHT22_humidity_readdelay 10000
@ -59,17 +59,17 @@
#define SENSOR_BMP180_temperature_minchange 0.2
#endif
#ifndef SENSOR_BMP180_temperature_senddelaymax
#define SENSOR_BMP180_temperature_senddelaymax 300000
#define SENSOR_BMP180_temperature_senddelaymax 5*60*1000
#endif
#ifndef SENSOR_BMP180_temperature_readdelay
#define SENSOR_BMP180_temperature_readdelay 10000
#endif
#ifndef SENSOR_BMP180_pressure_minchange
#define SENSOR_BMP180_pressure_minchange 2.0
#define SENSOR_BMP180_pressure_minchange 0.5
#endif
#ifndef SENSOR_BMP180_pressure_senddelaymax
#define SENSOR_BMP180_pressure_senddelaymax 300000
#define SENSOR_BMP180_pressure_senddelaymax 30*60*1000
#endif
#ifndef SENSOR_BMP180_pressure_readdelay
#define SENSOR_BMP180_pressure_readdelay 10000
@ -77,18 +77,29 @@
#endif
#ifdef SENSOR_HTU21D
//Connect SCL to D1, SDA to D2, GND and 3v3
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#include "sensor_htu21d.cpp"
Sensor_HTU21D sensor_htu21d;
#ifndef SENSOR_HTU21D_temperature_minchange
#define SENSOR_HTU21D_temperature_minchange 0.2
#endif
#ifndef SENSOR_HTU21D_temperature_senddelaymax
#define SENSOR_HTU21D_temperature_senddelaymax 300000
#endif
#ifndef SENSOR_HTU21D_temperature_readdelay
#define SENSOR_HTU21D_temperature_readdelay 10000
#endif
#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
#include "Adafruit_HTU21DF.h"
bool htu21dinit_ok=false;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
struct sensordata dataHTU21D_temperature; //struct values are changed in setup()
float value_temperatureHTU=0;
struct sensordata dataHTU21D_humidity; //struct values are changed in setup()
float value_humidityHTU=0;
#ifndef SENSOR_HTU21D_humidity_readdelay
#define SENSOR_HTU21D_humidity_readdelay 10000
#endif
#endif
#ifdef SENSOR_HS1101
@ -341,24 +352,9 @@ void setup() {
#endif
#ifdef SENSOR_HTU21D
Serial.println("initializing htu21d");
if (!htu.begin()) {
Serial.println("#ERROR: HTU21D init fail\n");
}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
sensor_htu21d.init();
sensor_htu21d.setSettings_Temperature(SENSOR_HTU21D_temperature_minchange,SENSOR_HTU21D_temperature_senddelaymax,SENSOR_HTU21D_temperature_readdelay);
sensor_htu21d.setSettings_Humidity(SENSOR_HTU21D_humidity_minchange,SENSOR_HTU21D_humidity_senddelaymax,SENSOR_HTU21D_humidity_readdelay);
#endif
@ -557,6 +553,10 @@ void setup() {
sensor_bmp180.advertise(sensorNode);
#endif
#ifdef SENSOR_HTU21D
sensor_htu21d.advertise(sensorNode);
#endif
#ifdef SENSOR_HS1101
#ifdef SENSOR_DHT22
sensorNode.advertise("humidity_hs1101");
@ -590,10 +590,7 @@ void setup() {
#endif
#ifdef SENSOR_HTU21D
sensorNode.advertise("temperature_htu");
sensorNode.advertise("humidity_htu");
#endif
#ifdef SENSOR_MHZ19
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
void loop_HS1101() {
@ -1247,10 +1187,7 @@ void loopHandler() {
#endif
#ifdef SENSOR_HTU21D
if (htu21dinit_ok) {
loop_HTU21D_temperature();
loop_HTU21D_humidity();
}
sensor_htu21d.sensorloop();
#endif
#ifdef SENSOR_HS1101

Loading…
Cancel
Save