Code Formatting (uncrustify)

This commit is contained in:
Pim van Pelt
2018-04-09 19:44:04 +02:00
parent 9067ab01dd
commit 096192138c
9 changed files with 194 additions and 203 deletions

View File

@ -6,4 +6,5 @@
float mgos_prometheus_sensors_dht_get_temp(uint8_t idx);
float mgos_prometheus_sensors_dht_get_humidity(uint8_t idx);
#endif // __MGOS_PROMETHEUS_SENSORS_H

View File

@ -6,88 +6,86 @@
#include "mgos_prometheus_metrics.h"
#include "mgos_prometheus_sensors.h"
static struct mgos_bme280 *s_bme280;
static struct mgos_bme280 * s_bme280;
static struct mgos_bme280_data s_bme280_data;
static void bme280_prometheus_metrics(struct mg_connection *nc, void *user_data) {
bool bme280;
struct mgos_bme280_stats stats;
if (!s_bme280) return;
bme280=mgos_bme280_is_bme280(s_bme280);
if (!s_bme280) {
return;
}
bme280 = mgos_bme280_is_bme280(s_bme280);
mgos_prometheus_metrics_printf(nc, GAUGE,
"pressure", "Barometer pressure in HPa",
"{sensor=\"0\", type=\"BM%s280\"} %f", bme280?"E":"P", s_bme280_data.press);
"pressure", "Barometer pressure in HPa",
"{sensor=\"0\", type=\"BM%s280\"} %f", bme280 ? "E" : "P", s_bme280_data.press);
mgos_prometheus_metrics_printf(nc, GAUGE,
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"BM%s280\"} %f", bme280?"E":"P", s_bme280_data.temp);
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"BM%s280\"} %f", bme280 ? "E" : "P", s_bme280_data.temp);
if (mgos_bme280_is_bme280(s_bme280)) {
mgos_prometheus_metrics_printf(nc, GAUGE,
"humidity", "Relative humidity in percent",
"{sensor=\"0\",type=\"BM%s280\"} %f", bme280?"E":"P", s_bme280_data.humid);
"humidity", "Relative humidity in percent",
"{sensor=\"0\",type=\"BM%s280\"} %f", bme280 ? "E" : "P", s_bme280_data.humid);
}
if (mgos_bme280_getStats(s_bme280, &stats)) {
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %u", bme280?"E":"P", stats.read);
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %u", bme280 ? "E" : "P", stats.read);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %u", bme280?"E":"P", stats.read_success);
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %u", bme280 ? "E" : "P", stats.read_success);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %u", bme280?"E":"P", stats.read_success_cached);
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %u", bme280 ? "E" : "P", 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=\"0\",type=\"BM%s280\"} %u", bme280?"E":"P", errors);
"sensor_read_error_total", "Total unsuccessful reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %u", bme280 ? "E" : "P", errors);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %f", bme280?"E":"P", stats.read_success_usecs);
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"BM%s280\"} %f", bme280 ? "E" : "P", stats.read_success_usecs);
}
(void) user_data;
(void)user_data;
}
static void bme280_timer_cb(void *user_data) {
struct mgos_bme280_stats stats_before, stats_after;
uint32_t usecs=0;
uint32_t usecs = 0;
mgos_bme280_getStats(s_bme280, &stats_before);
mgos_bme280_read(s_bme280, &s_bme280_data);
mgos_bme280_getStats(s_bme280, &stats_after);
usecs=stats_after.read_success_usecs - stats_before.read_success_usecs;
usecs = stats_after.read_success_usecs - stats_before.read_success_usecs;
if (mgos_bme280_is_bme280(s_bme280)) {
LOG(LL_INFO, ("BME280 sensor=0 humidity=%.2f%% temperature=%.2fC pressure=%.1fHPa usecs=%u", s_bme280_data.humid, s_bme280_data.temp, s_bme280_data.press, usecs));
}
else {
}else {
LOG(LL_INFO, ("BMP280 sensor=0 temperature=%.2fC pressure=%.1fHPa usecs=%u", s_bme280_data.temp, s_bme280_data.press, usecs));
}
(void) user_data;
(void)user_data;
}
void bme280_drv_init() {
s_bme280 = mgos_bme280_i2c_create(mgos_sys_config_get_sensors_bme280_i2caddr());
if (s_bme280) {
mgos_set_timer(mgos_sys_config_get_sensors_bme280_period()*1000, true, bme280_timer_cb, NULL);
mgos_set_timer(mgos_sys_config_get_sensors_bme280_period() * 1000, true, bme280_timer_cb, NULL);
mgos_prometheus_metrics_add_handler(bme280_prometheus_metrics, NULL);
}
else {
}else {
LOG(LL_ERROR, ("Could not create BME280 sensor on I2C address 0x%02x", mgos_sys_config_get_sensors_bme280_i2caddr()));
}
}
#else
void bme280_drv_init() {
LOG(LL_ERROR, ("BME280 disabled, include library in mos.yml to enable"));
}
#endif

