Update DHTlib, supported DHT11 or DHTxx automatic detection.
Using @MarsTechHAN 's method. New usage: status, temp, humi, tempdec, humidec = dht.read( id ) print( dht.read( id ) ) = dht.read( id )
This commit is contained in:
parent
98a2c9fdb6
commit
87341547d7
|
@ -66,6 +66,64 @@ double dht_getTemperature(void)
|
|||
return dht_temperature;
|
||||
}
|
||||
|
||||
// return values:
|
||||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht_read_universal(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int rv = dht_readSensor(pin, DHTLIB_DHT_UNI_WAKEUP);
|
||||
if (rv != DHTLIB_OK)
|
||||
{
|
||||
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
return rv; // propagate error value
|
||||
}
|
||||
|
||||
// Assume it is DHT11
|
||||
// If it is DHT11, both bit[1] and bit[3] is 0
|
||||
if ((dht_bits[1] == 0) && (dht_bits[3] == 0))
|
||||
{
|
||||
// It may DHT11
|
||||
// CONVERT AND STORE
|
||||
dht_humidity = dht_bits[0]; // dht_bits[1] == 0;
|
||||
dht_temperature = dht_bits[2]; // dht_bits[3] == 0;
|
||||
|
||||
// TEST CHECKSUM
|
||||
// dht_bits[1] && dht_bits[3] both 0
|
||||
uint8_t sum = dht_bits[0] + dht_bits[2];
|
||||
if (dht_bits[4] != sum)
|
||||
{
|
||||
// It may not DHT11
|
||||
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
// Do nothing
|
||||
}
|
||||
else
|
||||
{
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
}
|
||||
|
||||
// Assume it is not DHT11
|
||||
// CONVERT AND STORE
|
||||
dht_humidity = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[0], dht_bits[1]) * 0.1;
|
||||
dht_temperature = (double)COMBINE_HIGH_AND_LOW_BYTE(dht_bits[2] & 0x7F, dht_bits[3]) * 0.1;
|
||||
if (dht_bits[2] & 0x80) // negative dht_temperature
|
||||
{
|
||||
dht_temperature = -dht_temperature;
|
||||
}
|
||||
|
||||
// TEST CHECKSUM
|
||||
uint8_t sum = dht_bits[0] + dht_bits[1] + dht_bits[2] + dht_bits[3];
|
||||
if (dht_bits[4] != sum)
|
||||
{
|
||||
return DHTLIB_ERROR_CHECKSUM;
|
||||
}
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
|
||||
// return values:
|
||||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
#define DHTLIB_DHT11_WAKEUP 18
|
||||
#define DHTLIB_DHT_WAKEUP 1
|
||||
#define DHTLIB_DHT_UNI_WAKEUP 18
|
||||
|
||||
// max timeout is 100 usec.
|
||||
// For a 16 Mhz proc 100 usec is 1600 clock cycles
|
||||
|
@ -49,6 +50,7 @@
|
|||
// DHTLIB_OK
|
||||
// DHTLIB_ERROR_CHECKSUM
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht_read_universal(uint8_t pin);
|
||||
int dht_read11(uint8_t pin);
|
||||
int dht_read(uint8_t pin);
|
||||
|
||||
|
|
|
@ -16,59 +16,88 @@ int platform_dht_exists( unsigned id )
|
|||
return ((id < NUM_DHT) && (id > 0));
|
||||
}
|
||||
|
||||
// Lua: result = dht.read( id )
|
||||
// Lua: status, temp, humi, tempdec, humidec = 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) );
|
||||
lua_pushnumber( L, dht_getTemperature() );
|
||||
lua_pushnumber( L, dht_getHumidity() );
|
||||
return 3;
|
||||
lua_pushinteger( L, dht_read_universal(id) );
|
||||
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, temp );
|
||||
lua_pushnumber( L, humi );
|
||||
lua_pushnumber( L, tempdec );
|
||||
lua_pushnumber( L, humidec );
|
||||
return 5;
|
||||
}
|
||||
|
||||
// Lua: result = dht.read11( id )
|
||||
// Lua: status, temp, humi, tempdec, humidec = 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) );
|
||||
lua_pushnumber( L, dht_getTemperature() );
|
||||
lua_pushnumber( L, dht_getHumidity() );
|
||||
return 3;
|
||||
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, temp );
|
||||
lua_pushnumber( L, humi );
|
||||
lua_pushnumber( L, tempdec );
|
||||
lua_pushnumber( L, humidec );
|
||||
return 5;
|
||||
}
|
||||
|
||||
// Lua: result = dht.humidity()
|
||||
static int dht_lapi_humidity( lua_State *L )
|
||||
// Lua: status, temp, humi, tempdec, humidec = dht.readxx( id ))
|
||||
static int dht_lapi_readxx( lua_State *L )
|
||||
{
|
||||
lua_pushnumber( L, dht_getHumidity() );
|
||||
return 1;
|
||||
unsigned id = luaL_checkinteger( L, 1 );
|
||||
MOD_CHECK_ID( dht, id );
|
||||
lua_pushinteger( L, dht_read(id) );
|
||||
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, temp );
|
||||
lua_pushnumber( L, humi );
|
||||
lua_pushnumber( L, tempdec );
|
||||
lua_pushnumber( L, humidec );
|
||||
return 5;
|
||||
}
|
||||
|
||||
// Lua: result = dht.humiditydecimal()
|
||||
static int dht_lapi_humiditydecimal( lua_State *L )
|
||||
{
|
||||
double value = dht_getHumidity();
|
||||
int result = (int)((value - (int)value) * 1000);
|
||||
lua_pushnumber( L, result );
|
||||
return 1;
|
||||
}
|
||||
// // Lua: result = dht.humidity()
|
||||
// static int dht_lapi_humidity( lua_State *L )
|
||||
// {
|
||||
// lua_pushnumber( L, dht_getHumidity() );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// Lua: result = dht.temperature()
|
||||
static int dht_lapi_temperature( lua_State *L )
|
||||
{
|
||||
lua_pushnumber( L, dht_getTemperature() );
|
||||
return 1;
|
||||
}
|
||||
// // Lua: result = dht.humiditydecimal()
|
||||
// static int dht_lapi_humiditydecimal( lua_State *L )
|
||||
// {
|
||||
// double value = dht_getHumidity();
|
||||
// int result = (int)((value - (int)value) * 1000);
|
||||
// lua_pushnumber( L, result );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// Lua: result = dht.temperaturedecimal()
|
||||
static int dht_lapi_temperaturedecimal( lua_State *L )
|
||||
{
|
||||
double value = dht_getTemperature();
|
||||
int result = (int)((value - (int)value) * 1000);
|
||||
lua_pushnumber( L, result );
|
||||
return 1;
|
||||
}
|
||||
// // Lua: result = dht.temperature()
|
||||
// static int dht_lapi_temperature( lua_State *L )
|
||||
// {
|
||||
// lua_pushnumber( L, dht_getTemperature() );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// // Lua: result = dht.temperaturedecimal()
|
||||
// static int dht_lapi_temperaturedecimal( lua_State *L )
|
||||
// {
|
||||
// double value = dht_getTemperature();
|
||||
// int result = (int)((value - (int)value) * 1000);
|
||||
// lua_pushnumber( L, result );
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// Module function map
|
||||
#define MIN_OPT_LEVEL 2
|
||||
|
@ -77,10 +106,7 @@ 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 ) },
|
||||
{ LSTRKEY( "humiditydecimal" ), LFUNCVAL( dht_lapi_humiditydecimal ) },
|
||||
{ LSTRKEY( "temperaturedecimal" ), LFUNCVAL( dht_lapi_temperaturedecimal ) },
|
||||
{ LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) },
|
||||
#if LUA_OPTIMIZE_MEMORY > 0
|
||||
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
|
||||
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },
|
||||
|
|
Loading…
Reference in New Issue