From 8bf7b919ccd82137cda8c8694f7ed93a018d654d Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 4 Mar 2018 16:38:54 +0100 Subject: [PATCH] Factor out DHT It's now its own library (in git@git.ipng.nl:pim/prometheus-sensors) --- .gitignore | 1 + include/main.h | 2 -- mos.yml | 5 ++- src/dht.c | 89 -------------------------------------------------- src/main.c | 7 ---- 5 files changed, 3 insertions(+), 101 deletions(-) delete mode 100644 src/dht.c diff --git a/.gitignore b/.gitignore index 350abb0..b301f9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ deps/ +libs/prometheus-sensors diff --git a/include/main.h b/include/main.h index c92b520..f31fe63 100644 --- a/include/main.h +++ b/include/main.h @@ -29,6 +29,4 @@ bool channel_get(int idx); int channel_get_total(); void channel_handler(int gpio, void *arg); -void dht_init(); - #endif // __MAIN_H diff --git a/mos.yml b/mos.yml index c658e12..e2aaa25 100644 --- a/mos.yml +++ b/mos.yml @@ -42,7 +42,7 @@ config_schema: - ["app.hostname", "sonoff-sv"] - ["app.config", "s", {title: "Application specific config file"}] - ["app.config", "sonoff-basic.json"] - - ["app.dht_gpio", "s", "5,4,14", {title: "Comma Separated list of GPIO pins to enable DHT22/AM2302 sensors on"}] + - ["sensors.dht_gpio", "s", "5,4,14", {title: "Comma Separated list of GPIO pins to enable DHT22/AM2302 sensors on"}] - ["prometheus.pushgateway", "chbtl01.paphosting.net:9091"] @@ -58,8 +58,7 @@ libs: - origin: https://github.com/mongoose-os-libs/rpc-service-fs - origin: https://github.com/mongoose-os-libs/rpc-mqtt - origin: https://github.com/mongoose-os-libs/mqtt - - origin: https://github.com/mongoose-os-libs/dht - - origin: https://github.com/mongoose-os-libs/prometheus-metrics + - origin: libs/prometheus-sensors - origin: libs/rpc-service-ota # Used by the mos tool to catch mos binaries incompatible with this file format diff --git a/src/dht.c b/src/dht.c deleted file mode 100644 index d704928..0000000 --- a/src/dht.c +++ /dev/null @@ -1,89 +0,0 @@ -#include "mgos.h" - -#ifdef MGOS_HAVE_DHT -#include "main.h" -#include "mgos_dht.h" -#include "mgos_config.h" -#include "mgos_prometheus_metrics.h" -#include "mqtt.h" - -#define MAX_DHT 8 - -static struct mgos_dht *s_dht[MAX_DHT]; -static int s_num_dht = 0; - -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 dht_prometheus_metrics(struct mg_connection *nc, void *user_data) { - int i; - - // BUG -- repeated HELP and TYPE makes Prometheus parser bork :( - mgos_prometheus_metrics_printf(nc, GAUGE, - "temperature", "Temperature in celcius", - "{sensor=\"0\",type=\"DHT\"} %f", mgos_dht_get_temp(s_dht[0])); - mgos_prometheus_metrics_printf(nc, GAUGE, - "humidity", "Relative humidity percentage", - "{sensor=\"0\",type=\"DHT\"} %f", mgos_dht_get_humidity(s_dht[0])); - - for (i=1; i0) - mgos_prometheus_metrics_add_handler(dht_prometheus_metrics, NULL); -} - -#else -void dht_init() { - LOG(LL_INFO, ("DHT disabled, include library in mos.yml to enable")); -} -#endif diff --git a/src/main.c b/src/main.c index 602f294..2a330db 100644 --- a/src/main.c +++ b/src/main.c @@ -4,16 +4,9 @@ #include "mqtt.h" #include "rpc.h" -static void timer_cb(void *user_data) { - mgos_prometheus_metrics_push("lightswitch", mgos_sys_config_get_device_id()); - (void) user_data; -} - enum mgos_app_init_result mgos_app_init(void) { channel_init(mgos_sys_config_get_app_config()); mqtt_init(); rpc_init(); - dht_init(); - mgos_set_timer(5000, true, timer_cb, NULL); return MGOS_APP_INIT_SUCCESS; }