Skip to content

Commit

Permalink
New temp and RH sensor
Browse files Browse the repository at this point in the history
  Other changes:
  * Revert back WaterDevice status endpoint
    with some limitations due to the Scheduler
    specifics
  • Loading branch information
vi7 committed Nov 23, 2021
1 parent 2f7f916 commit 9a09e4c
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/grower_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef GROWER_VERSION_H
#define GROWER_VERSION_H

const char GROWER_VERSION[] = "0.8.0-beta3";
const char GROWER_VERSION[] = "0.8.0";
// Example version with appended build version (autoincrementing part):
// const char VERSION[] = "0.4.0-230498"

Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ monitor_dtr = 0
lib_deps =
[email protected]
DHT sensor library for [email protected]
https://github.com/jfturcot/SimpleTimer.git#b30890b8f7046bf3b0258e5870e5dda78ac5c71a
https://github.com/jfturcot/SimpleTimer.git#b30890b8f7046bf3b0258e5870e5dda78ac5c71a
https://github.com/enjoyneering/HTU2xD_SHT2x_Si70xx.git#5a76484f6fd0504125851cc23791f0e2a73e6dae
117 changes: 117 additions & 0 deletions src/HTU2xDDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* HTU2xDDevice.cpp
**/

#include "HTU2xDDevice.h"

void HTU2xDDevice::tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS) {
temp = htu2xD->readTemperature();
if (temp == HTU2XD_SHT2X_SI70XX_ERROR) {
_tempReadErrs++;
Serial.printf((char*)F("%s: Failed to read temperature! CRC8 or communication error occurred\n"), HTU2XD_NAME);
/*
* trying to recover and doing that only for temperature reading
* by following HTU2xD_SHT2x_Si70xx lib example
*/
htu2xD->softReset(); //last chance to make it alive, all registers (except heater bit) will set to default
htu2xD->setHeater(false); //true=heater on, false=heater off
htu2xD->setResolution(HUMD_12BIT_TEMP_14BIT); //humidity 12-bit, temperature 14-bit
return;
}
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, this->_pin);
// TODO: add delay(500) as in the HTU2xD_SHT2x_Si70xx lib example?
}

void HTU2xDDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERESIS) {
rH = htu2xD->getCompensatedHumidity(temp);
if (rH == HTU2XD_SHT2X_SI70XX_ERROR) {
_rHReadErrs++;
Serial.printf((char*)F("%s: Failed to read humidity! CRC8 or communication error occurred\n"), HTU2XD_NAME);
return;
}
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, this->_pin);
}

String HTU2xDDevice::status() {
return "{\"temperature\":\"" + String(temp, 1) +"\",\"humidity\":\"" + String(rH, 0) +"\"}";
}


/*******************************/
/** Metrics collector methods **/
/******************************/

// TODO: move metrics methods to a standalone HTU2xDDeviceCollector class
// Follow Prometheus libs and exporters development recommendations

void HTU2xDDevice::collect(String *metrics) {
_metrics.clear();

metricTemp();
metricHumidity();
metricUncompHum();
metricTempReadErr();
metricHumidityReadErr();

*metrics = _metrics;
}

void HTU2xDDevice::metricTemp() {
static size_t const BUFSIZE = 256;
static char const *responseTemplate =
"# HELP htu2xd_temp_celsius Air temperature.\n"
"# TYPE htu2xd_temp_celsius gauge\n"
"htu2xd_temp_celsius{model=\"%s\"} %f\n";

char metric[BUFSIZE];
snprintf(metric, BUFSIZE, responseTemplate, HTU2XD_NAME, temp);
_metrics.concat(metric);
}

void HTU2xDDevice::metricHumidity() {
static size_t const BUFSIZE = 256;
static char const *responseTemplate =
"# HELP htu2xd_humidity_percent Air relative humidity.\n"
"# TYPE htu2xd_humidity_percent gauge\n"
"htu2xd_humidity_percent{model=\"%s\"} %f\n";

char metric[BUFSIZE];
snprintf(metric, BUFSIZE, responseTemplate, HTU2XD_NAME, rH);
_metrics.concat(metric);
}

