Skip to content

Commit d84f64b

Browse files
authored
Feature/add shtc3 (#843)
* Add SHTC3 * remove adc config
1 parent 1c18e83 commit d84f64b

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

main/User_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ uint8_t wifiProtocol = 0; // default mode, automatic selection
233233
//#define ZboardM5STACK "ZboardM5STACK"
234234
//#define ZradioCC1101 "CC1101" //ESP8266, ESP32
235235
//#define ZactuatorPWM "PWM" //ESP8266, ESP32
236+
//#define ZsensorSHTC3 "SHTC3" //ESP8266, Arduino, ESP32, Sonoff RF Bridge
236237
//#define ZactuatorSomfy "Somfy" //ESP8266, Arduino, ESP32
237238
//#define ZgatewayRS232 "RS232" //ESP8266, Arduino, ESP32
238239

main/ZsensorSHTC3.ino

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "User_config.h"
2+
3+
#ifdef ZsensorSHTC3
4+
# include <SparkFun_SHTC3.h>
5+
6+
SHTC3 mySHTC3;
7+
8+
//Time used to wait for an interval before resending temp and hum
9+
unsigned long timedht = 0;
10+
void errorDecoder(SHTC3_Status_TypeDef message);
11+
12+
void errorDecoder(SHTC3_Status_TypeDef message) // The errorDecoder function prints "SHTC3_Status_TypeDef" resultsin a human-friendly way
13+
{
14+
switch (message) {
15+
case SHTC3_Status_Nominal:
16+
Log.notice("Nominal");
17+
break;
18+
case SHTC3_Status_Error:
19+
Log.error("Error");
20+
break;
21+
case SHTC3_Status_CRC_Fail:
22+
Log.error("CRC Fail");
23+
break;
24+
default:
25+
Log.error("Unknown return code");
26+
break;
27+
}
28+
}
29+
30+
void setupSHTC3() {
31+
Wire.begin();
32+
errorDecoder(mySHTC3.begin()); // To start the sensor you must call "begin()", the default settings use Wire (default Arduino I2C port)
33+
}
34+
35+
void MeasureTempAndHum() {
36+
if (millis() > (timedht + TimeBetweenReadingSHTC3)) { //retrieving value of temperature and humidity of the box from SHTC3 every xUL
37+
timedht = millis();
38+
static float persistedh;
39+
static float persistedt;
40+
SHTC3_Status_TypeDef result = mySHTC3.update();
41+
if (mySHTC3.lastStatus == SHTC3_Status_Nominal) {
42+
// Read temperature as Celsius (the default)
43+
float t = mySHTC3.toDegC();
44+
float h = mySHTC3.toPercent();
45+
// Check if any reads failed and exit early (to try again).
46+
if (isnan(h) || isnan(t)) {
47+
Log.error(F("Failed to read from SHTC3 sensor!" CR));
48+
} else {
49+
Log.trace(F("Creating SHTC3 buffer" CR));
50+
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
51+
JsonObject& SHTC3data = jsonBuffer.createObject();
52+
if (h != persistedh || shtc3_always) {
53+
SHTC3data.set("hum", (float)h);
54+
} else {
55+
Log.trace(F("Same hum don't send it" CR));
56+
}
57+
if (t != persistedt || shtc3_always) {
58+
SHTC3data.set("temp", (float)t); // remove for 0.9.6 release
59+
SHTC3data.set("tempc", (float)t);
60+
SHTC3data.set("tempf", mySHTC3.toDegF());
61+
} else {
62+
Log.trace(F("Same temp don't send it" CR));
63+
}
64+
if (SHTC3data.size() > 0)
65+
pub(SHTC3TOPIC, SHTC3data);
66+
}
67+
persistedh = h;
68+
persistedt = t;
69+
} else {
70+
errorDecoder(mySHTC3.lastStatus);
71+
Log.error(F("Failed to read from SHTC3 sensor!" CR));
72+
}
73+
}
74+
}
75+
#endif

main/config_SHTC3.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
OpenMQTTGateway - ESP8266 or Arduino program for home automation
3+
4+
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
5+
Send and receiving command by MQTT
6+
7+
This files enables to set your parameter for the SHTC311/22 sensor
8+
9+
Copyright: (c)Florian ROBERT
10+
11+
This file is part of OpenMQTTGateway.
12+
13+
OpenMQTTGateway is free software: you can redistribute it and/or modify
14+
it under the terms of the GNU General Public License as published by
15+
the Free Software Foundation, either version 3 of the License, or
16+
(at your option) any later version.
17+
18+
OpenMQTTGateway is distributed in the hope that it will be useful,
19+
but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
GNU General Public License for more details.
22+
23+
You should have received a copy of the GNU General Public License
24+
along with this program. If not, see <http://www.gnu.org/licenses/>.
25+
*/
26+
#ifndef config_SHTC3_h
27+
#define config_SHTC3_h
28+
29+
extern void setupSHTC3();
30+
extern void SHTC3toMQTT();
31+
32+
/*----------------------------USER PARAMETERS-----------------------------*/
33+
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
34+
#define SHTC3TOPIC "/SHTC3toMQTT/shtc31"
35+
#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
36+
#define TimeBetweenReadingSHTC3 30000 // time between 2 SHTC3 readings
37+
/*-------------SHTC3 SENSOR TYPE-------------*/
38+
//#define SHTC3_SENSOR_TYPE SHTC311 //uncomment for SHTC311 Sensor
39+
//#define SHTC3_SENSOR_TYPE SHTC321 //uncomment for SHTC321 Sensor
40+
#define SHTC3_SENSOR_TYPE SHTC322 //uncomment for SHTC322 Sensor (default for backwards compatibility)
41+
/*-------------------PIN DEFINITIONS----------------------*/
42+
#ifndef SHTC3_RECEIVER_GPIO
43+
# if defined(ESP8266)
44+
# define SHTC3_RECEIVER_GPIO 5 //5 = D1 you can put 14 = D5 if you don't use HCSR501 sensor and the RFM69
45+
# elif defined(ESP32)
46+
# define SHTC3_RECEIVER_GPIO 16
47+
# else
48+
# define SHTC3_RECEIVER_GPIO 8
49+
# endif
50+
#endif
51+
52+
#endif

main/main.ino

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ JsonArray& modules = modulesBuffer.createArray();
111111
#ifdef ZsensorDHT
112112
# include "config_DHT.h"
113113
#endif
114+
#ifdef ZsensorSHTC3
115+
# include "config_SHTC3.h"
116+
#endif
114117
#ifdef ZsensorDS1820
115118
# include "config_DS1820.h"
116119
#endif
@@ -715,6 +718,9 @@ void setup() {
715718
#endif
716719
#ifdef ZmqttDiscovery
717720
modules.add(ZmqttDiscovery);
721+
#endif
722+
#ifdef ZsensorSHTC3
723+
setupSHTC3();
718724
#endif
719725
Log.trace(F("mqtt_max_packet_size: %d" CR), mqtt_max_packet_size);
720726
Log.notice(F("Setup OpenMQTTGateway end" CR));
@@ -1273,6 +1279,9 @@ void loop() {
12731279
#ifdef ZsensorDHT
12741280
MeasureTempAndHum(); //Addon to measure the temperature with a DHT
12751281
#endif
1282+
#ifdef ZsensorSHTC3
1283+
MeasureTempAndHum(); //Addon to measure the temperature with a DHT
1284+
#endif
12761285
#ifdef ZsensorDS1820
12771286
MeasureDS1820Temp(); //Addon to measure the temperature with DS1820 sensor(s)
12781287
#endif

platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ m5stickcp = https://github.com/m5stack/M5StickC-Plus.git
113113
m5stack = M5Stack@0.3.0
114114
smartrc-cc1101-driver-lib = SmartRC-CC1101-Driver-Lib@2.3.5
115115
stl = https://github.com/mike-matera/ArduinoSTL.git#7411816
116+
shtc3 = https://github.com/sparkfun/SparkFun_SHTC3_Arduino_Library
116117
somfy_remote=Somfy_Remote_Lib@0.2.0
117118

118119
[env]
@@ -205,6 +206,7 @@ lib_deps =
205206
${libraries.onewire}
206207
${libraries.dallastemperature}
207208
${libraries.rfWeatherStation}
209+
${libraries.shtc3}
208210
build_flags =
209211
${com-esp.build_flags}
210212
'-DZgatewayRF="RF"'

0 commit comments

Comments
 (0)