dht.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44).
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "platform.h"
  5. #include "dht.h"
  6. #define NUM_DHT GPIO_PIN_NUM
  7. // ****************************************************************************
  8. // DHT functions
  9. int platform_dht_exists( unsigned id )
  10. {
  11. return ((id < NUM_DHT) && (id > 0));
  12. }
  13. // Lua: status, temp, humi, tempdec, humidec = dht.read( id )
  14. static int dht_lapi_read( lua_State *L )
  15. {
  16. unsigned id = luaL_checkinteger( L, 1 );
  17. MOD_CHECK_ID( dht, id );
  18. lua_pushinteger( L, dht_read_universal(id) );
  19. double temp = dht_getTemperature();
  20. double humi = dht_getHumidity();
  21. int tempdec = (int)((temp - (int)temp) * 1000);
  22. int humidec = (int)((humi - (int)humi) * 1000);
  23. lua_pushnumber( L, temp );
  24. lua_pushnumber( L, humi );
  25. lua_pushnumber( L, tempdec );
  26. lua_pushnumber( L, humidec );
  27. return 5;
  28. }
  29. // Lua: status, temp, humi, tempdec, humidec = dht.read11( id ))
  30. static int dht_lapi_read11( lua_State *L )
  31. {
  32. unsigned id = luaL_checkinteger( L, 1 );
  33. MOD_CHECK_ID( dht, id );
  34. lua_pushinteger( L, dht_read11(id) );
  35. double temp = dht_getTemperature();
  36. double humi = dht_getHumidity();
  37. int tempdec = (int)((temp - (int)temp) * 1000);
  38. int humidec = (int)((humi - (int)humi) * 1000);
  39. lua_pushnumber( L, temp );
  40. lua_pushnumber( L, humi );
  41. lua_pushnumber( L, tempdec );
  42. lua_pushnumber( L, humidec );
  43. return 5;
  44. }
  45. // Lua: status, temp, humi, tempdec, humidec = dht.readxx( id ))
  46. static int dht_lapi_readxx( lua_State *L )
  47. {
  48. unsigned id = luaL_checkinteger( L, 1 );
  49. MOD_CHECK_ID( dht, id );
  50. lua_pushinteger( L, dht_read(id) );
  51. double temp = dht_getTemperature();
  52. double humi = dht_getHumidity();
  53. int tempdec = (int)((temp - (int)temp) * 1000);
  54. int humidec = (int)((humi - (int)humi) * 1000);
  55. lua_pushnumber( L, temp );
  56. lua_pushnumber( L, humi );
  57. lua_pushnumber( L, tempdec );
  58. lua_pushnumber( L, humidec );
  59. return 5;
  60. }
  61. // // Lua: result = dht.humidity()
  62. // static int dht_lapi_humidity( lua_State *L )
  63. // {
  64. // lua_pushnumber( L, dht_getHumidity() );
  65. // return 1;
  66. // }
  67. // // Lua: result = dht.humiditydecimal()
  68. // static int dht_lapi_humiditydecimal( lua_State *L )
  69. // {
  70. // double value = dht_getHumidity();
  71. // int result = (int)((value - (int)value) * 1000);
  72. // lua_pushnumber( L, result );
  73. // return 1;
  74. // }
  75. // // Lua: result = dht.temperature()
  76. // static int dht_lapi_temperature( lua_State *L )
  77. // {
  78. // lua_pushnumber( L, dht_getTemperature() );
  79. // return 1;
  80. // }
  81. // // Lua: result = dht.temperaturedecimal()
  82. // static int dht_lapi_temperaturedecimal( lua_State *L )
  83. // {
  84. // double value = dht_getTemperature();
  85. // int result = (int)((value - (int)value) * 1000);
  86. // lua_pushnumber( L, result );
  87. // return 1;
  88. // }
  89. // Module function map
  90. static const LUA_REG_TYPE dht_map[] = {
  91. { LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) },
  92. { LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) },
  93. { LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) },
  94. { LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
  95. { LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },
  96. { LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) },
  97. { LNILKEY, LNILVAL }
  98. };
  99. NODEMCU_MODULE(DHT, "dht", dht_map, NULL);