Add barometer_drv.c
This commit is contained in:
1
mos.yml
1
mos.yml
@ -33,6 +33,7 @@ config_schema:
|
||||
- ["sensors.ccs811_i2caddr", "i", 0x5a, {title: "I2C Address for CCS811 sensor"}]
|
||||
- ["sensors.ccs811_period", "i", 3, {title: "Sample period in seconds for CCS811 sensor"}]
|
||||
- ["sensors.pushgateway_period", "i", 10, {title: "Period in seconds for Prometheus Pushgateway POSTs"}]
|
||||
- ["sensors.barometer_period", "i", 3, {title: "Sample period in seconds for barometer sensors"}]
|
||||
- ["prometheus.pushgateway", "prometheus.example.net:9091"]
|
||||
|
||||
|
||||
|
108
src/barometer_drv.c
Normal file
108
src/barometer_drv.c
Normal file
@ -0,0 +1,108 @@
|
||||
#include "mgos.h"
|
||||
|
||||
#ifdef MGOS_HAVE_BAROMETER
|
||||
#include "mgos_barometer.h"
|
||||
#include "mgos_config.h"
|
||||
#include "mgos_prometheus_metrics.h"
|
||||
#include "mgos_prometheus_sensors.h"
|
||||
#include <math.h>
|
||||
|
||||
#define NUM_BARO 5
|
||||
struct mgos_barometer *s_barometer[NUM_BARO];
|
||||
|
||||
static void barometer_prometheus_metrics(struct mg_connection *nc, void *user_data) {
|
||||
struct mgos_barometer_stats stats;
|
||||
float temperature=NAN, humidity=NAN, pressure=NAN;
|
||||
|
||||
for (int i=0; i<NUM_BARO && s_barometer[i]; i++) {
|
||||
if (mgos_barometer_get_temperature(s_barometer[i], &temperature)) {
|
||||
mgos_prometheus_metrics_printf(nc, GAUGE,
|
||||
"temperature", "Temperature in Celcius",
|
||||
"{sensor=\"%d\",type=\"BAROMETER\"} %f", i, temperature);
|
||||
}
|
||||
|
||||
if (mgos_barometer_get_pressure(s_barometer[i], &pressure)) {
|
||||
mgos_prometheus_metrics_printf(nc, GAUGE,
|
||||
"pressure", "Barometer pressure in Pascals",
|
||||
"{sensor=\"%d\", type=\"BAROMETER\"} %f", i, pressure);
|
||||
}
|
||||
|
||||
/*
|
||||
if (mgos_barometer_get_humidity(s_barometer[i], &humidity)) {
|
||||
mgos_prometheus_metrics_printf(nc, GAUGE,
|
||||
"humidity", "Relative humidity percentage",
|
||||
"{sensor=\"%d\",type=\"BAROMETER\"} %f", i, humidity);
|
||||
}
|
||||
*/
|
||||
|
||||
if (mgos_barometer_get_stats(s_barometer[i], &stats)) {
|
||||
mgos_prometheus_metrics_printf(nc, COUNTER,
|
||||
"sensor_read_total", "Total reads from sensor",
|
||||
"{sensor=\"%d\",type=\"BAROMETER\"} %u", i, stats.read);
|
||||
mgos_prometheus_metrics_printf(nc, COUNTER,
|
||||
"sensor_read_success_total", "Total successful reads from sensor",
|
||||
"{sensor=\"%d\",type=\"BAROMETER\"} %u", i, stats.read_success);
|
||||
mgos_prometheus_metrics_printf(nc, COUNTER,
|
||||
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
|
||||
"{sensor=\"%d\",type=\"BAROMETER\"} %u", i, stats.read_success_cached);
|
||||
uint32_t errors = stats.read - stats.read_success - stats.read_success_cached;
|
||||
mgos_prometheus_metrics_printf(nc, COUNTER,
|
||||
"sensor_read_error_total", "Total unsuccessful reads from sensor",
|
||||
"{sensor=\"%d\",type=\"BAROMETER\"} %u", i, errors);
|
||||
mgos_prometheus_metrics_printf(nc, COUNTER,
|
||||
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
|
||||
"{sensor=\"%d\",type=\"BAROMETER\"} %f", i, stats.read_success_usecs);
|
||||
}
|
||||
}
|
||||
|
||||
(void)user_data;
|
||||
(void)humidity;
|
||||
}
|
||||
|
||||
static void barometer_timer_cb(void *user_data) {
|
||||
struct mgos_barometer *baro = (struct mgos_barometer *)user_data;
|
||||
|
||||
float temperature=NAN, humidity=NAN, pressure=NAN;
|
||||
struct mgos_barometer_stats stats_before, stats_after;
|
||||
uint32_t usecs = 0;
|
||||
|
||||
mgos_barometer_get_stats(baro, &stats_before);
|
||||
mgos_barometer_get_temperature(baro, &temperature);
|
||||
mgos_barometer_get_pressure(baro, &pressure);
|
||||
// mgos_barometer_get_humidity(baro, &humidity);
|
||||
mgos_barometer_get_stats(baro, &stats_after);
|
||||
|
||||
usecs = stats_after.read_success_usecs - stats_before.read_success_usecs;
|
||||
LOG(LL_INFO, ("BAROMETER temperature=%.2fC humidity=%.1f%% pressure=%.0fPa usecs=%u", temperature, humidity, pressure, usecs));
|
||||
|
||||
(void)user_data;
|
||||
}
|
||||
|
||||
static struct mgos_barometer *barometer_create(struct mgos_i2c *i2c, uint8_t i2caddr, enum mgos_barometer_type type) {
|
||||
struct mgos_barometer *b;
|
||||
b=mgos_barometer_create_i2c(i2c, i2caddr, type);
|
||||
if (!b) return NULL;
|
||||
mgos_barometer_set_cache_ttl(b, 1000);
|
||||
mgos_set_timer(mgos_sys_config_get_sensors_barometer_period() * 1000, true, barometer_timer_cb, b);
|
||||
return b;
|
||||
}
|
||||
|
||||
void barometer_drv_init() {
|
||||
int num_baro=0;
|
||||
|
||||
if ((s_barometer[num_baro] = barometer_create(mgos_i2c_get_global(), 0x76, BARO_BME280)))
|
||||
num_baro++;
|
||||
|
||||
if ((s_barometer[num_baro] = barometer_create(mgos_i2c_get_global(), 0x60, BARO_MPL115)))
|
||||
num_baro++;
|
||||
|
||||
if (num_baro>0)
|
||||
mgos_prometheus_metrics_add_handler(barometer_prometheus_metrics, NULL);
|
||||
}
|
||||
|
||||
#else
|
||||
void barometer_drv_init() {
|
||||
LOG(LL_ERROR, ("Barometer disabled, include library in mos.yml to enable"));
|
||||
}
|
||||
|
||||
#endif
|
@ -10,6 +10,7 @@ void si7021_drv_init();
|
||||
void htu21df_drv_init();
|
||||
void mcp9808_drv_init();
|
||||
void ccs811_drv_init();
|
||||
void barometer_drv_init();
|
||||
|
||||
static void pushgateway_timer(void *user_data) {
|
||||
mgos_prometheus_metrics_push(MGOS_APP, mgos_sys_config_get_device_id());
|
||||
@ -25,6 +26,7 @@ bool mgos_prometheus_sensors_init(void) {
|
||||
htu21df_drv_init();
|
||||
mcp9808_drv_init();
|
||||
ccs811_drv_init();
|
||||
barometer_drv_init();
|
||||
|
||||
if (mgos_sys_config_get_sensors_pushgateway_period() > 0) {
|
||||
mgos_set_timer(mgos_sys_config_get_sensors_pushgateway_period() * 1000, true, pushgateway_timer, NULL);
|
||||
|
Reference in New Issue
Block a user