View File

@ -6,15 +6,14 @@
#include "mgos_prometheus_metrics.h"
#include "mgos_prometheus_sensors.h"
#define MAX_DHT 8
#define MAX_DHT 8
struct dht_sensor
{
struct dht_sensor {
struct mgos_dht *dht;
float temp;
float humidity;
uint8_t gpio;
uint8_t idx;
float temp;
float humidity;
uint8_t gpio;
uint8_t idx;
};
static struct dht_sensor *s_dht_sensor[MAX_DHT];
@ -24,116 +23,119 @@ static void dht_prometheus_metrics(struct mg_connection *nc, void *user_data) {
int i;
struct mgos_dht_stats stats;
for (i=0; i<s_num_dht; i++) {
for (i = 0; i < s_num_dht; i++) {
mgos_prometheus_metrics_printf(nc, GAUGE,
"temperature", "Temperature in Celcius",
"{sensor=\"%d\",type=\"DHT\"} %f", i, s_dht_sensor[i]->temp);
"temperature", "Temperature in Celcius",
"{sensor=\"%d\",type=\"DHT\"} %f", i, s_dht_sensor[i]->temp);
mgos_prometheus_metrics_printf(nc, GAUGE,
"humidity", "Relative humidity percentage",
"{sensor=\"%d\",type=\"DHT\"} %f", i, s_dht_sensor[i]->humidity);
"humidity", "Relative humidity percentage",
"{sensor=\"%d\",type=\"DHT\"} %f", i, s_dht_sensor[i]->humidity);
if (mgos_dht_getStats(s_dht_sensor[i]->dht, &stats)) {
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_total", "Total reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %u", i, stats.read);
"sensor_read_total", "Total reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %u", i, stats.read);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %u", i, stats.read_success);
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %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=\"DHT\"} %u", i, stats.read_success_cached);
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %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=\"DHT\"} %u", i, errors);
"sensor_read_error_total", "Total unsuccessful reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %u", i, errors);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %f", i, stats.read_success_usecs);
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"%d\",type=\"DHT\"} %f", i, stats.read_success_usecs);
}
}
(void) user_data;
(void)user_data;
}
static void dht_timer_cb(void *user_data) {
struct dht_sensor *dht_sensor = (struct dht_sensor *)user_data;
struct dht_sensor * dht_sensor = (struct dht_sensor *)user_data;
struct mgos_dht_stats stats_before, stats_after;
uint32_t usecs=0;
uint32_t usecs = 0;
if (!dht_sensor) return;
if (!dht_sensor) {
return;
}
mgos_dht_getStats(dht_sensor->dht, &stats_before);
dht_sensor->temp = mgos_dht_get_temp(dht_sensor->dht);
dht_sensor->temp = mgos_dht_get_temp(dht_sensor->dht);
dht_sensor->humidity = mgos_dht_get_humidity(dht_sensor->dht);
mgos_dht_getStats(dht_sensor->dht, &stats_after);
usecs=stats_after.read_success_usecs - stats_before.read_success_usecs;
usecs = stats_after.read_success_usecs - stats_before.read_success_usecs;
LOG(LL_INFO, ("DHT sensor=%u gpio=%u temperature=%.2fC humidity=%.1f%% usecs=%u", dht_sensor->idx, dht_sensor->gpio, dht_sensor->temp, dht_sensor->humidity, usecs));
}
static bool dht_sensor_create(int pin, enum dht_type type) {
struct dht_sensor *dht_sensor = calloc(1, sizeof(struct dht_sensor));
if (s_num_dht == MAX_DHT) {
LOG(LL_ERROR, ("No more sensor slots available (%d added)", MAX_DHT));
free(dht_sensor);
return false;
}
dht_sensor->dht=mgos_dht_create(pin, type);
dht_sensor->dht = mgos_dht_create(pin, type);
if (!dht_sensor->dht) {
LOG(LL_ERROR, ("Could not create DHT sensor on pin %d", pin));
free(dht_sensor);
return false;
}
dht_sensor->gpio=pin;
dht_sensor->idx=s_num_dht;
dht_sensor->gpio = pin;
dht_sensor->idx = s_num_dht;
s_dht_sensor[dht_sensor->idx] = dht_sensor;
s_num_dht++;
mgos_set_timer(mgos_sys_config_get_sensors_dht_period()*1000, true, dht_timer_cb, (void*)dht_sensor);
mgos_set_timer(mgos_sys_config_get_sensors_dht_period() * 1000, true, dht_timer_cb, (void *)dht_sensor);
return true;
}
float mgos_prometheus_sensors_dht_get_temp(uint8_t idx) {
if (idx>=s_num_dht)
if (idx >= s_num_dht) {
return NAN;
if (!s_dht_sensor[idx])
}
if (!s_dht_sensor[idx]) {
return NAN;
}
return s_dht_sensor[idx]->temp;
}
float mgos_prometheus_sensors_dht_get_humidity(uint8_t idx) {
if (idx>=s_num_dht)
if (idx >= s_num_dht) {
return NAN;
if (!s_dht_sensor[idx])
}
if (!s_dht_sensor[idx]) {
return NAN;
}
return s_dht_sensor[idx]->humidity;
}
void dht_drv_init() {
char *tok;
memset(s_dht_sensor, 0, sizeof (struct mgos_dht_sensor *) * MAX_DHT);
memset(s_dht_sensor, 0, sizeof(struct mgos_dht_sensor *) * MAX_DHT);
tok = strtok((char *)mgos_sys_config_get_sensors_dht_gpio(), ", ");
while (tok) {
int gpio;
gpio = atoi(tok);
tok=strtok(NULL, ", ");
tok = strtok(NULL, ", ");
dht_sensor_create(gpio, AM2302);
mgos_msleep(250);
}
if (s_num_dht>0)
if (s_num_dht > 0) {
mgos_prometheus_metrics_add_handler(dht_prometheus_metrics, NULL);
}
}
#else
void dht_drv_init() {
LOG(LL_ERROR, ("DHT disabled, include library in mos.yml to enable"));
}
#endif

