dht.c 3.2 KB

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