dht.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "cpu_esp8266.h"
  6. #include "dht/dht.h"
  7. #define NUM_DHT GPIO_PIN_NUM
  8. // ****************************************************************************
  9. // DHT functions
  10. int platform_dht_exists( unsigned id )
  11. {
  12. return ((id < NUM_DHT) && (id > 0));
  13. }
  14. static void aux_read( lua_State *L )
  15. {
  16. double temp = dht_getTemperature();
  17. double humi = dht_getHumidity();
  18. int tempdec = (int)((temp - (int)temp) * 1000);
  19. int humidec = (int)((humi - (int)humi) * 1000);
  20. lua_pushnumber( L, (lua_Float) temp );
  21. lua_pushnumber( L, (lua_Float) humi );
  22. lua_pushinteger( L, tempdec );
  23. lua_pushinteger( L, humidec );
  24. }
  25. // Lua: status, temp, humi, tempdec, humidec = dht.read( id )
  26. static int dht_lapi_read( lua_State *L )
  27. {
  28. unsigned id = luaL_checkinteger( L, 1 );
  29. MOD_CHECK_ID( dht, id );
  30. lua_pushinteger( L, dht_read_universal(id) );
  31. aux_read( L );
  32. return 5;
  33. }
  34. // Lua: status, temp, humi, tempdec, humidec = dht.read11( id ))
  35. static int dht_lapi_read11( lua_State *L )
  36. {
  37. unsigned id = luaL_checkinteger( L, 1 );
  38. MOD_CHECK_ID( dht, id );
  39. lua_pushinteger( L, dht_read11(id) );
  40. aux_read( L );
  41. return 5;
  42. }
  43. // Lua: status, temp, humi, tempdec, humidec = dht.readxx( id ))
  44. static int dht_lapi_readxx( lua_State *L )
  45. {
  46. unsigned id = luaL_checkinteger( L, 1 );
  47. MOD_CHECK_ID( dht, id );
  48. lua_pushinteger( L, dht_read(id) );
  49. aux_read( L );
  50. return 5;
  51. }
  52. // // Lua: result = dht.humidity()
  53. // static int dht_lapi_humidity( lua_State *L )
  54. // {
  55. // lua_pushnumber( L, dht_getHumidity() );
  56. // return 1;
  57. // }
  58. // // Lua: result = dht.humiditydecimal()
  59. // static int dht_lapi_humiditydecimal( lua_State *L )
  60. // {
  61. // double value = dht_getHumidity();
  62. // int result = (int)((value - (int)value) * 1000);
  63. // lua_pushnumber( L, result );
  64. // return 1;
  65. // }
  66. // // Lua: result = dht.temperature()
  67. // static int dht_lapi_temperature( lua_State *L )
  68. // {
  69. // lua_pushnumber( L, dht_getTemperature() );
  70. // return 1;
  71. // }
  72. // // Lua: result = dht.temperaturedecimal()
  73. // static int dht_lapi_temperaturedecimal( lua_State *L )
  74. // {
  75. // double value = dht_getTemperature();
  76. // int result = (int)((value - (int)value) * 1000);
  77. // lua_pushnumber( L, result );
  78. // return 1;
  79. // }
  80. // Module function map
  81. LROT_BEGIN(dht, NULL, 0)
  82. LROT_FUNCENTRY( read, dht_lapi_read )
  83. LROT_FUNCENTRY( read11, dht_lapi_read11 )
  84. LROT_FUNCENTRY( readxx, dht_lapi_readxx )
  85. LROT_NUMENTRY( OK, DHTLIB_OK )
  86. LROT_NUMENTRY( ERROR_CHECKSUM, DHTLIB_ERROR_CHECKSUM )
  87. LROT_NUMENTRY( ERROR_TIMEOUT, DHTLIB_ERROR_TIMEOUT )
  88. LROT_END(dht, NULL, 0)
  89. NODEMCU_MODULE(DHT, "dht", dht, NULL);