diff --git a/src/driver/drv_dht.c b/src/driver/drv_dht.c index 3e7365461..367ed8e7c 100644 --- a/src/driver/drv_dht.c +++ b/src/driver/drv_dht.c @@ -10,7 +10,10 @@ #include "drv_local.h" #include "drv_dht_internal.h" -dht_t *test = 0; +// test device +static dht_t *test = 0; +// per-pin devices +static dht_t **g_dhts = 0; // simplest demo void DHT_DoMyDHTTest() { @@ -26,7 +29,64 @@ void DHT_DoMyDHTTest() { addLogAdv(LOG_INFO, LOG_FEATURE_ENERGYMETER, "DHT says %f %f", humid, temp); } } - +int translateDHTType(int role) { + if (role == IOR_DHT11) + return DHT11; + if (role == IOR_DHT12) + return DHT12; + if (role == IOR_DHT21) + return DHT21; + if (role == IOR_DHT22) + return DHT22; + return 0; +} void DHT_OnEverySecond() { + int i; + int bHasDHT; + + bHasDHT = false; + + for (i = 0; i < PLATFORM_GPIO_MAX; i++) { + if (IS_PIN_DHT_ROLE(g_cfg.pins.roles[i])) { + bHasDHT = true; + } + } + if (bHasDHT == false) { + if (g_dhts) { + free(g_dhts); + g_dhts = 0; + } + // nothing to do here + return; + } + if (g_dhts==0) { + g_dhts = (dht_t**)malloc(sizeof(dht_t*)*PLATFORM_GPIO_MAX); + memset(g_dhts, 0, sizeof(dht_t*)*PLATFORM_GPIO_MAX); + } + for (i = 0; i < PLATFORM_GPIO_MAX; i++) { + if (IS_PIN_DHT_ROLE(g_cfg.pins.roles[i])) { + if (g_dhts[i] == 0) { + g_dhts[i] = DHT_Create(i, translateDHTType(g_cfg.pins.roles[i])); + } + if (g_dhts[i]) { + float temp, humid; + + humid = DHT_readHumidity(g_dhts[i], false); + temp = DHT_readTemperature(g_dhts[i], false, false); + + // don't want to loose accuracy, so multiply by 10 + // We have a channel types to handle that + CHANNEL_Set(g_cfg.pins.channels[i], (int)(temp * 10), 0); + CHANNEL_Set(g_cfg.pins.channels2[i], (int)(humid), 0); + } + } + else { + if (g_dhts[i] != 0) { + free(g_dhts[i]); + g_dhts[i] = 0; + } + } + } + } diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index b58e3c90b..635d03287 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -349,6 +349,21 @@ int http_fn_index(http_request_t* request) { poststr(request, ""); poststr(request, ""); + for (i = 0; i < PLATFORM_GPIO_MAX; i++) { + int role; + + role = PIN_GetPinRoleForPinIndex(i); + + if (IS_PIN_DHT_ROLE(role)) { + // DHT pin has two channels - temperature and humidity + poststr(request, ""); + } + } for (i = 0; i < CHANNEL_MAX; i++) { int channelType; @@ -2489,7 +2504,10 @@ int http_fn_cfg_pins(http_request_t* request) { poststr(request, ""); hprintf255(request, "", i, ch); - if (si == IOR_Button || si == IOR_Button_n) + if (si == IOR_Button || si == IOR_Button_n + || si == IOR_DHT11 || si == IOR_DHT22 + || si == IOR_DHT12 || si == IOR_DHT21 + ) { // extra param. For button, is relay index to toggle on double click hprintf255(request, "", i, ch2); diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index f3b462b22..7a0308c8b 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -412,6 +412,12 @@ const char* htmlPinRoleNames[] = { "Btn_NextTemperature_n", "Btn_ScriptOnly", "Btn_ScriptOnly_n", + "DHT11", + "DHT12", + "DHT21", + "DHT22", + "error", + "error", "error", "error", "error", diff --git a/src/new_pins.h b/src/new_pins.h index 9c981b5d6..404256b16 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -64,9 +64,17 @@ enum IORole { IOR_Button_ScriptOnly, IOR_Button_ScriptOnly_n, + IOR_DHT11, + IOR_DHT12, + IOR_DHT21, + IOR_DHT22, + + IOR_Total_Options, }; +#define IS_PIN_DHT_ROLE(role) (((role)>=IOR_DHT11) &&((role)<=IOR_DHT22)) + enum ChannelType { ChType_Default, ChType_Error,
"); + iValue = CHANNEL_Get(PIN_GetPinChannelForPinIndex(i)); + hprintf255(request, "Sensor %s on pin %i temperature %.2fC", PIN_RoleToString(role), i,(float) (iValue*0.1f)); + iValue = CHANNEL_Get(PIN_GetPinChannel2ForPinIndex(i)); + hprintf255(request, ", humidity %.1f%%
", (float)iValue); + poststr(request, "