View File

@ -12,66 +12,64 @@ static void htu21df_prometheus_metrics(struct mg_connection *nc, void *user_data
struct mgos_htu21df_stats stats;
mgos_prometheus_metrics_printf(nc, GAUGE,
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"HTU21DF\"} %f", mgos_htu21df_getTemperature(s_htu21df));
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"HTU21DF\"} %f", mgos_htu21df_getTemperature(s_htu21df));
mgos_prometheus_metrics_printf(nc, GAUGE,
"humidity", "Relative humidity percentage",
"{sensor=\"0\",type=\"HTU21DF\"} %f", mgos_htu21df_getHumidity(s_htu21df));
"humidity", "Relative humidity percentage",
"{sensor=\"0\",type=\"HTU21DF\"} %f", mgos_htu21df_getHumidity(s_htu21df));
if (mgos_htu21df_getStats(s_htu21df, &stats)) {
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %u", stats.read);
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %u", stats.read);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %u", stats.read_success);
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %u", stats.read_success);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %u", stats.read_success_cached);
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %u", 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=\"0\",type=\"HTU21DF\"} %u", errors);
"sensor_read_error_total", "Total unsuccessful reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %u", errors);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %f", stats.read_success_usecs);
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"HTU21DF\"} %f", stats.read_success_usecs);
}
(void) user_data;
(void)user_data;
}
static void htu21df_timer_cb(void *user_data) {
float temperature, humidity;
struct mgos_htu21df_stats stats_before, stats_after;
uint32_t usecs=0;
uint32_t usecs = 0;
mgos_htu21df_getStats(s_htu21df, &stats_before);
temperature=mgos_htu21df_getTemperature(s_htu21df);
humidity=mgos_htu21df_getHumidity(s_htu21df);
temperature = mgos_htu21df_getTemperature(s_htu21df);
humidity = mgos_htu21df_getHumidity(s_htu21df);
mgos_htu21df_getStats(s_htu21df, &stats_after);
usecs=stats_after.read_success_usecs - stats_before.read_success_usecs;
usecs = stats_after.read_success_usecs - stats_before.read_success_usecs;
LOG(LL_INFO, ("HTU21DF sensor=0 temperature=%.2fC humidity=%.1f%% usecs=%u", temperature, humidity, usecs));
(void) user_data;
(void)user_data;
}
void htu21df_drv_init() {
#ifdef MGOS_HAVE_SI7021_I2C
LOG(LL_WARN, ("HTU21DF and SI7021 are both on I2C address 0x40 -- do not enable both!"));
#endif
s_htu21df = mgos_htu21df_create(mgos_i2c_get_global(), mgos_sys_config_get_sensors_htu21df_i2caddr());
if (s_htu21df) {
mgos_set_timer(mgos_sys_config_get_sensors_htu21df_period()*1000, true, htu21df_timer_cb, NULL);
mgos_set_timer(mgos_sys_config_get_sensors_htu21df_period() * 1000, true, htu21df_timer_cb, NULL);
mgos_prometheus_metrics_add_handler(htu21df_prometheus_metrics, NULL);
}
}
#else
void htu21df_drv_init() {
LOG(LL_ERROR, ("HTU21DF disabled, include library in mos.yml to enable"));
}
#endif

