Add OpenMetrics output
This commit is contained in:
parent
f90672fda2
commit
bc9a56762b
|
@ -26,3 +26,11 @@ This uses the Arduino IDE.
|
||||||
1. Set the correct settings for the board.
|
1. Set the correct settings for the board.
|
||||||
- WEMOS D1 Mini uses board "WeMoS D1 R2 & mini".
|
- WEMOS D1 Mini uses board "WeMoS D1 R2 & mini".
|
||||||
1. Build and upload using the Arduino IDE.
|
1. Build and upload using the Arduino IDE.
|
||||||
|
|
||||||
|
## Metrics
|
||||||
|
|
||||||
|
| Metric | Description | Unit |
|
||||||
|
| - | - | - |
|
||||||
|
| iot_humidity_percent | Air humidity. | `%` |
|
||||||
|
| iot_temperature_celsius | Air temperature. | `°C` |
|
||||||
|
| iot_heat_index_celsius | Apparent air temperature, based on temperature and humidity. | `°C` |
|
||||||
|
|
48
src/src.ino
48
src/src.ino
|
@ -23,8 +23,7 @@ void log(char const *message, LogLevel level=LogLevel::INFO);
|
||||||
DHTesp dht_sensor;
|
DHTesp dht_sensor;
|
||||||
ESP8266WebServer http_server(HTTP_SERVER_PORT);
|
ESP8266WebServer http_server(HTTP_SERVER_PORT);
|
||||||
|
|
||||||
float humidity, temperature;
|
float humidity, temperature, heat_index;
|
||||||
char str_humidity[10], str_temperature[10];
|
|
||||||
uint32_t previous_read_time = 0;
|
uint32_t previous_read_time = 0;
|
||||||
|
|
||||||
void setup(void) {
|
void setup(void) {
|
||||||
|
@ -71,19 +70,42 @@ void loop(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_http_home_client() {
|
void handle_http_home_client() {
|
||||||
static char const *response =
|
static size_t const BUFSIZE = 256;
|
||||||
|
static char const *response_template =
|
||||||
"Prometheus ESP8266 DHT Exporter by HON95.\n"
|
"Prometheus ESP8266 DHT Exporter by HON95.\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Project: https://github.com/HON95/prometheus-esp8266-dht-exporter\n"
|
"Project: https://github.com/HON95/prometheus-esp8266-dht-exporter\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Usage: " HTTP_METRICS_ENDPOINT "\n";
|
"Usage: %s\n";
|
||||||
|
char response[BUFSIZE];
|
||||||
|
snprintf(response, BUFSIZE, response_template, HTTP_METRICS_ENDPOINT);
|
||||||
http_server.send(200, "text/plain; charset=utf-8", response);
|
http_server.send(200, "text/plain; charset=utf-8", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_http_metrics_client() {
|
void handle_http_metrics_client() {
|
||||||
|
static size_t const BUFSIZE = 1024;
|
||||||
|
static char const *response_template =
|
||||||
|
"# HELP iot_humidity_percent Air humidity.\n"
|
||||||
|
"# TYPE iot_humidity_percent gauge\n"
|
||||||
|
"# UNIT iot_humidity_percent %%\n"
|
||||||
|
"iot_humidity_percent %f\n"
|
||||||
|
"# HELP iot_temperature_celsius Air temperature.\n"
|
||||||
|
"# TYPE iot_temperature_celsius gauge\n"
|
||||||
|
"# UNIT iot_temperature_celsius \u00B0C\n"
|
||||||
|
"iot_temperature_celsius %f\n"
|
||||||
|
"# HELP iot_heat_index_celsius Apparent air temperature, based on temperature and humidity.\n"
|
||||||
|
"# TYPE iot_heat_index_celsius gauge\n"
|
||||||
|
"# UNIT iot_heat_index_celsius \u00B0C\n"
|
||||||
|
"iot_heat_index_celsius %f\n";
|
||||||
|
|
||||||
read_sensors();
|
read_sensors();
|
||||||
char response[100];
|
if (isnan(humidity) || isnan(temperature) || isnan(heat_index)) {
|
||||||
snprintf(response, 100, "Temperature: %s\nHumidity: %s", str_temperature, str_humidity);
|
http_server.send(500, "text/plain; charset=utf-8", "Sensor error.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char response[BUFSIZE];
|
||||||
|
snprintf(response, BUFSIZE, response_template, humidity, temperature, heat_index);
|
||||||
http_server.send(200, "text/plain; charset=utf-8", response);
|
http_server.send(200, "text/plain; charset=utf-8", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +120,7 @@ void read_sensors(boolean force) {
|
||||||
|
|
||||||
read_humidity_sensor();
|
read_humidity_sensor();
|
||||||
read_temperature_sensor();
|
read_temperature_sensor();
|
||||||
// TODO float hic = dht.computeHeatIndex(temperature, humidity, false);
|
read_heat_index();
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_humidity_sensor() {
|
void read_humidity_sensor() {
|
||||||
|
@ -108,9 +130,7 @@ void read_humidity_sensor() {
|
||||||
}, &humidity);
|
}, &humidity);
|
||||||
if (result) {
|
if (result) {
|
||||||
humidity += HUMIDITY_CORRECTION_OFFSET;
|
humidity += HUMIDITY_CORRECTION_OFFSET;
|
||||||
snprintf(str_humidity, 10, "%.0f%%", humidity);
|
|
||||||
} else {
|
} else {
|
||||||
snprintf(str_humidity, 10, "ERROR");
|
|
||||||
log("Failed to read humidity sensor.", LogLevel::ERROR);
|
log("Failed to read humidity sensor.", LogLevel::ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,13 +142,19 @@ void read_temperature_sensor() {
|
||||||
}, &temperature);
|
}, &temperature);
|
||||||
if (result) {
|
if (result) {
|
||||||
temperature += TEMPERATURE_CORRECTION_OFFSET;
|
temperature += TEMPERATURE_CORRECTION_OFFSET;
|
||||||
snprintf(str_temperature, 10, "%.0f\u00B0C", temperature);
|
|
||||||
} else {
|
} else {
|
||||||
snprintf(str_temperature, 10, "ERROR");
|
|
||||||
log("Failed to read temperature sensor.", LogLevel::ERROR);
|
log("Failed to read temperature sensor.", LogLevel::ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void read_heat_index() {
|
||||||
|
if (!isnan(humidity) && !isnan(temperature)) {
|
||||||
|
heat_index = dht_sensor.computeHeatIndex(temperature, humidity, false);
|
||||||
|
} else {
|
||||||
|
heat_index = NAN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool read_sensor(float (*function)(), float *value) {
|
bool read_sensor(float (*function)(), float *value) {
|
||||||
bool success = false;
|
bool success = false;
|
||||||
for (int i = 0; i < READ_TRY_COUNT; i++) {
|
for (int i = 0; i < READ_TRY_COUNT; i++) {
|
||||||
|
|
Loading…
Reference in New Issue