Add DHT support.

Very rough stab just to test the waters -- this will need some
more work.

For now, hard code 4,5,15 pins (as per Sonoff SV), but before
rolling this out, add a config flag for this behavior.

Neat trick -- the presence of dht library in mos.yml triggers
inclusion of the code via define -DMGOS_HAVE_DHT=1
This commit is contained in:
Pim van Pelt
2018-02-11 13:20:36 +01:00
parent b1472d059d
commit bb62e9b6ae
4 changed files with 49 additions and 2 deletions

43
src/dht.c Normal file
View File

@ -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

View File

@ -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;
}