View File

@ -12,59 +12,57 @@ static void mcp9808_prometheus_metrics(struct mg_connection *nc, void *user_data
struct mgos_mcp9808_stats stats;
mgos_prometheus_metrics_printf(nc, GAUGE,
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"MCP9808\"} %f", mgos_mcp9808_getTemperature(s_mcp9808));
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"MCP9808\"} %f", mgos_mcp9808_getTemperature(s_mcp9808));
if (mgos_mcp9808_getStats(s_mcp9808, &stats)) {
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %u", stats.read);
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %u", stats.read);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %u", stats.read_success);
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %u", stats.read_success);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %u", stats.read_success_cached);
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %u", 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=\"0\",type=\"MCP9808\"} %u", errors);
"sensor_read_error_total", "Total unsuccessful reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %u", errors);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %f", stats.read_success_usecs);
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"MCP9808\"} %f", stats.read_success_usecs);
}
(void) user_data;
(void)user_data;
}
static void mcp9808_timer_cb(void *user_data) {
float temperature;
struct mgos_mcp9808_stats stats_before, stats_after;
uint32_t usecs=0;
uint32_t usecs = 0;
mgos_mcp9808_getStats(s_mcp9808, &stats_before);
temperature=mgos_mcp9808_getTemperature(s_mcp9808);
temperature = mgos_mcp9808_getTemperature(s_mcp9808);
mgos_mcp9808_getStats(s_mcp9808, &stats_after);
usecs=stats_after.read_success_usecs - stats_before.read_success_usecs;
usecs = stats_after.read_success_usecs - stats_before.read_success_usecs;
LOG(LL_INFO, ("MCP9808 sensor=0 temperature=%.2fC usecs=%u", temperature, usecs));
(void) user_data;
(void)user_data;
}
void mcp9808_drv_init() {
s_mcp9808 = mgos_mcp9808_create(mgos_i2c_get_global(), mgos_sys_config_get_sensors_mcp9808_i2caddr());
if (s_mcp9808) {
mgos_set_timer(mgos_sys_config_get_sensors_mcp9808_period()*1000, true, mcp9808_timer_cb, NULL);
mgos_set_timer(mgos_sys_config_get_sensors_mcp9808_period() * 1000, true, mcp9808_timer_cb, NULL);
mgos_prometheus_metrics_add_handler(mcp9808_prometheus_metrics, NULL);
}
}
#else
void mcp9808_drv_init() {
LOG(LL_ERROR, ("MCP9808 disabled, include library in mos.yml to enable"));
}
#endif