void HTU2xDDevice::metricUncompHum() {
static size_t const BUFSIZE = 256;
static char const *responseTemplate =
"# HELP htu2xd_uncomp_humidity_percent Air relative humidity uncompensated.\n"
"# TYPE htu2xd_uncomp_humidity_percent gauge\n"
"htu2xd_uncomp_humidity_percent{model=\"%s\"} %f\n";

char metric[BUFSIZE];
snprintf(metric, BUFSIZE, responseTemplate, HTU2XD_NAME, htu2xD->readHumidity());
_metrics.concat(metric);
}

void HTU2xDDevice::metricTempReadErr() {
static size_t const BUFSIZE = 256;
static char const *responseTemplate =
"# HELP htu2xd_temp_read_errors_total Total number of temperature read errors.\n"
"# TYPE htu2xd_temp_read_errors_total counter\n"
"htu2xd_temp_read_errors_total{model=\"%s\"} %u\n";

char metric[BUFSIZE];
snprintf(metric, BUFSIZE, responseTemplate, HTU2XD_NAME, getTempReadErrs());
_metrics.concat(metric);
}

void HTU2xDDevice::metricHumidityReadErr() {
static size_t const BUFSIZE = 256;
static char const *responseTemplate =
"# HELP htu2xd_humidity_read_errors_total Total number of humidity read errors.\n"
"# TYPE htu2xd_humidity_read_errors_total counter\n"
"htu2xd_humidity_read_errors_total{model=\"%s\"} %u\n";

char metric[BUFSIZE];
snprintf(metric, BUFSIZE, responseTemplate, HTU2XD_NAME, getRHReadErrs());
_metrics.concat(metric);
}
60 changes: 60 additions & 0 deletions src/HTU2xDDevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* HTU2xDDevice.h
**/

#ifndef HTU2XDDEVICE_H
#define HTU2XDDEVICE_H

#include <Arduino.h>
#include <HTU2xD_SHT2x_Si70xx.h>
#include "Device.h"
#include "MetricsCollectable.h"

#define HTU2XD_NAME "HTU21D"
#define HTU2XD_RESPONSE_TIME_MS 10000

class HTU2xDDevice: public Device, MetricsCollectable {

public:
float temp, rH;

HTU2xDDevice(){
htu2xD = new HTU2xD_SHT2x_SI70xx(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT);
while (htu2xD->begin() != true) {
Serial.println(F("HTU2xD/SHT2x not connected, fail or VDD < +2.25v"));
delay(5000);
}
Serial.println(F("HTU2xD/SHT2x OK"));
}

void tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS);

void rhDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS);

void powerOn();

void powerOff();

String status();

void collect(String *metrics);

uint32_t getTempReadErrs() { return this->_tempReadErrs; }

uint32_t getRHReadErrs() { return this->_rHReadErrs; }

uint16_t getReadInterval() { return HTU2XD_RESPONSE_TIME_MS; }

private:
HTU2xD_SHT2x_SI70xx *htu2xD;
String _metrics;
uint32_t _tempReadErrs = 0, _rHReadErrs = 0;

void metricTemp();
void metricHumidity();
void metricUncompHum();
void metricTempReadErr();
void metricHumidityReadErr();
};

#endif
8 changes: 3 additions & 5 deletions src/WaterDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ void WaterDevice::scheduledWater(Scheduler scheduler, timer_callback pumpOffFunc
String WaterDevice::status() {
// TODO: generate from template using snprintf

// return "{\"date_time\":\"" + waterScheduler.getStartDateTime(ISO8601) +"\","
// + "\"date_time\":\"" + waterScheduler.getNextDateTime(ISO8601) + "\","
// + "\"days\":\"" + String(WATER_SCHEDULE.intervalDays) +"\","
// + "\"duration\":\"" + String(WATER_DURATION) +"\"}";
}
return "{\"days\":\"" + String(WATER_SCHEDULE.intervalDays) +"\","
+ "\"duration\":\"" + String(WATER_DURATION) +"\"}";
}
17 changes: 8 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* main.cpp
*
*
*/

