sensoresp/include/sensor_sht31.cpp

130 lines
3.8 KiB
C++

//Connect SCL to D1, SDA to D2, GND and 3v3
#include "sensor_sht31.h"
Sensor_SHT31::Sensor_SHT31()
{
sht31 = new Adafruit_SHT31();
}
void Sensor_SHT31::init() //Things to be done during setup()
{
Serial.println("initializing sht31");
if (!sht31->begin(0x44)){
Serial.println("#ERROR: SHT31 init fail\n");
}else{
init_ok=true; //stays false if init failed, sensor will not be read in loop
}
}
//Also called during setup()
void Sensor_SHT31::setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data_temperature.minchange=minchange;
data_temperature.senddelaymax=senddelaymax;
data_temperature.readdelay=readdelay;
}
//Also called during setup()
void Sensor_SHT31::setSettings_Humidity(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data_humidity.minchange=minchange;
data_humidity.senddelaymax=senddelaymax;
data_humidity.readdelay=readdelay;
}
//Called during setup
void Sensor_SHT31::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("temperature_sht");
sensorNode->advertise("humidity_sht");
sensorNode->advertise("sht_heater");
}
void Sensor_SHT31::sensorloop()
{
if (init_ok) {
loop_SHT31_temperature();
loop_SHT31_humidity();
}
}
void Sensor_SHT31::loop_SHT31_temperature()
{
sensordata &d=data_temperature;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
d.value = sht31->readTemperature();
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending SHT31_temperature. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (!(isnan(d.value) == 1)){ //success
sensorNode->setProperty("temperature_sht").send(String(d.value));
Homie.getLogger() << "temperature_sht " << ": " << d.value << endl;
d.lastsentvalue=d.value;
}
d.lastsent=millis();
}
}
void Sensor_SHT31::loop_SHT31_humidity()
{
sensordata &d=data_humidity;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
d.value = sht31->readHumidity();
checkHeater(d.value);
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending SHT31_humidity. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (!(isnan(d.value) == 1)){ //success
Homie.getLogger() << "humidity_sht " << ": " << d.value << endl;
sensorNode->setProperty("humidity_sht").send(String(d.value));
d.lastsentvalue=d.value;
}
d.lastsent=millis();
}
}
void Sensor_SHT31::checkHeater(float humidity) {
bool heaterstatus=sht31->isHeaterEnabled();
if (isnan(humidity)==1) { //if sensor cant be read. turn heater on to prevent condensation just in case
sht31->heater(true);
Serial.println("checkHeater humidity isnan. turned heater on");
}else{
if (!heaterstatus && humidity>=SHT31_HUMIDITY_HEATERON) {//heater was off and humidity is too high
sht31->heater(true);
Serial.println("checkHeater humidity too high. turned heater on");
}else if(heaterstatus && humidity<=SHT31_HUMIDITY_HEATEROFF) { //heater was on and humidity is low enough
sht31->heater(false);
Serial.println("checkHeater humidity low enough. turned heater off");
}
}
heaterstatus=sht31->isHeaterEnabled();
if (heaterstatus!=last_sent_heaterstatus || millis()<30000) { //changed or just booted
sensorNode->setProperty("sht_heater").send(String(heaterstatus));
last_sent_heaterstatus=heaterstatus;
}
}