View File

@ -12,10 +12,9 @@ void mcp9808_drv_init();
static void pushgateway_timer(void *user_data) {
mgos_prometheus_metrics_push(MGOS_APP, mgos_sys_config_get_device_id());
(void) user_data;
(void)user_data;
}
bool mgos_prometheus_sensors_init(void) {
dht_drv_init();
veml6075_drv_init();
@ -25,7 +24,8 @@ bool mgos_prometheus_sensors_init(void) {
htu21df_drv_init();
mcp9808_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);
if (mgos_sys_config_get_sensors_pushgateway_period() > 0) {
mgos_set_timer(mgos_sys_config_get_sensors_pushgateway_period() * 1000, true, pushgateway_timer, NULL);
}
return true;
}

View File

@ -12,65 +12,63 @@ static void sht31_prometheus_metrics(struct mg_connection *nc, void *user_data)
struct mgos_sht31_stats stats;
mgos_prometheus_metrics_printf(nc, GAUGE,
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"SHT31\"} %f", mgos_sht31_getTemperature(s_sht31));
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"SHT31\"} %f", mgos_sht31_getTemperature(s_sht31));
mgos_prometheus_metrics_printf(nc, GAUGE,
"humidity", "Relative humidity percentage",
"{sensor=\"0\",type=\"SHT31\"} %f", mgos_sht31_getHumidity(s_sht31));
"humidity", "Relative humidity percentage",
"{sensor=\"0\",type=\"SHT31\"} %f", mgos_sht31_getHumidity(s_sht31));
if (mgos_sht31_getStats(s_sht31, &stats)) {
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %u", stats.read);
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %u", stats.read);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %u", stats.read_success);
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %u", stats.read_success);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %u", stats.read_success_cached);
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %u", 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=\"0\",type=\"SHT31\"} %u", errors);
"sensor_read_error_total", "Total unsuccessful reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %u", errors);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %f", stats.read_success_usecs);
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"SHT31\"} %f", stats.read_success_usecs);
}
(void) user_data;
(void)user_data;
}
static void sht31_timer_cb(void *user_data) {
float temperature, humidity;
struct mgos_sht31_stats stats_before, stats_after;
uint32_t usecs=0;
uint32_t usecs = 0;
mgos_sht31_getStats(s_sht31, &stats_before);
temperature=mgos_sht31_getTemperature(s_sht31);
humidity=mgos_sht31_getHumidity(s_sht31);
temperature = mgos_sht31_getTemperature(s_sht31);
humidity = mgos_sht31_getHumidity(s_sht31);
mgos_sht31_getStats(s_sht31, &stats_after);
usecs=stats_after.read_success_usecs - stats_before.read_success_usecs;
usecs = stats_after.read_success_usecs - stats_before.read_success_usecs;
LOG(LL_INFO, ("SHT31 sensor=0 temperature=%.2fC humidity=%.1f%% usecs=%u", temperature, humidity, usecs));
(void) user_data;
(void)user_data;
}
void sht31_drv_init() {
#ifdef MGOS_HAVE_SI7021_I2C
#endif
s_sht31 = mgos_sht31_create(mgos_i2c_get_global(), mgos_sys_config_get_sensors_sht31_i2caddr());
if (s_sht31) {
mgos_set_timer(mgos_sys_config_get_sensors_sht31_period()*1000, true, sht31_timer_cb, NULL);
mgos_set_timer(mgos_sys_config_get_sensors_sht31_period() * 1000, true, sht31_timer_cb, NULL);
mgos_prometheus_metrics_add_handler(sht31_prometheus_metrics, NULL);
}
}
#else
void sht31_drv_init() {
LOG(LL_ERROR, ("SHT31 disabled, include library in mos.yml to enable"));
}
#endif

