Add DHT Lua Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44).
This commit is contained in:
parent
515a7bf1ab
commit
899935e60b
|
@ -27,6 +27,7 @@
|
||||||
// Released to the public domain
|
// Released to the public domain
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "user_interface.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "dht.h"
|
#include "dht.h"
|
||||||
|
|
||||||
|
@ -51,6 +52,20 @@ static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay);
|
||||||
// PUBLIC
|
// PUBLIC
|
||||||
//
|
//
|
||||||
|
|
||||||
|
// return values:
|
||||||
|
// Humidity
|
||||||
|
double dht_getHumidity(void)
|
||||||
|
{
|
||||||
|
return dht_humidity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return values:
|
||||||
|
// Temperature
|
||||||
|
double dht_getTemperature(void)
|
||||||
|
{
|
||||||
|
return dht_temperature;
|
||||||
|
}
|
||||||
|
|
||||||
// return values:
|
// return values:
|
||||||
// DHTLIB_OK
|
// DHTLIB_OK
|
||||||
// DHTLIB_ERROR_CHECKSUM
|
// DHTLIB_ERROR_CHECKSUM
|
||||||
|
@ -111,6 +126,42 @@ int dht_read(uint8_t pin)
|
||||||
return DHTLIB_OK;
|
return DHTLIB_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return values:
|
||||||
|
// DHTLIB_OK
|
||||||
|
// DHTLIB_ERROR_CHECKSUM
|
||||||
|
// DHTLIB_ERROR_TIMEOUT
|
||||||
|
inline int dht_read21(uint8_t pin)
|
||||||
|
{
|
||||||
|
return dht_read(pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
// return values:
|
||||||
|
// DHTLIB_OK
|
||||||
|
// DHTLIB_ERROR_CHECKSUM
|
||||||
|
// DHTLIB_ERROR_TIMEOUT
|
||||||
|
inline int dht_read22(uint8_t pin)
|
||||||
|
{
|
||||||
|
return dht_read(pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
// return values:
|
||||||
|
// DHTLIB_OK
|
||||||
|
// DHTLIB_ERROR_CHECKSUM
|
||||||
|
// DHTLIB_ERROR_TIMEOUT
|
||||||
|
inline int dht_read33(uint8_t pin)
|
||||||
|
{
|
||||||
|
return dht_read(pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
// return values:
|
||||||
|
// DHTLIB_OK
|
||||||
|
// DHTLIB_ERROR_CHECKSUM
|
||||||
|
// DHTLIB_ERROR_TIMEOUT
|
||||||
|
inline int dht_read44(uint8_t pin)
|
||||||
|
{
|
||||||
|
return dht_read(pin);
|
||||||
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// PRIVATE
|
// PRIVATE
|
||||||
|
@ -138,6 +189,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
||||||
|
|
||||||
// REQUEST SAMPLE
|
// REQUEST SAMPLE
|
||||||
// pinMode(pin, OUTPUT);
|
// pinMode(pin, OUTPUT);
|
||||||
|
platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP);
|
||||||
DIRECT_MODE_OUTPUT(pin);
|
DIRECT_MODE_OUTPUT(pin);
|
||||||
// digitalWrite(pin, LOW); // T-be
|
// digitalWrite(pin, LOW); // T-be
|
||||||
DIRECT_WRITE_LOW(pin);
|
DIRECT_WRITE_LOW(pin);
|
||||||
|
|
|
@ -52,10 +52,13 @@
|
||||||
int dht_read11(uint8_t pin);
|
int dht_read11(uint8_t pin);
|
||||||
int dht_read(uint8_t pin);
|
int dht_read(uint8_t pin);
|
||||||
|
|
||||||
inline int dht_read21(uint8_t pin) { return dht_read(pin); };
|
inline int dht_read21(uint8_t pin);
|
||||||
inline int dht_read22(uint8_t pin) { return dht_read(pin); };
|
inline int dht_read22(uint8_t pin);
|
||||||
inline int dht_read33(uint8_t pin) { return dht_read(pin); };
|
inline int dht_read33(uint8_t pin);
|
||||||
inline int dht_read44(uint8_t pin) { return dht_read(pin); };
|
inline int dht_read44(uint8_t pin);
|
||||||
|
|
||||||
|
double dht_getHumidity(void);
|
||||||
|
double dht_getTemperature(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
//
|
//
|
||||||
|
|
|
@ -33,6 +33,7 @@
|
||||||
#define LUA_USE_MODULES_CJSON
|
#define LUA_USE_MODULES_CJSON
|
||||||
#define LUA_USE_MODULES_CRYPTO
|
#define LUA_USE_MODULES_CRYPTO
|
||||||
#define LUA_USE_MODULES_RC
|
#define LUA_USE_MODULES_RC
|
||||||
|
#define LUA_USE_MODULES_DHT
|
||||||
|
|
||||||
#endif /* LUA_USE_MODULES */
|
#endif /* LUA_USE_MODULES */
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,7 @@ INCLUDES += -I ../wofs
|
||||||
INCLUDES += -I ../spiffs
|
INCLUDES += -I ../spiffs
|
||||||
INCLUDES += -I ../smart
|
INCLUDES += -I ../smart
|
||||||
INCLUDES += -I ../cjson
|
INCLUDES += -I ../cjson
|
||||||
|
INCLUDES += -I ../dhtlib
|
||||||
PDIR := ../$(PDIR)
|
PDIR := ../$(PDIR)
|
||||||
sinclude $(PDIR)Makefile
|
sinclude $(PDIR)Makefile
|
||||||
|
|
||||||
|
|
|
@ -82,6 +82,15 @@ LUALIB_API int ( luaopen_ow )( lua_State *L );
|
||||||
#define AUXLIB_CJSON "cjson"
|
#define AUXLIB_CJSON "cjson"
|
||||||
LUALIB_API int ( luaopen_cjson )( lua_State *L );
|
LUALIB_API int ( luaopen_cjson )( lua_State *L );
|
||||||
|
|
||||||
|
#define AUXLIB_CRYPTO "crypto"
|
||||||
|
LUALIB_API int ( luaopen_crypto )( lua_State *L );
|
||||||
|
|
||||||
|
#define AUXLIB_RC "rc"
|
||||||
|
LUALIB_API int ( luaopen_rc )( lua_State *L );
|
||||||
|
|
||||||
|
#define AUXLIB_DHT "dht"
|
||||||
|
LUALIB_API int ( luaopen_dht )( lua_State *L );
|
||||||
|
|
||||||
// Helper macros
|
// Helper macros
|
||||||
#define MOD_CHECK_ID( mod, id )\
|
#define MOD_CHECK_ID( mod, id )\
|
||||||
if( !platform_ ## mod ## _exists( id ) )\
|
if( !platform_ ## mod ## _exists( id ) )\
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
// Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44).
|
||||||
|
|
||||||
|
#include "lualib.h"
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "auxmods.h"
|
||||||
|
#include "lrotable.h"
|
||||||
|
#include "cpu_esp8266.h"
|
||||||
|
#include "dht.h"
|
||||||
|
|
||||||
|
#define NUM_DHT GPIO_PIN_NUM
|
||||||
|
|
||||||
|
// ****************************************************************************
|
||||||
|
// DHT functions
|
||||||
|
int platform_dht_exists( unsigned id )
|
||||||
|
{
|
||||||
|
return ((id < NUM_DHT) && (id > 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lua: result = dht.read( id )
|
||||||
|
static int dht_lapi_read( lua_State *L )
|
||||||
|
{
|
||||||
|
unsigned id = luaL_checkinteger( L, 1 );
|
||||||
|
MOD_CHECK_ID( dht, id );
|
||||||
|
lua_pushinteger( L, dht_read(id) );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lua: result = dht.read11( id )
|
||||||
|
static int dht_lapi_read11( lua_State *L )
|
||||||
|
{
|
||||||
|
unsigned id = luaL_checkinteger( L, 1 );
|
||||||
|
MOD_CHECK_ID( dht, id );
|
||||||
|
lua_pushinteger( L, dht_read11(id) );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lua: result = dht.humidity()
|
||||||
|
static int dht_lapi_humidity( lua_State *L )
|
||||||
|
{
|
||||||
|
lua_pushinteger( L, dht_getHumidity() );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lua: result = dht.temperature()
|
||||||
|
static int dht_lapi_temperature( lua_State *L )
|
||||||
|
{
|
||||||
|
lua_pushinteger( L, dht_getTemperature() );
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Module function map
|
||||||
|
#define MIN_OPT_LEVEL 2
|
||||||
|
#include "lrodefs.h"
|
||||||
|
const LUA_REG_TYPE dht_map[] =
|
||||||
|
{
|
||||||
|
{ LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) },
|
||||||
|
{ LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) },
|
||||||
|
{ LSTRKEY( "humidity" ), LFUNCVAL( dht_lapi_humidity ) },
|
||||||
|
{ LSTRKEY( "temperature" ), LFUNCVAL( dht_lapi_temperature ) },
|
||||||
|
#if LUA_OPTIMIZE_MEMORY > 0
|
||||||
|
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
|
||||||
|
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },
|
||||||
|
{ LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) },
|
||||||
|
#endif
|
||||||
|
{ LNILKEY, LNILVAL }
|
||||||
|
};
|
||||||
|
|
||||||
|
LUALIB_API int luaopen_dht( lua_State *L )
|
||||||
|
{
|
||||||
|
#if LUA_OPTIMIZE_MEMORY > 0
|
||||||
|
return 0;
|
||||||
|
#else // #if LUA_OPTIMIZE_MEMORY > 0
|
||||||
|
luaL_register( L, AUXLIB_DHT, dht_map );
|
||||||
|
|
||||||
|
// Add the constants
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
#endif // #if LUA_OPTIMIZE_MEMORY > 0
|
||||||
|
}
|
|
@ -165,6 +165,14 @@ _ROM(MODULES_RC, luaopen_rc, rc_map)
|
||||||
#define ROM_MODULES_RC
|
#define ROM_MODULES_RC
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(LUA_USE_MODULES_DHT)
|
||||||
|
#define MODULES_DHT "dht"
|
||||||
|
#define ROM_MODULES_DHT \
|
||||||
|
_ROM(MODULES_DHT, luaopen_dht, dht_map)
|
||||||
|
#else
|
||||||
|
#define ROM_MODULES_DHT
|
||||||
|
#endif
|
||||||
|
|
||||||
#define LUA_MODULES_ROM \
|
#define LUA_MODULES_ROM \
|
||||||
ROM_MODULES_GPIO \
|
ROM_MODULES_GPIO \
|
||||||
ROM_MODULES_PWM \
|
ROM_MODULES_PWM \
|
||||||
|
@ -185,6 +193,7 @@ _ROM(MODULES_RC, luaopen_rc, rc_map)
|
||||||
ROM_MODULES_WS2812 \
|
ROM_MODULES_WS2812 \
|
||||||
ROM_MODULES_CJSON \
|
ROM_MODULES_CJSON \
|
||||||
ROM_MODULES_CRYPTO \
|
ROM_MODULES_CRYPTO \
|
||||||
ROM_MODULES_RC
|
ROM_MODULES_RC \
|
||||||
|
ROM_MODULES_DHT
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -75,6 +75,7 @@ int platform_tmr_exists( unsigned id )
|
||||||
return id < NUM_TMR;
|
return id < NUM_TMR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ****************************************************************************
|
||||||
// I2C support
|
// I2C support
|
||||||
int platform_i2c_exists( unsigned id )
|
int platform_i2c_exists( unsigned id )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue