From 0e257a58c137124c81967086970353640ec411c8 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 4 Jul 2020 23:40:06 +0900 Subject: [PATCH] Add AHTx0 Sensor (AHT10 and AHT20) (#687) * Add AHT Sensor --- docs/use/sensors.md | 7 +++ main/ZmqttDiscovery.ino | 20 +++++++ main/ZsensorAHTx0.ino | 112 ++++++++++++++++++++++++++++++++++++++++ main/config_AHTx0.h | 58 +++++++++++++++++++++ main/main.ino | 12 +++++ platformio.ini | 7 +++ 6 files changed, 216 insertions(+) create mode 100644 main/ZsensorAHTx0.ino create mode 100644 main/config_AHTx0.h diff --git a/docs/use/sensors.md b/docs/use/sensors.md index da450b4..18759a6 100644 --- a/docs/use/sensors.md +++ b/docs/use/sensors.md @@ -21,6 +21,13 @@ You will receive the HTU21 sensor readings every TimeBetweenReadinghtu21 (set in If you don't want to resend values that haven't changed you can set htu21_always = false in config_HTU21.h +### AHTx0 (AHT10 and AHT20) +You will receive the AHT sensor readings every TimeBetweenReadingAHTx0 (set into config_AHTx0.h) (30s by default). + +`home/OpenMQTTGateway/CLIMAtoMQTT/aht { "tempc": 27.48108, "tempf": 81.46594, "hum": 48.90614 }` + +If you don't want to resend values that haven't changed you can set AHTx0_always = false in config_AHTx0.h + ### DS18x20 You will receive the DS18x20 sensor readings every DS1820_INTERVAL_SEC (set into config_DS1820.h) (60s by default). Each sensor will be published under the following topic using each sensors' address. diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index d843531..eec1cb2 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -186,6 +186,26 @@ void pubMqttDiscovery() { } # endif +# ifdef ZsensorAHTx0 +# define AHTparametersCount 3 + Log.trace(F("AHTx0Discovery" CR)); + char* AHTsensor[AHTparametersCount][8] = { + {"sensor", "tempc", "aht", "temperature", jsonTemp, "", "", "°C"}, + {"sensor", "tempf", "aht", "temperature", jsonTempf, "", "", "°F"}, + {"sensor", "hum", "aht", "humidity", jsonHum, "", "", "%"} + //component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement + }; + + for (int i = 0; i < AHTparametersCount; i++) { + Log.trace(F("CreateDiscoverySensor" CR)); + createDiscovery(AHTsensor[i][0], + AHTTOPIC, AHTsensor[i][1], (char*)getUniqueId(AHTsensor[i][1], AHTsensor[i][2]).c_str(), + will_Topic, AHTsensor[i][3], AHTsensor[i][4], + AHTsensor[i][5], AHTsensor[i][6], AHTsensor[i][7], + 0, "", "", true, ""); + } +# endif + # ifdef ZsensorDHT # define DHTparametersCount 2 Log.trace(F("DHTDiscovery" CR)); diff --git a/main/ZsensorAHTx0.ino b/main/ZsensorAHTx0.ino new file mode 100644 index 0000000..f102420 --- /dev/null +++ b/main/ZsensorAHTx0.ino @@ -0,0 +1,112 @@ +/* + 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 + - Generates Values for: Temperature in degrees C and F, Humidity in % + - Required Hardware Module: AHTX0 (AHT10 & AHT20) + - Required Library: Adafruit AHTX0 by Adafruit + + Connection Schemata: + -------------------- + + AHT10 ------> Arduino Uno ----------> ESP8266 + ============================================== + Vcc ---------> 5V -------------------> 3v3 (3V) + 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 ZsensorAHTx0 +# include + +# include "Adafruit_AHTX0.h" +# include "Wire.h" // Library for communication with I2C / TWI devices +# include "config_AHTx0.h" + +//Time used to wait for an interval before resending measured values +unsigned long timeAHTx0 = 0; + +//Global sensor object +Adafruit_AHTX0 ahtSensor; + +void setupZsensorAHTx0() { + delay(10); // Gives the Sensor enough time to turn on + Log.notice(F("AHTx0 Initialized - begin()" CR)); + +# if defined(ESP32) + Wire.begin(AHT_I2C_SDA, AHT_I2C_SCL); + ahtSensor.begin(&Wire); +# else + ahtSensor.begin(); +# endif +} + +void MeasureAHTTempHum() { + if (millis() > (timeAHTx0 + TimeBetweenReadingAHTx0)) { + Log.trace(F("Read AHTx0 Sensor" CR)); + + timeAHTx0 = millis(); + static float persisted_aht_tempc; + static float persisted_aht_hum; + + sensors_event_t ahtTempC, ahtHum; + ahtSensor.getEvent(&ahtHum, &ahtTempC); // get sensor data + + if (ahtTempC.temperature >= 998 || ahtHum.relative_humidity >= 998) { + Log.error(F("Failed to read from sensor AHTx0!" CR)); + return; + } + + // Check if reads failed and exit early (to try again). + if (isnan(ahtTempC.temperature) || isnan(ahtHum.relative_humidity)) { + Log.error(F("Failed to read from sensor AHTx0!" CR)); + } else { + Log.notice(F("Creating AHTx0 buffer" CR)); + StaticJsonBuffer jsonBuffer; + JsonObject& AHTx0data = jsonBuffer.createObject(); + // Generate Temperature in degrees C + if (ahtTempC.temperature != persisted_aht_tempc || AHTx0_always) { + float ahtTempF = (ahtTempC.temperature * 1.8) + 32; + AHTx0data.set("tempc", (float)ahtTempC.temperature); + AHTx0data.set("tempf", (float)ahtTempF); + } else { + Log.notice(F("Same Temp. Don't send it" CR)); + } + + // Generate Humidity in percent + if (ahtHum.relative_humidity != persisted_aht_hum || AHTx0_always) { + AHTx0data.set("hum", (float)ahtHum.relative_humidity); + } else { + Log.notice(F("Same Humidity. Don't send it" CR)); + } + + if (AHTx0data.size() > 0) + pub(AHTTOPIC, AHTx0data); + } + persisted_aht_tempc = ahtTempC.temperature; + persisted_aht_hum = ahtHum.relative_humidity; + } +} + +#endif diff --git a/main/config_AHTx0.h b/main/config_AHTx0.h new file mode 100644 index 0000000..09d4fc7 --- /dev/null +++ b/main/config_AHTx0.h @@ -0,0 +1,58 @@ +/* + 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 + - Generates Values for: Temperature in degrees C and F, Humidity in % + - Required Hardware Module: AHTX0 (AHT10 & AHT20) + - Required Library: Adafruit AHTX0 by Adafruit + + Connection Schemata: + -------------------- + + AHT10 ------> Arduino Uno ----------> ESP8266 + ============================================== + Vcc ---------> 5V -------------------> 3v3 (3V) + 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_AHTx0_h +#define config_AHTx0_h + +extern void setupAHTx0(); +extern void AHTx0toMQTT(); + +#define AHTx0_always true // if false when the current value of the parameter is the same as previous one don't send it by MQTT +#define TimeBetweenReadingAHTx0 30000 + +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define AHTTOPIC "/CLIMAtoMQTT/aht" + +#if defined(ESP32) +# if !defined(AHT_I2C_SDA) || !defined(AHT_I2C_SCL) +# define AHT_I2C_SDA 16 +# define AHT_I2C_SCL 0 +# endif +#endif + +#endif \ No newline at end of file diff --git a/main/main.ino b/main/main.ino index d31f338..7a8a994 100644 --- a/main/main.ino +++ b/main/main.ino @@ -88,6 +88,9 @@ unsigned long ReceivedSignal[array_size][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; #ifdef ZsensorHTU21 # include "config_HTU21.h" #endif +#ifdef ZsensorAHTx0 +# include "config_AHTx0.h" +#endif #ifdef ZsensorHCSR04 # include "config_HCSR04.h" #endif @@ -542,6 +545,9 @@ void setup() { #ifdef ZsensorHTU21 setupZsensorHTU21(); #endif +#ifdef ZsensorAHTx0 + setupZsensorAHTx0(); +#endif #ifdef ZsensorBH1750 setupZsensorBH1750(); #endif @@ -1076,6 +1082,9 @@ void loop() { #ifdef ZsensorHTU21 MeasureTempHum(); //Addon to measure Temperature, Humidity, of a HTU21 sensor #endif +#ifdef ZsensorAHTx0 + MeasureAHTTempHum(); //Addon to measure Temperature, Humidity, of an 'AHTx0' sensor +#endif #ifdef ZsensorHCSR04 MeasureDistance(); //Addon to measure distance with a HC-SR04 #endif @@ -1196,6 +1205,9 @@ void stateMeasures() { # ifdef ZsensorHTU21 modules = modules + ZsensorHTU21; # endif +# ifdef ZsensorAHTx0 + modules = modules + ZsensorAHTx0; +# endif # ifdef ZsensorHCSR04 modules = modules + ZsensorHCSR04; # endif diff --git a/platformio.ini b/platformio.ini index 8e6cf90..610bb66 100644 --- a/platformio.ini +++ b/platformio.ini @@ -82,6 +82,7 @@ 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 +ahtx0 = Adafruit AHTX0 ina226 = jarzebski/Arduino-INA226#968a684 a6lib = 1technophile/A6lib wifimanager = tzapu/WiFiManager#c3ff582 @@ -173,6 +174,7 @@ lib_deps = ${libraries.bme280} ${libraries.bmp180} ${libraries.htu21} + ${libraries.ahtx0} ${libraries.unifiedsensor} ${libraries.dht} ${libraries.tsl2561} @@ -196,6 +198,7 @@ build_flags = '-DZsensorBH1750="BH1750"' '-DZsensorBME280="BME280"' '-DZsensorHTU21="HTU21"' + '-DZsensorAHTx0="AHTx0"' '-DZsensorTSL2561="TSL2561"' '-DZsensorDHT="DHT"' '-DZsensorDS1820="DS1820"' @@ -365,6 +368,7 @@ lib_deps = ${libraries.bme280} ${libraries.bmp180} ${libraries.htu21} + ${libraries.ahtx0} ${libraries.unifiedsensor} ${libraries.dht} ${libraries.tsl2561} @@ -391,6 +395,7 @@ build_flags = '-DZsensorBH1750="BH1750"' '-DZsensorBME280="BME280"' '-DZsensorHTU21="HTU21"' + '-DZsensorAHTx0="AHTx0"' '-DZsensorTSL2561="TSL2561"' '-DZsensorDHT="DHT"' '-DZsensorDS1820="DS1820"' @@ -596,6 +601,7 @@ lib_deps = ${libraries.bme280} ${libraries.bmp180} ${libraries.htu21} + ${libraries.ahtx0} ${libraries.unifiedsensor} ${libraries.dht} ${libraries.tsl2561} @@ -620,6 +626,7 @@ build_flags = '-DZsensorBH1750="BH1750"' '-DZsensorBME280="BME280"' '-DZsensorHTU21="HTU21"' + '-DZsensorAHTx0="AHTx0"' '-DZsensorTSL2561="TSL2561"' '-DZsensorDHT="DHT"' '-DZsensorDS1820="DS1820"'