diff --git a/main/User_config.h b/main/User_config.h index 849a95e..b399bb5 100755 --- a/main/User_config.h +++ b/main/User_config.h @@ -233,6 +233,7 @@ uint8_t wifiProtocol = 0; // default mode, automatic selection //#define ZboardM5STACK "ZboardM5STACK" //#define ZradioCC1101 "CC1101" //ESP8266, ESP32 //#define ZactuatorPWM "PWM" //ESP8266, ESP32 +//#define ZsensorSHTC3 "SHTC3" //ESP8266, Arduino, ESP32, Sonoff RF Bridge //#define ZactuatorSomfy "Somfy" //ESP8266, Arduino, ESP32 //#define ZgatewayRS232 "RS232" //ESP8266, Arduino, ESP32 diff --git a/main/ZsensorSHTC3.ino b/main/ZsensorSHTC3.ino new file mode 100644 index 0000000..650f83b --- /dev/null +++ b/main/ZsensorSHTC3.ino @@ -0,0 +1,75 @@ +#include "User_config.h" + +#ifdef ZsensorSHTC3 +# include + +SHTC3 mySHTC3; + +//Time used to wait for an interval before resending temp and hum +unsigned long timedht = 0; +void errorDecoder(SHTC3_Status_TypeDef message); + +void errorDecoder(SHTC3_Status_TypeDef message) // The errorDecoder function prints "SHTC3_Status_TypeDef" resultsin a human-friendly way +{ + switch (message) { + case SHTC3_Status_Nominal: + Log.notice("Nominal"); + break; + case SHTC3_Status_Error: + Log.error("Error"); + break; + case SHTC3_Status_CRC_Fail: + Log.error("CRC Fail"); + break; + default: + Log.error("Unknown return code"); + break; + } +} + +void setupSHTC3() { + Wire.begin(); + errorDecoder(mySHTC3.begin()); // To start the sensor you must call "begin()", the default settings use Wire (default Arduino I2C port) +} + +void MeasureTempAndHum() { + if (millis() > (timedht + TimeBetweenReadingSHTC3)) { //retrieving value of temperature and humidity of the box from SHTC3 every xUL + timedht = millis(); + static float persistedh; + static float persistedt; + SHTC3_Status_TypeDef result = mySHTC3.update(); + if (mySHTC3.lastStatus == SHTC3_Status_Nominal) { + // Read temperature as Celsius (the default) + float t = mySHTC3.toDegC(); + float h = mySHTC3.toPercent(); + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t)) { + Log.error(F("Failed to read from SHTC3 sensor!" CR)); + } else { + Log.trace(F("Creating SHTC3 buffer" CR)); + StaticJsonBuffer jsonBuffer; + JsonObject& SHTC3data = jsonBuffer.createObject(); + if (h != persistedh || shtc3_always) { + SHTC3data.set("hum", (float)h); + } else { + Log.trace(F("Same hum don't send it" CR)); + } + if (t != persistedt || shtc3_always) { + SHTC3data.set("temp", (float)t); // remove for 0.9.6 release + SHTC3data.set("tempc", (float)t); + SHTC3data.set("tempf", mySHTC3.toDegF()); + } else { + Log.trace(F("Same temp don't send it" CR)); + } + if (SHTC3data.size() > 0) + pub(SHTC3TOPIC, SHTC3data); + } + persistedh = h; + persistedt = t; + } else { + errorDecoder(mySHTC3.lastStatus); + Log.error(F("Failed to read from SHTC3 sensor!" CR)); + } + } +} +#endif diff --git a/main/config_SHTC3.h b/main/config_SHTC3.h new file mode 100644 index 0000000..0f26535 --- /dev/null +++ b/main/config_SHTC3.h @@ -0,0 +1,52 @@ +/* + OpenMQTTGateway - 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 files enables to set your parameter for the SHTC311/22 sensor + + Copyright: (c)Florian ROBERT + + 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_SHTC3_h +#define config_SHTC3_h + +extern void setupSHTC3(); +extern void SHTC3toMQTT(); + +/*----------------------------USER PARAMETERS-----------------------------*/ +/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ +#define SHTC3TOPIC "/SHTC3toMQTT/shtc31" +#define shtc3_always true // if false when the current value for temp or hum is the same as previous one don't send it by MQTT +#define TimeBetweenReadingSHTC3 30000 // time between 2 SHTC3 readings +/*-------------SHTC3 SENSOR TYPE-------------*/ +//#define SHTC3_SENSOR_TYPE SHTC311 //uncomment for SHTC311 Sensor +//#define SHTC3_SENSOR_TYPE SHTC321 //uncomment for SHTC321 Sensor +#define SHTC3_SENSOR_TYPE SHTC322 //uncomment for SHTC322 Sensor (default for backwards compatibility) +/*-------------------PIN DEFINITIONS----------------------*/ +#ifndef SHTC3_RECEIVER_GPIO +# if defined(ESP8266) +# define SHTC3_RECEIVER_GPIO 5 //5 = D1 you can put 14 = D5 if you don't use HCSR501 sensor and the RFM69 +# elif defined(ESP32) +# define SHTC3_RECEIVER_GPIO 16 +# else +# define SHTC3_RECEIVER_GPIO 8 +# endif +#endif + +#endif \ No newline at end of file diff --git a/main/main.ino b/main/main.ino index bc63603..8112808 100644 --- a/main/main.ino +++ b/main/main.ino @@ -111,6 +111,9 @@ JsonArray& modules = modulesBuffer.createArray(); #ifdef ZsensorDHT # include "config_DHT.h" #endif +#ifdef ZsensorSHTC3 +# include "config_SHTC3.h" +#endif #ifdef ZsensorDS1820 # include "config_DS1820.h" #endif @@ -715,6 +718,9 @@ void setup() { #endif #ifdef ZmqttDiscovery modules.add(ZmqttDiscovery); +#endif +#ifdef ZsensorSHTC3 + setupSHTC3(); #endif Log.trace(F("mqtt_max_packet_size: %d" CR), mqtt_max_packet_size); Log.notice(F("Setup OpenMQTTGateway end" CR)); @@ -1273,6 +1279,9 @@ void loop() { #ifdef ZsensorDHT MeasureTempAndHum(); //Addon to measure the temperature with a DHT #endif +#ifdef ZsensorSHTC3 + MeasureTempAndHum(); //Addon to measure the temperature with a DHT +#endif #ifdef ZsensorDS1820 MeasureDS1820Temp(); //Addon to measure the temperature with DS1820 sensor(s) #endif diff --git a/platformio.ini b/platformio.ini index 6cd8e5b..4078961 100644 --- a/platformio.ini +++ b/platformio.ini @@ -113,6 +113,7 @@ m5stickcp = https://github.com/m5stack/M5StickC-Plus.git m5stack = M5Stack@0.3.0 smartrc-cc1101-driver-lib = SmartRC-CC1101-Driver-Lib@2.3.5 stl = https://github.com/mike-matera/ArduinoSTL.git#7411816 +shtc3 = https://github.com/sparkfun/SparkFun_SHTC3_Arduino_Library somfy_remote=Somfy_Remote_Lib@0.2.0 [env] @@ -205,6 +206,7 @@ lib_deps = ${libraries.onewire} ${libraries.dallastemperature} ${libraries.rfWeatherStation} + ${libraries.shtc3} build_flags = ${com-esp.build_flags} '-DZgatewayRF="RF"'