diff --git a/include/main.h b/include/main.h index f31fe63..c92b520 100644 --- a/include/main.h +++ b/include/main.h @@ -29,4 +29,6 @@ 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 049441b..f714584 100644 --- a/mos.yml +++ b/mos.yml @@ -44,8 +44,8 @@ config_schema: - ["app.config", "sonoff-basic.json"] -build_vars: - FLASH_SIZE: 1048576 +#build_vars: +# FLASH_SIZE: 1048576 # List of libraries used by this app, in order of initialisation libs: @@ -56,6 +56,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: 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 new file mode 100644 index 0000000..945d94e --- /dev/null +++ b/src/dht.c @@ -0,0 +1,43 @@ +#include "mgos.h" + +#ifdef MGOS_HAVE_DHT +#include "main.h" +#include "mgos_dht.h" +#include "mqtt.h" +static void dht_cb(void *ud) { + struct mgos_dht *dht = (struct mgos_dht *)ud; + float t, h; + + if (!dht) { + LOG(LL_ERROR, ("No DHT handle to work with!")); + return; + } + t = mgos_dht_get_temp(dht); + h = mgos_dht_get_humidity(dht); + + mqtt_publish_stat("dht", "{temp=%.2f, humidity=%.1f}", t, h); +} + +static void dht_create(int pin, enum dht_type type) { + struct mgos_dht *dht; + dht=mgos_dht_create(pin, type); + if (!dht) { + LOG(LL_ERROR, ("Could not create DHT sensor on pin %d", pin)); + return; + } + mgos_set_timer(5000, true, dht_cb, (void*)dht); +} + +void dht_init() { + dht_create(5, AM2302); + mgos_msleep(250); + dht_create(4, AM2302); + mgos_msleep(250); + dht_create(14, AM2302); +} + +#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 f8754e1..a1caa0f 100644 --- a/src/main.c +++ b/src/main.c @@ -7,5 +7,6 @@ enum mgos_app_init_result mgos_app_init(void) { channel_init(mgos_sys_config_get_app_config()); mqtt_init(); rpc_init(); + dht_init(); return MGOS_APP_INIT_SUCCESS; }