|
| 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 |
0 commit comments