From 9cd0681e4d935f34416913756cffd43ae169a16d Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Wed, 21 Mar 2018 23:07:04 +0100 Subject: [PATCH] Add VEML6075 scaffolding. Also, remove DHT dependency and add a note in mos.yml to have application owners include those libraries. Without them, we'll compile no scaffolding. With them, we'll pull those in based on -DHAVE_* macros. --- mos.yml | 6 +++- src/mgos_prometheus_sensors.c | 2 ++ src/veml6075.c | 67 +++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/veml6075.c diff --git a/mos.yml b/mos.yml index bdab145..f627150 100644 --- a/mos.yml +++ b/mos.yml @@ -18,13 +18,17 @@ config_schema: - ["sensors", "o", {title: "Sensors settings"}] - ["sensors.dht_gpio", "s", "13,12,14", {title: "Comma Separated list of GPIO pins to enable DHT22/AM2302 sensors on"}] - ["sensors.dht_period", "i", 3, {title: "Sample period in seconds for DHT sensors"}] + - ["sensors.veml6075_i2caddr", "i", 0x10, {title: "I2C Address for VEML6075 sensor"}] + - ["sensors.veml6075_period", "i", 3, {title: "Sample period in seconds for VEML6075 sensor"}] - ["sensors.pushgateway_period", "i", 10, {title: "Period in seconds for Prometheus Pushgateway POSTs"}] - ["prometheus.pushgateway", "prometheus.example.net:9091"] # List of libraries used by this app, in order of initialisation +# Note: To use sensor libraries, include one or more of these in your application mos.yml: +# - origin: https://github.com/mongoose-os-libs/dht +# - origin: https://github.com/mongoose-os-libs/veml6075-i2c libs: - - origin: https://github.com/mongoose-os-libs/dht - origin: https://github.com/mongoose-os-libs/prometheus-metrics # Used by the mos tool to catch mos binaries incompatible with this file format diff --git a/src/mgos_prometheus_sensors.c b/src/mgos_prometheus_sensors.c index 36a8570..eddd85a 100644 --- a/src/mgos_prometheus_sensors.c +++ b/src/mgos_prometheus_sensors.c @@ -3,6 +3,7 @@ #include "mgos_prometheus_sensors.h" void dht_init(); +void veml6075_init(); static void pushgateway_timer(void *user_data) { mgos_prometheus_metrics_push(MGOS_APP, mgos_sys_config_get_device_id()); @@ -11,6 +12,7 @@ static void pushgateway_timer(void *user_data) { bool mgos_prometheus_sensors_init(void) { dht_init(); + veml6075_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); return true; diff --git a/src/veml6075.c b/src/veml6075.c new file mode 100644 index 0000000..3604f34 --- /dev/null +++ b/src/veml6075.c @@ -0,0 +1,67 @@ +#include "mgos.h" + +#ifdef MGOS_HAVE_VEML6075_I2C +#include "mgos_veml6075.h" +#include "mgos_config.h" +#include "mgos_prometheus_metrics.h" +#include "mgos_prometheus_sensors.h" + +static struct mgos_veml6075 *s_veml6075; + +static void print_chunk(struct mg_connection *nc, char *name, char *fmt, ...) { + char chunk[500]; + int chunklen=0; + va_list ap; + + snprintf(chunk, sizeof(chunk), "%s%s", name, fmt[0]=='{' ? "" : " "); + va_start(ap, fmt); + vsnprintf(chunk+strlen(chunk), sizeof(chunk)-strlen(chunk), fmt, ap); + va_end(ap); + strncat(chunk, "\n", sizeof(chunk)); + chunklen=strlen(chunk); +// LOG(LL_DEBUG, ("Chunk '%s' with length %d", chunk, chunklen)); + mg_printf(nc, "%X\r\n%s\r\n", chunklen, chunk); +} + +static void veml6075_prometheus_metrics(struct mg_connection *nc, void *user_data) { + // BUG -- repeated HELP and TYPE makes Prometheus parser bork :( + 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)); + print_chunk(nc, "UV", "{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)); + + (void) user_data; +} + +static void veml6075_timer_cb(void *user_data) { + 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); + LOG(LL_INFO, ("VEML6075 sensor=0 uva=%.1f uvb=%.1f uvindex=%.2f usecs=%u", uva, uvb, uvindex, usecs)); + + (void) user_data; +} + +void veml6075_init() { + s_veml6075 = mgos_veml6075_create(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_prometheus_metrics_add_handler(veml6075_prometheus_metrics, NULL); + } +} + +#else +void veml6075_init() { + LOG(LL_ERROR, ("VEML6075 disabled, include library in mos.yml to enable")); +} +#endif