add htu21d sensor

master
interfisch 1 year ago
parent f786cbd77c
commit fe29b238ff
  1. 28
      platformio.ini
  2. 111
      src/main.cpp

@ -16,8 +16,9 @@
#data_dir=data_sensoresp1
#data_dir=data_sensoresp2
#data_dir=data_sensoresp3
data_dir=data_sensoresp4
#data_dir=data_sensoresp4
#data_dir=data_sensoresp5
data_dir=data_sensoresp3dprinter
#Outdoor
@ -244,3 +245,28 @@ lib_deps =
Homie@3.0.0
#3D Printer
[env:sensoresp3dprinter]
platform = espressif8266
board = d1_mini
framework = arduino
monitor_port = /dev/ttyUSB0
monitor_speed = 115200
build_flags =
-D SENSOR_HTU21D
-D dataHTU21D_temperature_minchange=0.2
-D dataHTU21D_temperature_senddelaymax=1000*60*10
-D dataHTU21D_humidity_minchange=1.0
-D dataHTU21D_humidity_senddelaymax=1000*60*10
lib_deps =
adafruit/Adafruit HTU21DF Library @ ^1.0.5
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
Homie@3.0.0

@ -53,6 +53,21 @@ struct sensordata
float value_pressureBMP=0;
#endif
#ifdef SENSOR_HTU21D
//Connect SCL to D1, SDA to D2, GND and 3v3
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#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;
#endif
#ifdef SENSOR_HS1101
struct sensordata dataHS1101;
float value_humidityHS1101=0;
@ -66,7 +81,10 @@ struct sensordata
#ifdef SENSOR_BH1750
//SCL=D1, SDA=D2
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#endif
#include <BH1750.h>
BH1750 lightMeter(0x23); //0x23 if addr connected to ground (=pin open), 0x5c if addr pulled high
bool bh1750init_ok=false;
@ -297,6 +315,29 @@ void setup() {
#endif
#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
#endif
#ifdef SENSOR_HS1101
Serial.println("initializing hs1101");
#ifdef dataHS1101_minchange
@ -507,6 +548,11 @@ void setup() {
sensorNode.advertise("pressure");
#endif
#ifdef SENSOR_HTU21D
sensorNode.advertise("temperature_htu");
sensorNode.advertise("humidity_htu");
#endif
#ifdef SENSOR_MHZ19
sensorNode.advertise("co2");
#ifdef MHZ19CALIBRATIONTOPIC
@ -671,6 +717,64 @@ void loop_BMP180_pressure()
}
#endif
#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() {
@ -1156,6 +1260,13 @@ void loopHandler() {
}
#endif
#ifdef SENSOR_HTU21D
if (htu21dinit_ok) {
loop_HTU21D_temperature();
loop_HTU21D_humidity();
}
#endif
#ifdef SENSOR_HS1101
loop_HS1101();
#endif

Loading…
Cancel
Save