From 90fd2c9cf341fd405126b480a48b994d85bbdb35 Mon Sep 17 00:00:00 2001 From: Butch Date: Mon, 6 Apr 2020 13:50:16 -0400 Subject: [PATCH] Add SHT31 temp/humidity sensor --- examples/sht31_example.cpp | 51 +++++++++++++++++++++++++++ platformio.ini | 4 ++- src/sensors/sht31.cpp | 71 ++++++++++++++++++++++++++++++++++++++ src/sensors/sht31.h | 45 ++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 examples/sht31_example.cpp create mode 100644 src/sensors/sht31.cpp create mode 100644 src/sensors/sht31.h diff --git a/examples/sht31_example.cpp b/examples/sht31_example.cpp new file mode 100644 index 000000000..6050e1af5 --- /dev/null +++ b/examples/sht31_example.cpp @@ -0,0 +1,51 @@ +// SHT31_example.cpp + +#include + +//#define SERIAL_DEBUG_DISABLED + +#define USE_LIB_WEBSOCKET true + +#include "sensesp_app.h" +#include "signalk/signalk_output.h" +#include "sensors/sht31.h" + +ReactESP app([] () { + #ifndef SERIAL_DEBUG_DISABLED + Serial.begin(115200); + + // A small arbitrary delay is required to let the + // serial port catch up + + delay(100); + Debug.setSerialEnabled(true); + #endif + + // true will disable systemHz, freemem, uptime, and ipaddress "sensors" + bool disableStandardSensors = false; + + sensesp_app = new SensESPApp(disableStandardSensors); + + // Create a SHT31, which represents the physical sensor. + // 0x44 is the default address. Some chips use 0x45, which is shown here. + auto* pSHT31 = new SHT31(0x45); + + + // Define the read_delay you're going to use. The default is 500 ms. + const uint read_delay = 1000; + + // Create a SHT31value, which is used to read a specific value from the SHT31, and send its output + // to SignalK as a Number (float). This one is for the temperature reading. + auto* pSHT31temperature = new SHT31value(pSHT31, temperature, read_delay, "fridge/temperature"); + + pSHT31temperature->connectTo(new SKOutputNumber("environment.inside.refrigerator.temperature")); + + + // Do the same for the humidity value. + auto* pSHT31humidity = new SHT31value(pSHT31, humidity, read_delay, "fridge/humidity"); + + pSHT31humidity->connectTo(new SKOutputNumber("environment.inside.refrigerator.humidity")); + + + sensesp_app->enable(); +}); diff --git a/platformio.ini b/platformio.ini index 3884459df..e2d40177a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,7 +38,9 @@ lib_deps = https://github.com/JoaoLopesF/RemoteDebug.git#0b5a9c1a49fd2ade0e3cadc3a3707781e819359a Adafruit ADS1X15 Wire - + Adafruit BME280 Library + Adafruit BMP280 Library + Adafruit SHT31 Library extra_scripts = extra_script.py diff --git a/src/sensors/sht31.cpp b/src/sensors/sht31.cpp new file mode 100644 index 000000000..42f639287 --- /dev/null +++ b/src/sensors/sht31.cpp @@ -0,0 +1,71 @@ +#include "sht31.h" + +#include "sensesp.h" +#include + + +// SHT31 represents an ADAfruit (or compatible) SHT31 temperature & humidity sensor. +SHT31::SHT31(uint8_t addr, String config_path) : + Sensor(config_path), addr{addr} { + className = "SHT31"; + load_configuration(); + pAdafruitSHT31 = new Adafruit_SHT31(); + if (!pAdafruitSHT31->begin(addr)) { + debugE("Could not find a valid SHT31 sensor: check address and wiring"); + } +} + + +// SHT31value reads and outputs the specified type of value of a SHT31 sensor +SHT31value::SHT31value(SHT31* pSHT31, SHT31ValType val_type, uint read_delay, String config_path) : + NumericSensor(config_path), pSHT31{pSHT31}, val_type{val_type}, read_delay{read_delay} { + className = "SHT31value"; + load_configuration(); +} + +// SHT31 outputs temp in Celsius. Need to convert to Kelvin before sending to Signal K. +// Humidity is output in relative humidity (0 - 100%) +void SHT31value::enable() { + app.onRepeat(read_delay, [this](){ + if (val_type == temperature) { + output = pSHT31->pAdafruitSHT31->readTemperature() + 273.15; // Kelvin is Celsius + 273.15 + } + else if (val_type == humidity) { + output = pSHT31->pAdafruitSHT31->readHumidity(); + } + else output = 0.0; + + notify(); + }); +} + +JsonObject& SHT31value::get_configuration(JsonBuffer& buf) { + JsonObject& root = buf.createObject(); + root["read_delay"] = read_delay; + root["value"] = output; + return root; + }; + + static const char SCHEMA[] PROGMEM = R"###({ + "type": "object", + "properties": { + "read_delay": { "title": "Read delay", "type": "number", "description": "The time, in milliseconds, between each read of the input" }, + "value": { "title": "Last value", "type" : "number", "readOnly": true } + } + })###"; + + + String SHT31value::get_config_schema() { + return FPSTR(SCHEMA); +} + +bool SHT31value::set_configuration(const JsonObject& config) { + String expected[] = {"read_delay"}; + for (auto str : expected) { + if (!config.containsKey(str)) { + return false; + } + } + read_delay = config["read_delay"]; + return true; +} diff --git a/src/sensors/sht31.h b/src/sensors/sht31.h new file mode 100644 index 000000000..f729927eb --- /dev/null +++ b/src/sensors/sht31.h @@ -0,0 +1,45 @@ +#ifndef _sht31_H_ +#define _sht31_H_ + +#include +#include + +#include "sensor.h" + +// The SHT31 classes are based on the ADAfruit_SHT31 library. + +// SHT31 represents an ADAfruit (or compatible) SHT31 temperature & humidity sensor. +// The constructor creates a pointer to the instance, and starts up the sensor. The pointer is +// passed to SHT31value, which retrieves the specified value. + +class SHT31 : public Sensor { + public: + SHT31(uint8_t addr = 0x44, String config_path = ""); + Adafruit_SHT31* pAdafruitSHT31; + + private: + uint8_t addr; +}; + + +// Pass one of these in the constructor to SHT31value() to tell which type of value you want to output +enum SHT31ValType { temperature, humidity }; + +// SHT31value reads and outputs the specified value of a SHT31 sensor. +class SHT31value : public NumericSensor { + public: + SHT31value(SHT31* pSHT31, SHT31ValType val_type, uint read_delay = 500, String config_path=""); + void enable() override final; + SHT31* pSHT31; + + private: + + SHT31ValType val_type; + uint read_delay; + virtual JsonObject& get_configuration(JsonBuffer& buf) override; + virtual bool set_configuration(const JsonObject& config) override; + virtual String get_config_schema() override; + +}; + +#endif