DHT12 (Lua)

DHT12 (Lua)

This page is to Setup and Config DHT12 (Lua)

Hardware and Software

Hardware

DHT12 (Lua)

Software

Esplorer

Sanki Notes

    • DHT Pin 1 (left + No Words Face) : VCC

    • DHT Pin 2 : D2 (Nodemcu/esp-01)

    • DHT Pin 3 : NIL

    • DHT Pin 4 : GND

Examples

This example is ..........

            1. -- Measure temperature, humidity and post data to thingspeak.com

            2. -- 2014 OK1CDJ

            3. -- DHT11 code is from esp8266.com

            4. ---Sensor DHT11 is conntected to GPIO2

            5. pin = 2

            6. Humidity = 0

            7. HumidityDec=0

            8. Temperature = 0

            9. TemperatureDec=0

            10. Checksum = 0

            11. ChecksumTest=0

            12. function getTemp()

            13. Humidity = 0

            14. HumidityDec=0

            15. Temperature = 0

            16. TemperatureDec=0

            17. Checksum = 0

            18. ChecksumTest=0

            19. --Data stream acquisition timing is critical. There's

            20. --barely enough speed to work with to make this happen.

            21. --Pre-allocate vars used in loop.

            22. bitStream = {}

            23. for j = 1, 40, 1 do

            24. bitStream[j]=0

            25. end

            26. bitlength=0

            27. gpio.mode(pin, gpio.OUTPUT)

            28. gpio.write(pin, gpio.LOW)

            29. tmr.delay(20000)

            30. --Use Markus Gritsch trick to speed up read/write on GPIO

            31. gpio_read=gpio.read

            32. gpio_write=gpio.write

            33. gpio.mode(pin, gpio.INPUT)

            34. --bus will always let up eventually, don't bother with timeout

            35. while (gpio_read(pin)==0 ) do end

            36. c=0

            37. while (gpio_read(pin)==1 and c<100) do c=c+1 end

            38. --bus will always let up eventually, don't bother with timeout

            39. while (gpio_read(pin)==0 ) do end

            40. c=0

            41. while (gpio_read(pin)==1 and c<100) do c=c+1 end

            42. --acquisition loop

            43. for j = 1, 40, 1 do

            44. while (gpio_read(pin)==1 and bitlength<10 ) do

            45. bitlength=bitlength+1

            46. end

            47. bitStream[j]=bitlength

            48. bitlength=0

            49. --bus will always let up eventually, don't bother with timeout

            50. while (gpio_read(pin)==0) do end

            51. end

            52. --DHT data acquired, process.

            53. for i = 1, 8, 1 do

            54. if (bitStream[i+0] > 2) then

            55. Humidity = Humidity+2^(8-i)

            56. end

            57. end

            58. for i = 1, 8, 1 do

            59. if (bitStream[i+8] > 2) then

            60. HumidityDec = HumidityDec+2^(8-i)

            61. end

            62. end

            63. for i = 1, 8, 1 do

            64. if (bitStream[i+16] > 2) then

            65. Temperature = Temperature+2^(8-i)

            66. end

            67. end

            68. for i = 1, 8, 1 do

            69. if (bitStream[i+24] > 2) then

            70. TemperatureDec = TemperatureDec+2^(8-i)

            71. end

            72. end

            73. for i = 1, 8, 1 do

            74. if (bitStream[i+32] > 2) then

            75. Checksum = Checksum+2^(8-i)

            76. end

            77. end

            78. ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF

            79. print ("Temperature: "..Temperature.."."..TemperatureDec)

            80. print ("Humidity: "..Humidity.."."..HumidityDec)

            81. print ("ChecksumReceived: "..Checksum)

            82. print ("ChecksumTest: "..ChecksumTest)

            83. end

            84. --- Get temp and send data to thingspeak.com

            85. function sendData()

            86. getTemp()

            87. -- conection to thingspeak.com

            88. print("Sending data to thingspeak.com")

            89. conn=net.createConnection(net.TCP, 0)

            90. conn:on("receive", function(conn, payload) print(payload) end)

            91. -- api.thingspeak.com 184.106.153.149

            92. conn:connect(80,'192.168.0.107')

            93. conn:send("GET /update?key=DBKBFHIR0L020NIQ&field1="..Temperature.."."..TemperatureDec.."&field2="..Humidity.."."..HumidityDec.." HTTP/1.1\r\n")

            94. conn:send("Host: api.thingspeak.com\r\n")

            95. conn:send("Accept: */*\r\n")

            96. conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")

            97. conn:send("\r\n")

            98. conn:on("sent",function(conn)

            99. print("Closing connection")

            100. conn:close()

            101. end)

            102. conn:on("disconnection", function(conn)

            103. print("Got disconnection...")

            104. end)

            105. end

            106. -- send data every X ms to thing speak

            107. --tmr.alarm(2, 60000, 1, function() sendData() end )

            108. tmr.alarm(2, 10000, 1,

            109. function()

            110. --sendData()

            111. getTemp()

            112. end )