Compare commits
2 Commits
d4a64fc8b9
...
9a028544bb
Author | SHA1 | Date |
---|---|---|
SeanOMik | 9a028544bb | |
SeanOMik | 5f47aff952 |
80
src/src.cpp
80
src/src.cpp
|
@ -169,9 +169,9 @@ void handle_http_root() {
|
||||||
log_request();
|
log_request();
|
||||||
static size_t const BUFSIZE = 256;
|
static size_t const BUFSIZE = 256;
|
||||||
static char const *response_template =
|
static char const *response_template =
|
||||||
"Prometheus ESP8266 DHT Exporter by HON95.\n"
|
"Prometheus ESP8266 DHT and SGP30 Exporter by SeanOMik.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Project: https://github.com/HON95/prometheus-esp8266-dht-exporter\n"
|
"Project: https://github.com/seanomik/prometheus-esp8266-dht-exporter\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Usage: %s\n";
|
"Usage: %s\n";
|
||||||
char response[BUFSIZE];
|
char response[BUFSIZE];
|
||||||
|
@ -185,20 +185,39 @@ void handle_http_metrics() {
|
||||||
|
|
||||||
uint32_t free_heap = system_get_free_heap_size();
|
uint32_t free_heap = system_get_free_heap_size();
|
||||||
|
|
||||||
static char const *response_template =
|
char* response = (char*) malloc(sizeof(char) * BUFSIZE);
|
||||||
"# HELP " PROM_NAMESPACE "_info Metadata about the device.\n"
|
memset(response, 0, strlen(response)+1); // clear the string to zero's
|
||||||
|
|
||||||
|
{
|
||||||
|
static const char* base_metrics_tmpl = "# HELP " PROM_NAMESPACE "_info Metadata about the device.\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_info gauge\n"
|
"# TYPE " PROM_NAMESPACE "_info gauge\n"
|
||||||
"# UNIT " PROM_NAMESPACE "_info \n"
|
"# UNIT " PROM_NAMESPACE "_info bool\n"
|
||||||
PROM_NAMESPACE "_info{version=\"%s\",board=\"%s\"} 1\n"
|
PROM_NAMESPACE "_info{version=\"%s\",board=\"%s\"} 1\n"
|
||||||
"# HELP " PROM_NAMESPACE "_free_heap Free heap\n"
|
"# HELP " PROM_NAMESPACE "_free_heap Free heap\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_free_heap gauge\n"
|
"# TYPE " PROM_NAMESPACE "_free_heap gauge\n"
|
||||||
"# UNIT " PROM_NAMESPACE "_free_heap byte\n"
|
"# UNIT " PROM_NAMESPACE "_free_heap byte\n"
|
||||||
PROM_NAMESPACE "_free_heap %d\n"
|
PROM_NAMESPACE "_free_heap %d\n";
|
||||||
"# HELP " PROM_NAMESPACE "_sensor Metadata about a sensor.\n"
|
|
||||||
|
int len = strlen(base_metrics_tmpl) + 100; // add 100 extra characters to fit the templated values
|
||||||
|
char* base_metrics = (char*) malloc(sizeof(char) * len);
|
||||||
|
snprintf(base_metrics, len, base_metrics_tmpl, VERSION, BOARD_NAME, free_heap);
|
||||||
|
strcat(response, base_metrics);
|
||||||
|
}
|
||||||
|
|
||||||
|
read_sensors();
|
||||||
|
|
||||||
|
// report dht metrics
|
||||||
|
if (isnan(humidity) || isnan(temperature) || isnan(heat_index)) {
|
||||||
|
static const char* dht_failure = "# HELP " PROM_NAMESPACE "_sensor Metadata and status of the sensor.\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_sensor gauge\n"
|
"# TYPE " PROM_NAMESPACE "_sensor gauge\n"
|
||||||
"# UNIT " PROM_NAMESPACE "_sensor \n"
|
"# UNIT " PROM_NAMESPACE "_sensor bool\n"
|
||||||
PROM_NAMESPACE "_sensor{sensor=\"%s\",serial=\"%s\"} 1\n"
|
PROM_NAMESPACE "_sensor{sensor=\"" DHT_NAME "\",serial=\"n/a\"} 0\n";
|
||||||
PROM_NAMESPACE "_sensor{sensor=\"%s\",serial=\"%s\"} 1\n"
|
strcat(response, dht_failure);
|
||||||
|
} else {
|
||||||
|
static const char* tmpl = "# HELP " PROM_NAMESPACE "_sensor Metadata and status of the sensor.\n"
|
||||||
|
"# TYPE " PROM_NAMESPACE "_sensor gauge\n"
|
||||||
|
"# UNIT " PROM_NAMESPACE "_sensor bool\n"
|
||||||
|
PROM_NAMESPACE "_sensor{sensor=\"" DHT_NAME "\",serial=\"n/a\"} 1\n"
|
||||||
"# HELP " PROM_NAMESPACE "_air_humidity_percent Air humidity.\n"
|
"# HELP " PROM_NAMESPACE "_air_humidity_percent Air humidity.\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_air_humidity_percent gauge\n"
|
"# TYPE " PROM_NAMESPACE "_air_humidity_percent gauge\n"
|
||||||
"# UNIT " PROM_NAMESPACE "_air_humidity_percent %%\n"
|
"# UNIT " PROM_NAMESPACE "_air_humidity_percent %%\n"
|
||||||
|
@ -210,14 +229,36 @@ void handle_http_metrics() {
|
||||||
"# HELP " PROM_NAMESPACE "_air_heat_index_celsius Apparent air temperature, based on temperature and humidity.\n"
|
"# HELP " PROM_NAMESPACE "_air_heat_index_celsius Apparent air temperature, based on temperature and humidity.\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_air_heat_index_celsius gauge\n"
|
"# TYPE " PROM_NAMESPACE "_air_heat_index_celsius gauge\n"
|
||||||
"# UNIT " PROM_NAMESPACE "_air_heat_index_celsius \u00B0C\n"
|
"# UNIT " PROM_NAMESPACE "_air_heat_index_celsius \u00B0C\n"
|
||||||
PROM_NAMESPACE "_air_heat_index_celsius %f\n"
|
PROM_NAMESPACE "_air_heat_index_celsius %f\n";
|
||||||
|
|
||||||
|
int len = strlen(tmpl) + 100; // add 100 extra characters to fit the templated values
|
||||||
|
char* templated = (char*) malloc(sizeof(char) * len);
|
||||||
|
snprintf(templated, len, tmpl, humidity, temperature, heat_index);
|
||||||
|
strcat(response, templated);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isnan(tvoc) || isnan(co2) || isnan(h2) || isnan(ethanol)) {
|
||||||
|
static const char* sgp30_failure_template = "# HELP " PROM_NAMESPACE "_sensor Metadata and status of the sensor.\n"
|
||||||
|
"# TYPE " PROM_NAMESPACE "_sensor gauge\n"
|
||||||
|
"# UNIT " PROM_NAMESPACE "_sensor bool\n"
|
||||||
|
PROM_NAMESPACE "_sensor{sensor=\"SGP30\",serial=\"%s\"} 0\n";
|
||||||
|
|
||||||
|
int len = strlen(sgp30_failure_template) + 100; // add 100 extra characters to fit the templated values
|
||||||
|
char* templated = (char*) malloc(sizeof(char) * len);
|
||||||
|
snprintf(templated, len, sgp30_failure_template, sgp30_serial);
|
||||||
|
strcat(response, templated);
|
||||||
|
} else {
|
||||||
|
static const char* tmpl = "# HELP " PROM_NAMESPACE "_sensor Metadata and status of the sensor.\n"
|
||||||
|
"# TYPE " PROM_NAMESPACE "_sensor gauge\n"
|
||||||
|
"# UNIT " PROM_NAMESPACE "_sensor bool\n"
|
||||||
|
PROM_NAMESPACE "_sensor{sensor=\"SGP30\",serial=\"%s\"} 1\n"
|
||||||
"# HELP " PROM_NAMESPACE "_air_quality_eco2 Equivalent calculated carbon-dioxide (eCO2).\n"
|
"# HELP " PROM_NAMESPACE "_air_quality_eco2 Equivalent calculated carbon-dioxide (eCO2).\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_air_quality_eco2 gauge\n"
|
"# TYPE " PROM_NAMESPACE "_air_quality_eco2 gauge\n"
|
||||||
"# UNIT " PROM_NAMESPACE "_air_quality_eco2 ppm\n"
|
"# UNIT " PROM_NAMESPACE "_air_quality_eco2 ppm\n"
|
||||||
PROM_NAMESPACE "_air_quality_eco2 %d\n"
|
PROM_NAMESPACE "_air_quality_eco2 %d\n"
|
||||||
"# HELP " PROM_NAMESPACE "_air_quality_tvoc Total Volatile Organic Compound (TVOC).\n"
|
"# HELP " PROM_NAMESPACE "_air_quality_tvoc Total Volatile Organic Compound (TVOC).\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_air_quality_tvoc gauge\n"
|
"# TYPE " PROM_NAMESPACE "_air_quality_tvoc gauge\n"
|
||||||
"# UNIT " PROM_NAMESPACE "_air_quality_tvoc ppb\\t\n"
|
"# UNIT " PROM_NAMESPACE "_air_quality_tvoc ppb/t\n"
|
||||||
PROM_NAMESPACE "_air_quality_tvoc %d\n"
|
PROM_NAMESPACE "_air_quality_tvoc %d\n"
|
||||||
"# HELP " PROM_NAMESPACE "_air_quality_h2 Hydrogen.\n"
|
"# HELP " PROM_NAMESPACE "_air_quality_h2 Hydrogen.\n"
|
||||||
"# TYPE " PROM_NAMESPACE "_air_quality_h2 gauge\n"
|
"# TYPE " PROM_NAMESPACE "_air_quality_h2 gauge\n"
|
||||||
|
@ -228,14 +269,14 @@ void handle_http_metrics() {
|
||||||
"# UNIT " PROM_NAMESPACE "_air_quality_ethanol ppm\n"
|
"# UNIT " PROM_NAMESPACE "_air_quality_ethanol ppm\n"
|
||||||
PROM_NAMESPACE "_air_quality_ethanol %d\n";
|
PROM_NAMESPACE "_air_quality_ethanol %d\n";
|
||||||
|
|
||||||
read_sensors();
|
int len = strlen(tmpl) + 100; // add 100 extra characters to fit the templated values
|
||||||
if (isnan(humidity) || isnan(temperature) || isnan(heat_index) || isnan(tvoc) || isnan(co2) || isnan(h2) || isnan(ethanol)) {
|
char* templated = (char*) malloc(sizeof(char) * len);
|
||||||
http_server.send(500, "text/plain; charset=utf-8", "Sensor error.");
|
snprintf(templated, len, tmpl, sgp30_serial, co2, tvoc, h2, ethanol);
|
||||||
return;
|
strcat(response, templated);
|
||||||
}
|
}
|
||||||
|
|
||||||
char response[BUFSIZE];
|
//char response[BUFSIZE];
|
||||||
snprintf(response, BUFSIZE, response_template, VERSION, BOARD_NAME, free_heap, DHT_NAME, "n/a", "SGP30", sgp30_serial, humidity, temperature, heat_index, tvoc, co2, h2, ethanol);
|
//snprintf(response, BUFSIZE, response_template, VERSION, BOARD_NAME, free_heap, DHT_NAME, "n/a", "SGP30", sgp30_serial, humidity, temperature, heat_index, tvoc, co2, h2, ethanol);
|
||||||
http_server.send(200, "text/plain; charset=utf-8", response);
|
http_server.send(200, "text/plain; charset=utf-8", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,7 +348,10 @@ float getAbsoluteHumidity(float t, float h) {
|
||||||
void read_sgp() {
|
void read_sgp() {
|
||||||
log("Reading air quality (SGP30) sensor ...", LogLevel::DEBUG);
|
log("Reading air quality (SGP30) sensor ...", LogLevel::DEBUG);
|
||||||
|
|
||||||
|
// only set humidity if dht reading was successful
|
||||||
|
if (!isnan(temperature) && !isnan(humidity)) {
|
||||||
sgp_sensor.setHumidity(getAbsoluteHumidity(temperature, humidity));
|
sgp_sensor.setHumidity(getAbsoluteHumidity(temperature, humidity));
|
||||||
|
}
|
||||||
|
|
||||||
if (sgp_sensor.IAQmeasure() && sgp_sensor.IAQmeasureRaw()) {
|
if (sgp_sensor.IAQmeasure() && sgp_sensor.IAQmeasureRaw()) {
|
||||||
tvoc = sgp_sensor.TVOC;
|
tvoc = sgp_sensor.TVOC;
|
||||||
|
|
Loading…
Reference in New Issue