From affa077d61fdad2d10820d08ff4150261f6dc5ef Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Tue, 3 Apr 2018 19:52:52 +0200 Subject: [PATCH] Add four new drivers! HTU21DF SHT31 SI7021 MCP9808 .. which will be published to GitHub after launch review at Google. --- README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++ mos.yml | 9 +++++++ src/htu21df_drv.c | 48 +++++++++++++++++++++++++++++++++++ src/mcp9808_drv.c | 43 +++++++++++++++++++++++++++++++ src/sht31_drv.c | 48 +++++++++++++++++++++++++++++++++++ src/si7021_drv.c | 48 +++++++++++++++++++++++++++++++++++ 6 files changed, 260 insertions(+) create mode 100644 src/htu21df_drv.c create mode 100644 src/mcp9808_drv.c create mode 100644 src/sht31_drv.c create mode 100644 src/si7021_drv.c diff --git a/README.md b/README.md index 1d48b3b..e2f80b3 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,67 @@ temperature{sensor="0",type="BME280"} 18.4 humidity{sensor="0",type="BME280"} 77.4 pressure{sensor="0",type="BME280"} 96720.4 ``` + +### MCP9808 + +This is using `mcp9808-i2c` library; one sensor is allowed based on I2C +address on the bus, normally `0x18`, but configurable with `sensors.mcp9808_i2caddr` +in `mos.yml`. The chip is polled with a period of `sensors.mcp9808_period` +which defaults to 3 seconds, each sensor reading takes approximately 4ms. + +Please see the upstream [source](https://github.com/mongoose-os-libs/mcp9808-i2c) +for more information on the driver. + +Reported values (all types are gauges): +``` +temperature{sensor="0",type="MCP9808"} 18.6 +``` + +### Si7021 + +This is using `si7021-i2c` library; one sensor is allowed based on I2C +address on the bus, normally `0x40`, but configurable with `sensors.si7021_i2caddr` +in `mos.yml`. The chip is polled with a period of `sensors.si7021_period` +which defaults to 3 seconds, each sensor reading takes approximately 4ms. + +Please see the upstream [source](https://github.com/mongoose-os-libs/si7021-i2c) +for more information on the driver. + +Reported values (all types are gauges): +``` +temperature{sensor="0",type="SI7021"} 18.6 +humidity{sensor="0",type="SI7021"} 56.4 +``` + +### SHT31 + +This is using `sht31-i2c` library; one sensor is allowed based on I2C +address on the bus, normally `0x44`, but configurable with `sensors.sht31_i2caddr` +in `mos.yml`. The chip is polled with a period of `sensors.sht31_period` +which defaults to 3 seconds, each sensor reading takes approximately 4ms. + +Please see the upstream [source](https://github.com/mongoose-os-libs/si7021-i2c) +for more information on the driver. + +Reported values (all types are gauges): +``` +temperature{sensor="0",type="SHT31"} 18.6 +humidity{sensor="0",type="SHT31"} 56.4 +``` + +### HTU21D-F + +This is using `htu21df-i2c` library; one sensor is allowed based on I2C +address on the bus, normally `0x40`, but configurable with `sensors.htu21df_i2caddr` +in `mos.yml`. The chip is polled with a period of `sensors.htu21df_period` +which defaults to 3 seconds, each sensor reading takes approximately 4ms. + +Please see the upstream [source](https://github.com/mongoose-os-libs/si7021-i2c) +for more information on the driver. + +Reported values (all types are gauges): +``` +temperature{sensor="0",type="HTU21DF"} 18.5 +humidity{sensor="0",type="HTU21DF"} 55.8 +``` + diff --git a/mos.yml b/mos.yml index b15a10c..c37c58d 100644 --- a/mos.yml +++ b/mos.yml @@ -22,6 +22,14 @@ config_schema: - ["sensors.veml6075_period", "i", 3, {title: "Sample period in seconds for VEML6075 sensor"}] - ["sensors.bme280_i2caddr", "i", 0x77, {title: "I2C Address for BME280 sensor"}] - ["sensors.bme280_period", "i", 3, {title: "Sample period in seconds for BME280 sensor"}] + - ["sensors.htu21df_i2caddr", "i", 0x40, {title: "I2C Address for HTU21DF sensor"}] + - ["sensors.htu21df_period", "i", 3, {title: "Sample period in seconds for HTU21DF sensor"}] + - ["sensors.sht31_i2caddr", "i", 0x44, {title: "I2C Address for SHT31 sensor"}] + - ["sensors.sht31_period", "i", 3, {title: "Sample period in seconds for SHT31 sensor"}] + - ["sensors.si7021_i2caddr", "i", 0x40, {title: "I2C Address for SI7021 sensor"}] + - ["sensors.si7021_period", "i", 3, {title: "Sample period in seconds for SI7021 sensor"}] + - ["sensors.mcp9808_i2caddr", "i", 0x18, {title: "I2C Address for MCP9808 sensor"}] + - ["sensors.mcp9808_period", "i", 3, {title: "Sample period in seconds for MCP9808 sensor"}] - ["sensors.pushgateway_period", "i", 10, {title: "Period in seconds for Prometheus Pushgateway POSTs"}] - ["prometheus.pushgateway", "prometheus.example.net:9091"] @@ -33,6 +41,7 @@ config_schema: # - origin: https://github.com/mongoose-os-libs/bme280 libs: - origin: https://github.com/mongoose-os-libs/prometheus-metrics + version: latest # Used by the mos tool to catch mos binaries incompatible with this file format manifest_version: 2017-05-18 diff --git a/src/htu21df_drv.c b/src/htu21df_drv.c new file mode 100644 index 0000000..c59e143 --- /dev/null +++ b/src/htu21df_drv.c @@ -0,0 +1,48 @@ +#include "mgos.h" + +#ifdef MGOS_HAVE_HTU21DF_I2C +#include "mgos_htu21df.h" +#include "mgos_config.h" +#include "mgos_prometheus_metrics.h" +#include "mgos_prometheus_sensors.h" + +static struct mgos_htu21df *s_htu21df; + +static void htu21df_prometheus_metrics(struct mg_connection *nc, void *user_data) { + mgos_prometheus_metrics_printf(nc, GAUGE, + "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)); + + (void) user_data; +} + +static void htu21df_timer_cb(void *user_data) { + double start; + uint32_t usecs=0; + float temperature, humidity; + + start=mgos_uptime(); + temperature=mgos_htu21df_getTemperature(s_htu21df); + humidity=mgos_htu21df_getHumidity(s_htu21df); + usecs=1000000*(mgos_uptime()-start); + LOG(LL_INFO, ("HTU21DF sensor=0 temperature=%.2f humidity=%.1f usecs=%u", temperature, humidity, usecs)); + + (void) user_data; +} + +void htu21df_drv_init() { + 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_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 diff --git a/src/mcp9808_drv.c b/src/mcp9808_drv.c new file mode 100644 index 0000000..fb7207c --- /dev/null +++ b/src/mcp9808_drv.c @@ -0,0 +1,43 @@ +#include "mgos.h" + +#ifdef MGOS_HAVE_MCP9808_I2C +#include "mgos_mcp9808.h" +#include "mgos_config.h" +#include "mgos_prometheus_metrics.h" +#include "mgos_prometheus_sensors.h" + +static struct mgos_mcp9808 *s_mcp9808; + +static void mcp9808_prometheus_metrics(struct mg_connection *nc, void *user_data) { + mgos_prometheus_metrics_printf(nc, GAUGE, + "temperature", "Temperature in Celcius", + "{sensor=\"0\",type=\"MCP9808\"} %f", mgos_mcp9808_getTemperature(s_mcp9808)); + (void) user_data; +} + +static void mcp9808_timer_cb(void *user_data) { + double start; + uint32_t usecs=0; + float temperature; + + start=mgos_uptime(); + temperature=mgos_mcp9808_getTemperature(s_mcp9808); + usecs=1000000*(mgos_uptime()-start); + LOG(LL_INFO, ("MCP9808 sensor=0 temperature=%.2f usecs=%u", temperature, usecs)); + + (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_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 diff --git a/src/sht31_drv.c b/src/sht31_drv.c new file mode 100644 index 0000000..e681ee7 --- /dev/null +++ b/src/sht31_drv.c @@ -0,0 +1,48 @@ +#include "mgos.h" + +#ifdef MGOS_HAVE_SHT31_I2C +#include "mgos_sht31.h" +#include "mgos_config.h" +#include "mgos_prometheus_metrics.h" +#include "mgos_prometheus_sensors.h" + +static struct mgos_sht31 *s_sht31; + +static void sht31_prometheus_metrics(struct mg_connection *nc, void *user_data) { + mgos_prometheus_metrics_printf(nc, GAUGE, + "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)); + + (void) user_data; +} + +static void sht31_timer_cb(void *user_data) { + double start; + uint32_t usecs=0; + float temperature, humidity; + + start=mgos_uptime(); + temperature=mgos_sht31_getTemperature(s_sht31); + humidity=mgos_sht31_getHumidity(s_sht31); + usecs=1000000*(mgos_uptime()-start); + LOG(LL_INFO, ("SHT31 sensor=0 temperature=%.2f humidity=%.1f usecs=%u", temperature, humidity, usecs)); + + (void) user_data; +} + +void sht31_drv_init() { + 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_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 diff --git a/src/si7021_drv.c b/src/si7021_drv.c new file mode 100644 index 0000000..52e202a --- /dev/null +++ b/src/si7021_drv.c @@ -0,0 +1,48 @@ +#include "mgos.h" + +#ifdef MGOS_HAVE_SI7021_I2C +#include "mgos_si7021.h" +#include "mgos_config.h" +#include "mgos_prometheus_metrics.h" +#include "mgos_prometheus_sensors.h" + +static struct mgos_si7021 *s_si7021; + +static void si7021_prometheus_metrics(struct mg_connection *nc, void *user_data) { + mgos_prometheus_metrics_printf(nc, GAUGE, + "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)); + + (void) user_data; +} + +static void si7021_timer_cb(void *user_data) { + double start; + uint32_t usecs=0; + float temperature, humidity; + + start=mgos_uptime(); + temperature=mgos_si7021_getTemperature(s_si7021); + humidity=mgos_si7021_getHumidity(s_si7021); + usecs=1000000*(mgos_uptime()-start); + LOG(LL_INFO, ("SI7021 sensor=0 temperature=%.2f humidity=%.1f usecs=%u", temperature, humidity, usecs)); + + (void) user_data; +} + +void si7021_drv_init() { + 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_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