dht_lib.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. -- ***************************************************************************
  2. -- DHTxx(11,21,22) module for ESP8266 with nodeMCU
  3. --
  4. -- Written by Javier Yanez mod by Martin
  5. -- but based on a script of Pigs Fly from ESP8266.com forum
  6. --
  7. -- MIT license, http://opensource.org/licenses/MIT
  8. -- ***************************************************************************
  9. --Support list:
  10. --DHT11 Tested ->read11
  11. --DHT21 Not Tested->read22
  12. --DHT22 Tested->read22
  13. --==========================Module Part======================
  14. local moduleName = ...
  15. local M = {}
  16. _G[moduleName] = M
  17. --==========================Local the UMI and TEMP===========
  18. local humidity
  19. local temperature
  20. --==========================Local the bitStream==============
  21. local bitStream = {}
  22. ---------------------------Read bitStream from DHTXX--------------------------
  23. local function read(pin)
  24. local bitlength = 0
  25. humidity = 0
  26. temperature = 0
  27. -- Use Markus Gritsch trick to speed up read/write on GPIO
  28. local gpio_read = gpio.read
  29. for j = 1, 40, 1 do
  30. bitStream[j] = 0
  31. end
  32. -- Step 1: send out start signal to DHT22
  33. gpio.mode(pin, gpio.OUTPUT)
  34. gpio.write(pin, gpio.HIGH)
  35. tmr.delay(100)
  36. gpio.write(pin, gpio.LOW)
  37. tmr.delay(20000)
  38. gpio.write(pin, gpio.HIGH)
  39. gpio.mode(pin, gpio.INPUT)
  40. -- Step 2: Receive bitStream from DHT11/22
  41. -- bus will always let up eventually, don't bother with timeout
  42. while (gpio_read(pin) == 0 ) do end
  43. local c=0
  44. while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
  45. -- bus will always let up eventually, don't bother with timeout
  46. while (gpio_read(pin) == 0 ) do end
  47. c=0
  48. while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
  49. -- Step 3: DHT22 send data
  50. for j = 1, 40, 1 do
  51. while (gpio_read(pin) == 1 and bitlength < 10 ) do
  52. bitlength = bitlength + 1
  53. end
  54. bitStream[j] = bitlength
  55. bitlength = 0
  56. -- bus will always let up eventually, don't bother with timeout
  57. while (gpio_read(pin) == 0) do end
  58. end
  59. end
  60. ---------------------------Convert the bitStream into Number through DHT11 Ways--------------------------
  61. function M.read11(pin)
  62. --As for DHT11 40Bit is consisit of 5Bytes
  63. --First byte->Humidity Data's Int part
  64. --Sencond byte->Humidity Data's Float Part(Which should be empty)
  65. --Third byte->Temp Data;s Intpart
  66. --Forth byte->Temp Data's Float Part(Which should be empty)
  67. --Fifth byte->SUM Byte, Humi+Temp
  68. read(pin)
  69. local checksum = 0
  70. local checksumTest
  71. --DHT data acquired, process.
  72. for i = 1, 8, 1 do -- Byte[0]
  73. if (bitStream[i] > 3) then
  74. humidity = humidity + 2 ^ (8 - i)
  75. end
  76. end
  77. for i = 1, 8, 1 do -- Byte[2]
  78. if (bitStream[i + 16] > 3) then
  79. temperature = temperature + 2 ^ (8 - i)
  80. end
  81. end
  82. for i = 1, 8, 1 do --Byte[4]
  83. if (bitStream[i + 32] > 3) then
  84. checksum = checksum + 2 ^ (8 - i)
  85. end
  86. end
  87. if(checksum ~= humidity+temperature) then
  88. humidity = nil
  89. temperature = nil
  90. end
  91. end
  92. ---------------------------Convert the bitStream into Number through DHT22 Ways--------------------------
  93. function M.read22( pin )
  94. --As for DHT22 40Bit is consisit of 5Bytes
  95. --First byte->Humidity Data's High Bit
  96. --Sencond byte->Humidity Data's Low Bit(And if over 0x8000, use complement)
  97. --Third byte->Temp Data's High Bit
  98. --Forth byte->Temp Data's Low Bit
  99. --Fifth byte->SUM Byte
  100. read(pin)
  101. local checksum = 0
  102. local checksumTest
  103. --DHT data acquired, process.
  104. for i = 1, 16, 1 do
  105. if (bitStream[i] > 3) then
  106. humidity = humidity + 2 ^ (16 - i)
  107. end
  108. end
  109. for i = 1, 16, 1 do
  110. if (bitStream[i + 16] > 3) then
  111. temperature = temperature + 2 ^ (16 - i)
  112. end
  113. end
  114. for i = 1, 8, 1 do
  115. if (bitStream[i + 32] > 3) then
  116. checksum = checksum + 2 ^ (8 - i)
  117. end
  118. end
  119. checksumTest = (bit.band(humidity, 0xFF) + bit.rshift(humidity, 8) + bit.band(temperature, 0xFF) + bit.rshift(temperature, 8))
  120. checksumTest = bit.band(checksumTest, 0xFF)
  121. if temperature > 0x8000 then
  122. -- convert to negative format
  123. temperature = -(temperature - 0x8000)
  124. end
  125. -- conditions compatible con float point and integer
  126. if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then
  127. humidity = nil
  128. end
  129. end
  130. ---------------------------Check out the data--------------------------
  131. function M.getTemperature()
  132. return temperature
  133. end
  134. function M.getHumidity()
  135. return humidity
  136. end
  137. return M