#include <Arduino.h>
Expand All @@ -14,7 +14,7 @@
#include "endpoints.h"
#include "Device.h"
#include "WaterDevice.h"
#include "DHTDevice.h"
#include "HTU2xDDevice.h"
#include "LDRDevice.h"
#include "MetricsExporter.h"
#include "MetricsCollectable.h"
Expand All @@ -26,7 +26,6 @@
*/
#include "secrets.h"

#define DHTPIN D3
#define LAMPRELAYPIN D1
#define FANRELAYPIN D0
#define HUMRELAYPIN D4
Expand All @@ -51,15 +50,15 @@ Device fan(FANRELAYPIN);
Device hum(HUMRELAYPIN);
Device lamp(LAMPRELAYPIN);
WaterDevice waterDevice(PUMPPIN);
DHTDevice dht(DHTPIN);
LDRDevice ldr(LDRPIN);
HTU2xDDevice htu2xD; // HTU21D temperature and humidity sensor
LDRDevice ldr(LDRPIN); // Light-Dependent Resistor (photoresistor) - light sensor

Scheduler waterScheduler;
Scheduler lampOnScheduler, lampOffScheduler;
Scheduler fanOnScheduler, fanOffScheduler;
Scheduler humOnScheduler, humOffScheduler;

MetricsExporter dhtDeviceExporter((ESP8266WebServer*)&server, (MetricsCollectable*)&dht);
MetricsExporter htu2xDDeviceExporter((ESP8266WebServer*)&server, (MetricsCollectable*)&htu2xD);


/**************************/
Expand Down Expand Up @@ -102,8 +101,8 @@ void setup() {
initWiFi(WIFI_SSID, WIFI_PSK);

/* SimpleTimer function execution scheduling */
timer.setInterval(dht.dhtReadInterval, []{dht.tempDataHandler(&lamp, MAX_TEMP, TEMP_HYSTERESIS);});
timer.setInterval(dht.dhtReadInterval, []{dht.rhDataHandler(&hum, MAX_RH, RH_HYSTERESIS);});
timer.setInterval(htu2xD.getReadInterval(), []{htu2xD.tempDataHandler(&lamp, MAX_TEMP, TEMP_HYSTERESIS);});
timer.setInterval(htu2xD.getReadInterval(), []{htu2xD.rhDataHandler(&hum, MAX_RH, RH_HYSTERESIS);});
timer.setInterval(LIGHT_CHECK_INTERVAL * 1000, []{ldr.lampStatus();});

// TODO: candidate for debug logging
Expand All @@ -128,7 +127,7 @@ void setup() {
server.registerEndpoint(&fan, FAN_ENDPOINTS, sizeof(FAN_ENDPOINTS)/sizeof(FAN_ENDPOINTS[0]));
server.registerEndpoint(&hum, HUM_ENDPOINTS, sizeof(HUM_ENDPOINTS)/sizeof(HUM_ENDPOINTS[0]));
server.registerEndpoint(&waterDevice, WATER_ENDPOINTS, sizeof(WATER_ENDPOINTS)/sizeof(WATER_ENDPOINTS[0]));
server.registerEndpoint(&dht, DHT_ENDPOINTS, sizeof(DHT_ENDPOINTS)/sizeof(DHT_ENDPOINTS[0]));
server.registerEndpoint(&htu2xD, DHT_ENDPOINTS, sizeof(DHT_ENDPOINTS)/sizeof(DHT_ENDPOINTS[0]));
server.registerEndpoint(&ldr, LDR_ENDPOINTS, sizeof(LDR_ENDPOINTS)/sizeof(LDR_ENDPOINTS[0]));

server.begin();
Expand Down

0 comments on commit 9a09e4c

Please sign in to comment.