Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SHT31 temp/humidity sensor #101

Merged
merged 2 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/sht31_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SHT31_example.cpp

#include <Arduino.h>

//#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();
});
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ lib_deps =
Wire
Adafruit BME280 Library
Adafruit BMP280 Library
Adafruit SHT31 Library

extra_scripts = extra_script.py

71 changes: 71 additions & 0 deletions src/sensors/sht31.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "sht31.h"

#include "sensesp.h"
#include <RemoteDebug.h>


// 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;
}
45 changes: 45 additions & 0 deletions src/sensors/sht31.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef _sht31_H_
#define _sht31_H_

#include <Wire.h>
#include <Adafruit_SHT31.h>

#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