From cf641851afc96796f341b7e512a7b57a81cbcada Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 29 Apr 2020 22:22:17 +0900 Subject: [PATCH] Added HTU21 Sensor (#588) * Added HTU21 Sensor Support Co-authored-by: Jonathan Wells --- docs/setitup/sensors.md | 3 + docs/use/sensors.md | 11 +++- main/User_config.h | 1 + main/ZmqttDiscovery.ino | 22 +++++++ main/ZsensorHTU21.ino | 125 ++++++++++++++++++++++++++++++++++++++++ main/config_HTU21.h | 56 ++++++++++++++++++ main/main.ino | 12 ++++ platformio.ini | 7 +++ 8 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 main/ZsensorHTU21.ino create mode 100644 main/config_HTU21.h diff --git a/docs/setitup/sensors.md b/docs/setitup/sensors.md index f7d394c..935c1c1 100644 --- a/docs/setitup/sensors.md +++ b/docs/setitup/sensors.md @@ -7,6 +7,7 @@ |HCSR501|PIR|[parts list](https://docs.google.com/spreadsheets/d/1_5fQjAixzRtepkykmL-3uN3G5bLfQ0zMajM9OBZ1bx0/edit#gid=1323184277)| |BH1750|Digital light|[parts list](https://docs.google.com/spreadsheets/d/1_5fQjAixzRtepkykmL-3uN3G5bLfQ0zMajM9OBZ1bx0/edit#gid=1323184277)| |BME280|Temperature, Humidity and pressure|[parts list](https://docs.google.com/spreadsheets/d/1_5fQjAixzRtepkykmL-3uN3G5bLfQ0zMajM9OBZ1bx0/edit#gid=1323184277)| +|HTU21|Temperature, Humidity|[parts list](https://docs.google.com/spreadsheets/d/1_5fQjAixzRtepkykmL-3uN3G5bLfQ0zMajM9OBZ1bx0/edit#gid=1323184277)| |GPIO Input|Inputs|-| |GPIO KeyCode|Keycode|[parts list](https://docs.google.com/spreadsheets/d/1_5fQjAixzRtepkykmL-3uN3G5bLfQ0zMajM9OBZ1bx0/edit#gid=1323184277)| |INA226|Current and voltage|[parts list](https://docs.google.com/spreadsheets/d/1_5fQjAixzRtepkykmL-3uN3G5bLfQ0zMajM9OBZ1bx0/edit#gid=1323184277)| @@ -22,6 +23,8 @@ |BH1750 SCL|A5|D1|22| |BME280 SDA|A4|D2|21| |BME280 SCL|A5|D1|22| +|HTU21 SDA|A4|D2|21| +|HTU21 SCL|A5|D1|22| |INA226 SDA|A4|D2|21| |INA226 SCL|A5|D1|22| |TSL2561 SDA|A4|D2|21| diff --git a/docs/use/sensors.md b/docs/use/sensors.md index 07caac5..3ada367 100644 --- a/docs/use/sensors.md +++ b/docs/use/sensors.md @@ -4,7 +4,7 @@ Subscribe to all the messages with mosquitto or open your MQTT client software: ` sudo mosquitto_sub -t +/# -v` ### ADC -The value is between 0 and 1024 and is tranmitted via Mqtt when it changes. +The value is between 0 and 1024 and is tranmitted via Mqtt when it changes. `home/OpenMQTTGateway/DHTtoMQTT {"value":543}` ### DHT @@ -12,4 +12,11 @@ You will receive every TimeBetweenReadingDHT (set into config_DHT.h) the DHT mea `home/OpenMQTTGateway/DHTtoMQTT {"temp":21,"hum":51}` -If you want to don't resend value when it is the same you can set dht_always = false in config_DHT.h \ No newline at end of file +If you want to don't resend value when it is the same you can set dht_always = false in config_DHT.h + +### HTU21 +You will receive the HTU21 sensor readings every TimeBetweenReadinghtu21 (set into config_HTU21.h) (30s by default). + +`home/OpenMQTTGateway/CLIMAtoMQTT/htu {"tempc":25.34064,"tempf":77.61314,"hum":56.53052}` + +If you don't want to resend values that haven't changed you can set htu21_always = false in config_HTU21.h \ No newline at end of file diff --git a/main/User_config.h b/main/User_config.h index e77678a..9c7c3b3 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -170,6 +170,7 @@ int low_power_mode = DEFAULT_LOW_POWER_MODE; //#define ZsensorBH1750 "BH1750" //ESP8266, Arduino, ESP32 //#define ZsensorTSL2561 "TSL2561" //ESP8266, Arduino, ESP32 //#define ZsensorBME280 "BME280" //ESP8266, Arduino, ESP32 +//#define ZsensorHTU21 "HTU21" //ESP8266, Arduino, ESP32 //#define ZsensorDHT "DHT" //ESP8266, Arduino, ESP32, Sonoff RF Bridge //#define ZsensorDS1820 "DS1820" //ESP8266, Arduino, ESP32 //#define ZsensorGPIOKeyCode "GPIOKeyCode" //ESP8266, Arduino, ESP32 diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 6b0ef62..ed43adc 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -182,6 +182,28 @@ void pubMqttDiscovery() } #endif +#ifdef ZsensorHTU21 +#define HTUparametersCount 3 + Log.trace(F("htu21Discovery" CR)); + char *HTUsensor[HTUparametersCount][8] = { + {"sensor", "tempc", "htu", "temperature", "{{ value_json.tempc }}", "", "", "°C"}, + {"sensor", "tempf", "htu", "temperature", "{{ value_json.tempf }}", "", "", "°F"}, + {"sensor", "hum", "htu", "humidity", "{{ value_json.hum }}", "", "", "%"} + //component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement + }; + + for (int i = 0; i < HTUparametersCount; i++) + { + Log.trace(F("CreateDiscoverySensor" CR)); + //trc(HTUsensor[i][1]); + createDiscovery(HTUsensor[i][0], + HTUTOPIC, HTUsensor[i][1], (char *)getUniqueId(HTUsensor[i][1], HTUsensor[i][2]).c_str(), + will_Topic, HTUsensor[i][3], HTUsensor[i][4], + HTUsensor[i][5], HTUsensor[i][6], HTUsensor[i][7], + 0, "", "", true, ""); + } +#endif + #ifdef ZsensorDHT #define DHTparametersCount 2 Log.trace(F("DHTDiscovery" CR)); diff --git a/main/ZsensorHTU21.ino b/main/ZsensorHTU21.ino new file mode 100644 index 0000000..513147e --- /dev/null +++ b/main/ZsensorHTU21.ino @@ -0,0 +1,125 @@ +/* + OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This is the Climate Addon: + - Measures Temperature, Humidity and Pressure + - Generates Values for: Temperature in degrees C and F, Humidity in %, Pressure in Pa, Altitude in Meter and Feet + - Required Hardware Module: HTU21 + - Required Library: SparkFun HTU21 Library v1.1.3 + + Connection Schemata: + -------------------- + + HTU21 ------> Arduino Uno ----------> ESP8266 + ============================================== + Vcc ---------> 5V -------------------> Vu (5V) + GND ---------> GND ------------------> GND + SCL ---------> Pin A5 ---------------> D1 + SDA ---------> Pin A4 ---------------> D2 + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#include "User_config.h" + +#ifdef ZsensorHTU21 +#include "Wire.h" // Library for communication with I2C / TWI devices +#include +#include "SparkFunHTU21D.h" +#include "config_HTU21.h" + +//Time used to wait for an interval before resending measured values +unsigned long timehtu21 = 0; + +//Global sensor object +HTU21D htuSensor; + +void setupZsensorHTU21() +{ + delay(10); // Gives the Sensor enough time to turn on + Log.notice(F("HTU21 Initialized - begin()" CR)); + +#if defined(ESP32) + Wire.begin(I2C_SDA, I2C_SCL); + htuSensor.begin(Wire); +#else +#endif + htuSensor.begin(); +} + +void MeasureTempHum() +{ + + if (millis() > (timehtu21 + TimeBetweenReadinghtu21)) + { + Log.trace(F("Read HTU21 Sensor" CR)); + + timehtu21 = millis(); + static float persisted_htu_tempc; + static float persisted_htu_hum; + + float HtuTempC = htuSensor.readTemperature(); + float HtuHum = htuSensor.readHumidity(); + + if (HtuTempC >= 998 || HtuHum >= 998 ) + { + Log.error(F("Failed to read from sensor HTU21!" CR)); + return; + } + + // Check if reads failed and exit early (to try again). + if (isnan(HtuTempC) || isnan(HtuHum)) + { + Log.error(F("Failed to read from sensor HTU21!" CR)); + } + else + { + Log.notice(F("Creating HTU21 buffer" CR)); + StaticJsonBuffer jsonBuffer; + JsonObject &HTU21data = jsonBuffer.createObject(); + // Generate Temperature in degrees C + if (HtuTempC != persisted_htu_tempc || htu21_always) + { + float HtuTempF = (HtuTempC*1.8)+32; + HTU21data.set("tempc", (float)HtuTempC); + HTU21data.set("tempf", (float)HtuTempF); + } + else + { + Log.notice(F("Same Temp. Don't send it" CR)); + } + + // Generate Humidity in percent + if (HtuHum != persisted_htu_hum || htu21_always) + { + HTU21data.set("hum", (float)HtuHum); + } + else + { + Log.notice(F("Same Humidity. Don't send it" CR)); + } + + if (HTU21data.size() > 0) + pub(HTUTOPIC, HTU21data); + } + persisted_htu_tempc = HtuTempC; + persisted_htu_hum = HtuHum; + } +} + +#endif diff --git a/main/config_HTU21.h b/main/config_HTU21.h new file mode 100644 index 0000000..1cca815 --- /dev/null +++ b/main/config_HTU21.h @@ -0,0 +1,56 @@ +/* + OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation + + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Send and receiving command by MQTT + + This is the Climate Addon: + - Measures Temperature, Humidity and Pressure + - Generates Values for: Temperature in degrees C and F, Humidity in %, Pressure in Pa, Altitude in Meter and Feet + - Required Hardware Module: HTU21 + - Required Library: SparkFun HTU21 Library v1.1.3 + + Connection Schemata: + -------------------- + + HTU21 ------> Arduino Uno ----------> ESP8266 + ============================================== + Vcc ---------> 5V -------------------> Vu (5V) + GND ---------> GND ------------------> GND + SCL ---------> Pin A5 ---------------> D1 + SDA ---------> Pin A4 ---------------> D2 + + This file is part of OpenMQTTGateway. + + OpenMQTTGateway is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenMQTTGateway is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +#ifndef config_HTU21_h +#define config_HTU21_h + +extern void setupHTU21(); +extern void HTU21toMQTT(); + +#define htu21_always true // if false when the current value of the parameter is the same as previous one don't send it by MQTT +#define TimeBetweenReadinghtu21 30000 + +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define HTUTOPIC "/CLIMAtoMQTT/htu" + +#if defined(ESP32) + #define I2C_SDA 16 + #define I2C_SCL 0 +#endif + +#endif \ No newline at end of file diff --git a/main/main.ino b/main/main.ino index 5371a2d..a6ed5f5 100644 --- a/main/main.ino +++ b/main/main.ino @@ -85,6 +85,9 @@ unsigned long ReceivedSignal[array_size][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; #ifdef ZsensorBME280 #include "config_BME280.h" #endif +#ifdef ZsensorHTU21 +#include "config_HTU21.h" +#endif #ifdef ZsensorHCSR04 #include "config_HCSR04.h" #endif @@ -599,6 +602,9 @@ void setup() #ifdef ZsensorBME280 setupZsensorBME280(); #endif + #ifdef ZsensorHTU21 + setupZsensorHTU21(); + #endif #ifdef ZsensorBH1750 setupZsensorBH1750(); #endif @@ -1157,6 +1163,9 @@ void loop() #ifdef ZsensorBME280 MeasureTempHumAndPressure(); //Addon to measure Temperature, Humidity, Pressure and Altitude with a Bosch BME280 #endif + #ifdef ZsensorHTU21 + MeasureTempHum(); //Addon to measure Temperature, Humidity, of a HTU21 sensor + #endif #ifdef ZsensorHCSR04 MeasureDistance(); //Addon to measure distance with a HC-SR04 #endif @@ -1286,6 +1295,9 @@ void stateMeasures() #ifdef ZsensorBME280 modules = modules + ZsensorBME280; #endif + #ifdef ZsensorHTU21 + modules = modules + ZsensorHTU21; + #endif #ifdef ZsensorHCSR04 modules = modules + ZsensorHCSR04; #endif diff --git a/platformio.ini b/platformio.ini index bdbe8ae..821a602 100644 --- a/platformio.ini +++ b/platformio.ini @@ -80,6 +80,7 @@ unifiedsensor = Adafruit Unified Sensor@1.0.2 tsl2561 = Adafruit TSL2561@1.0.3 bme280 = SparkFun BME280@2.0.4 bmp180 = BMP180@efac46bd8d +htu21 = SparkFun HTU21D Humidity and Temperature Sensor Br@1.1.3 ina226 = jarzebski/Arduino-INA226#968a684 a6lib = 1technophile/A6lib wifimanager = tzapu/WiFiManager#c3ff582 @@ -173,6 +174,7 @@ lib_deps = ${libraries.newremoteswitch} ${libraries.bme280} ${libraries.bmp180} + ${libraries.htu21} ${libraries.unifiedsensor} ${libraries.dht} ${libraries.tsl2561} @@ -194,6 +196,7 @@ build_flags = '-DZsensorADC="ADC"' '-DZsensorBH1750="BH1750"' '-DZsensorBME280="BME280"' + '-DZsensorHTU21="HTU21"' '-DZsensorTSL2561="TSL2561"' '-DZsensorDHT="DHT"' '-DZsensorDS1820="DS1820"' @@ -361,6 +364,7 @@ lib_deps = ${libraries.newremoteswitch} ${libraries.bme280} ${libraries.bmp180} + ${libraries.htu21} ${libraries.unifiedsensor} ${libraries.dht} ${libraries.tsl2561} @@ -386,6 +390,7 @@ build_flags = '-DZsensorADC="ADC"' '-DZsensorBH1750="BH1750"' '-DZsensorBME280="BME280"' + '-DZsensorHTU21="HTU21"' '-DZsensorTSL2561="TSL2561"' '-DZsensorDHT="DHT"' '-DZsensorDS1820="DS1820"' @@ -576,6 +581,7 @@ lib_deps = ${libraries.newremoteswitch} ${libraries.bme280} ${libraries.bmp180} + ${libraries.htu21} ${libraries.unifiedsensor} ${libraries.dht} ${libraries.tsl2561} @@ -598,6 +604,7 @@ build_flags = '-DZsensorADC="ADC"' '-DZsensorBH1750="BH1750"' '-DZsensorBME280="BME280"' + '-DZsensorHTU21="HTU21"' '-DZsensorTSL2561="TSL2561"' '-DZsensorDHT="DHT"' '-DZsensorDS1820="DS1820"'