View File

@ -12,66 +12,64 @@ static void si7021_prometheus_metrics(struct mg_connection *nc, void *user_data)
struct mgos_si7021_stats stats;
mgos_prometheus_metrics_printf(nc, GAUGE,
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"SI7021\"} %f", mgos_si7021_getTemperature(s_si7021));
"temperature", "Temperature in Celcius",
"{sensor=\"0\",type=\"SI7021\"} %f", mgos_si7021_getTemperature(s_si7021));
mgos_prometheus_metrics_printf(nc, GAUGE,
"humidity", "Relative humidity percentage",
"{sensor=\"0\",type=\"SI7021\"} %f", mgos_si7021_getHumidity(s_si7021));
"humidity", "Relative humidity percentage",
"{sensor=\"0\",type=\"SI7021\"} %f", mgos_si7021_getHumidity(s_si7021));
if (mgos_si7021_getStats(s_si7021, &stats)) {
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %u", stats.read);
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %u", stats.read);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %u", stats.read_success);
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %u", stats.read_success);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %u", stats.read_success_cached);
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %u", 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=\"0\",type=\"SI7021\"} %u", errors);
"sensor_read_error_total", "Total unsuccessful reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %u", errors);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %f", stats.read_success_usecs);
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"SI7021\"} %f", stats.read_success_usecs);
}
(void) user_data;
(void)user_data;
}
static void si7021_timer_cb(void *user_data) {
float temperature, humidity;
struct mgos_si7021_stats stats_before, stats_after;
uint32_t usecs=0;
uint32_t usecs = 0;
mgos_si7021_getStats(s_si7021, &stats_before);
temperature=mgos_si7021_getTemperature(s_si7021);
humidity=mgos_si7021_getHumidity(s_si7021);
temperature = mgos_si7021_getTemperature(s_si7021);
humidity = mgos_si7021_getHumidity(s_si7021);
mgos_si7021_getStats(s_si7021, &stats_after);
usecs=stats_after.read_success_usecs - stats_before.read_success_usecs;
usecs = stats_after.read_success_usecs - stats_before.read_success_usecs;
LOG(LL_INFO, ("SI7021 sensor=0 temperature=%.2fC humidity=%.1f%% usecs=%u", temperature, humidity, usecs));
(void) user_data;
(void)user_data;
}
void si7021_drv_init() {
#ifdef MGOS_HAVE_HTU21DF_I2C
LOG(LL_WARN, ("HTU21DF and SI7021 are both on I2C address 0x40 -- do not enable both!"));
#endif
s_si7021 = mgos_si7021_create(mgos_i2c_get_global(), mgos_sys_config_get_sensors_si7021_i2caddr());
if (s_si7021) {
mgos_set_timer(mgos_sys_config_get_sensors_si7021_period()*1000, true, si7021_timer_cb, NULL);
mgos_set_timer(mgos_sys_config_get_sensors_si7021_period() * 1000, true, si7021_timer_cb, NULL);
mgos_prometheus_metrics_add_handler(si7021_prometheus_metrics, NULL);
}
}
#else
void si7021_drv_init() {
LOG(LL_ERROR, ("SI7021 disabled, include library in mos.yml to enable"));
}
#endif

View File

