Factor out DHT
It's now its own library (in git@git.ipng.nl:pim/prometheus-sensors)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
build/
|
||||
deps/
|
||||
libs/prometheus-sensors
|
||||
|
@ -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
|
||||
|
5
mos.yml
5
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
|
||||
|
89
src/dht.c
89
src/dht.c
@ -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; i<s_num_dht; i++) {
|
||||
print_chunk(nc, "temperature", "{sensor=\"%d\",type=\"DHT\"} %f", i, mgos_dht_get_temp(s_dht[i]));
|
||||
print_chunk(nc, "humidity", "{sensor=\"%d\",type=\"DHT\"} %f", i, mgos_dht_get_humidity(s_dht[i]));
|
||||
}
|
||||
|
||||
(void) user_data;
|
||||
}
|
||||
|
||||
static bool dht_create(int pin, enum dht_type type) {
|
||||
struct mgos_dht *dht;
|
||||
if (s_num_dht == MAX_DHT) {
|
||||
LOG(LL_ERROR, ("No more sensor slots available (%d added)", MAX_DHT));
|
||||
return false;
|
||||
}
|
||||
|
||||
dht=mgos_dht_create(pin, type);
|
||||
if (!dht) {
|
||||
LOG(LL_ERROR, ("Could not create DHT sensor on pin %d", pin));
|
||||
return false;
|
||||
}
|
||||
s_dht[s_num_dht] = dht;
|
||||
s_num_dht++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void dht_init() {
|
||||
char *tok;
|
||||
|
||||
memset(s_dht, 0, sizeof (struct mgos_dht *) * MAX_DHT);
|
||||
tok = strtok((char *)mgos_sys_config_get_app_dht_gpio(), ", ");
|
||||
while (tok) {
|
||||
int gpio;
|
||||
gpio = atoi(tok);
|
||||
tok=strtok(NULL, ", ");
|
||||
dht_create(gpio, AM2302);
|
||||
mgos_msleep(250);
|
||||
}
|
||||
|
||||
if (s_num_dht>0)
|
||||
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
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user