2015-06-18 16:31:40 +08:00
|
|
|
// Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44).
|
|
|
|
|
2015-12-16 16:04:58 +11:00
|
|
|
#include "module.h"
|
2015-06-18 16:31:40 +08:00
|
|
|
#include "lauxlib.h"
|
2015-12-13 02:29:37 +00:00
|
|
|
#include "platform.h"
|
2015-06-18 16:31:40 +08:00
|
|
|
#include "cpu_esp8266.h"
|
2019-07-23 18:47:18 +03:00
|
|
|
#include "dht/dht.h"
|
2015-06-18 16:31:40 +08:00
|
|
|
|
|
|
|
#define NUM_DHT GPIO_PIN_NUM
|
|
|
|
|
|
|
|
// ****************************************************************************
|
|
|
|
// DHT functions
|
|
|
|
int platform_dht_exists( unsigned id )
|
|
|
|
{
|
|
|
|
return ((id < NUM_DHT) && (id > 0));
|
|
|
|
}
|
|
|
|
|
2020-08-29 12:48:24 -04:00
|
|
|
static void aux_read( lua_State *L )
|
|
|
|
{
|
|
|
|
double temp = dht_getTemperature();
|
|
|
|
double humi = dht_getHumidity();
|
|
|
|
int tempdec = (int)((temp - (int)temp) * 1000);
|
|
|
|
int humidec = (int)((humi - (int)humi) * 1000);
|
|
|
|
lua_pushnumber( L, (lua_Float) temp );
|
|
|
|
lua_pushnumber( L, (lua_Float) humi );
|
|
|
|
lua_pushinteger( L, tempdec );
|
|
|
|
lua_pushinteger( L, humidec );
|
|
|
|
}
|
|
|
|
|
2015-06-18 20:53:52 +08:00
|
|
|
// Lua: status, temp, humi, tempdec, humidec = dht.read( id )
|
2015-06-18 16:31:40 +08:00
|
|
|
static int dht_lapi_read( lua_State *L )
|
|
|
|
{
|
|
|
|
unsigned id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( dht, id );
|
2020-12-16 23:07:03 +01:00
|
|
|
lua_pushinteger( L, dht_read(id, DHT_NON11) );
|
2020-08-29 12:48:24 -04:00
|
|
|
aux_read( L );
|
2015-06-18 20:53:52 +08:00
|
|
|
return 5;
|
2015-06-18 16:31:40 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 20:53:52 +08:00
|
|
|
// Lua: status, temp, humi, tempdec, humidec = dht.read11( id ))
|
2015-06-18 16:31:40 +08:00
|
|
|
static int dht_lapi_read11( lua_State *L )
|
|
|
|
{
|
|
|
|
unsigned id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( dht, id );
|
2020-12-16 23:07:03 +01:00
|
|
|
lua_pushinteger( L, dht_read(id, DHT11) );
|
|
|
|
aux_read( L );
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: status, temp, humi, tempdec, humidec = dht.read12( id ))
|
|
|
|
static int dht_lapi_read12( lua_State *L )
|
|
|
|
{
|
|
|
|
unsigned id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( dht, id );
|
|
|
|
lua_pushinteger( L, dht_read(id, DHT12) );
|
2020-08-29 12:48:24 -04:00
|
|
|
aux_read( L );
|
2015-06-18 20:53:52 +08:00
|
|
|
return 5;
|
2015-06-18 16:31:40 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 20:53:52 +08:00
|
|
|
// Lua: status, temp, humi, tempdec, humidec = dht.readxx( id ))
|
|
|
|
static int dht_lapi_readxx( lua_State *L )
|
2015-06-18 16:31:40 +08:00
|
|
|
{
|
2015-06-18 20:53:52 +08:00
|
|
|
unsigned id = luaL_checkinteger( L, 1 );
|
|
|
|
MOD_CHECK_ID( dht, id );
|
2020-12-16 23:07:03 +01:00
|
|
|
lua_pushinteger( L, dht_read(id, DHT22) );
|
2020-08-29 12:48:24 -04:00
|
|
|
aux_read( L );
|
2015-06-18 20:53:52 +08:00
|
|
|
return 5;
|
2015-06-18 17:19:55 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 16:31:40 +08:00
|
|
|
// Module function map
|
2020-04-27 01:13:38 +01:00
|
|
|
LROT_BEGIN(dht, NULL, 0)
|
2019-05-08 14:08:20 +03:00
|
|
|
LROT_FUNCENTRY( read, dht_lapi_read )
|
|
|
|
LROT_FUNCENTRY( read11, dht_lapi_read11 )
|
2020-12-16 23:07:03 +01:00
|
|
|
LROT_FUNCENTRY( read12, dht_lapi_read12 )
|
|
|
|
LROT_FUNCENTRY( readxx, dht_lapi_read )
|
2019-05-08 14:08:20 +03:00
|
|
|
LROT_NUMENTRY( OK, DHTLIB_OK )
|
|
|
|
LROT_NUMENTRY( ERROR_CHECKSUM, DHTLIB_ERROR_CHECKSUM )
|
|
|
|
LROT_NUMENTRY( ERROR_TIMEOUT, DHTLIB_ERROR_TIMEOUT )
|
2020-04-27 01:13:38 +01:00
|
|
|
LROT_END(dht, NULL, 0)
|
2015-06-18 16:31:40 +08:00
|
|
|
|
2019-05-08 14:08:20 +03:00
|
|
|
|
|
|
|
NODEMCU_MODULE(DHT, "dht", dht, NULL);
|