@ -12,65 +12,63 @@ static void veml6075_prometheus_metrics(struct mg_connection *nc, void *user_dat
struct mgos_veml6075_stats stats;
mgos_prometheus_metrics_printf(nc, GAUGE,
"UV", "Ultra Violet light intensity, in sensor counts",
"{band=\"UVA\",type=\"VEML6075\", sensor=\"0\"} %f", mgos_veml6075_getUVA(s_veml6075));
"UV", "Ultra Violet light intensity, in sensor counts",
"{band=\"UVA\",type=\"VEML6075\", sensor=\"0\"} %f", mgos_veml6075_getUVA(s_veml6075));
mgos_prometheus_metrics_printf(nc, GAUGE,
"UV", "Ultra Violet light intensity, in sensor counts",
"{band=\"UVB\",type=\"VEML6075\", sensor=\"0\"} %f", mgos_veml6075_getUVB(s_veml6075));
"UV", "Ultra Violet light intensity, in sensor counts",
"{band=\"UVB\",type=\"VEML6075\", sensor=\"0\"} %f", mgos_veml6075_getUVB(s_veml6075));
mgos_prometheus_metrics_printf(nc, GAUGE,
"UVIndex", "2: Low, 5.5 Moderate, 7.5 High, 10.5 Very High, else Extreme",
"{sensor=\"0\",type=\"VEML6075\"} %f", mgos_veml6075_getUVIndex(s_veml6075));
"UVIndex", "2: Low, 5.5 Moderate, 7.5 High, 10.5 Very High, else Extreme",
"{sensor=\"0\",type=\"VEML6075\"} %f", mgos_veml6075_getUVIndex(s_veml6075));
if (mgos_veml6075_getStats(s_veml6075, &stats)) {
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %u", stats.read);
"sensor_read_total", "Total reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %u", stats.read);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %u", stats.read_success);
"sensor_read_success_total", "Total successful reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %u", stats.read_success);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %u", stats.read_success_cached);
"sensor_read_success_cached_total", "Total successful cached reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %u", 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=\"0\",type=\"VEML6075\"} %u", errors);
"sensor_read_error_total", "Total unsuccessful reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %u", errors);
mgos_prometheus_metrics_printf(nc, COUNTER,
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %f", stats.read_success_usecs);
"sensor_read_success_usecs_total", "Total microseconds spent in reads from sensor",
"{sensor=\"0\",type=\"VEML6075\"} %f", stats.read_success_usecs);
}
(void) user_data;
(void)user_data;
}
static void veml6075_timer_cb(void *user_data) {
double start;
uint32_t usecs=0;
float uva, uvb, uvindex;
double start;
uint32_t usecs = 0;
float uva, uvb, uvindex;
start=mgos_uptime();
uva=mgos_veml6075_getUVA(s_veml6075);
uvb=mgos_veml6075_getUVB(s_veml6075);
uvindex=mgos_veml6075_getUVIndex(s_veml6075);
usecs=1000000*(mgos_uptime()-start);
start = mgos_uptime();
uva = mgos_veml6075_getUVA(s_veml6075);
uvb = mgos_veml6075_getUVB(s_veml6075);
uvindex = mgos_veml6075_getUVIndex(s_veml6075);
usecs = 1000000 * (mgos_uptime() - start);
LOG(LL_INFO, ("VEML6075 sensor=0 uva=%.1f uvb=%.1f uvindex=%.2f usecs=%u", uva, uvb, uvindex, usecs));
(void) user_data;
(void)user_data;
}
void veml6075_drv_init() {
s_veml6075 = mgos_veml6075_create(mgos_i2c_get_global(), mgos_sys_config_get_sensors_veml6075_i2caddr());
if (s_veml6075) {
mgos_set_timer(mgos_sys_config_get_sensors_veml6075_period()*1000, true, veml6075_timer_cb, NULL);
mgos_set_timer(mgos_sys_config_get_sensors_veml6075_period() * 1000, true, veml6075_timer_cb, NULL);
mgos_prometheus_metrics_add_handler(veml6075_prometheus_metrics, NULL);
}
}
#else
void veml6075_drv_init() {
LOG(LL_ERROR, ("VEML6075 disabled, include library in mos.yml to enable"));
}
#endif