Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  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 = 4
  6. Humidity = 0
  7. HumidityDec=0
  8. Temperature = 0
  9. TemperatureDec=0
  10. Checksum = 0
  11. ChecksumTest=0
  12.  
  13.  
  14. function getTemp()
  15. Humidity = 0
  16. HumidityDec=0
  17. Temperature = 0
  18. TemperatureDec=0
  19. Checksum = 0
  20. ChecksumTest=0
  21.  
  22. --Data stream acquisition timing is critical. There's
  23. --barely enough speed to work with to make this happen.
  24. --Pre-allocate vars used in loop.
  25.  
  26. bitStream = {}
  27. for j = 1, 40, 1 do
  28. bitStream[j]=0
  29. end
  30. bitlength=0
  31.  
  32. gpio.mode(pin, gpio.OUTPUT)
  33. gpio.write(pin, gpio.LOW)
  34. tmr.delay(20000)
  35. --Use Markus Gritsch trick to speed up read/write on GPIO
  36. gpio_read=gpio.read
  37. gpio_write=gpio.write
  38.  
  39. gpio.mode(pin, gpio.INPUT)
  40.  
  41. --bus will always let up eventually, don't bother with timeout
  42. while (gpio_read(pin)==0 ) do end
  43.  
  44. c=0
  45. while (gpio_read(pin)==1 and c<100) do c=c+1 end
  46.  
  47. --bus will always let up eventually, don't bother with timeout
  48. while (gpio_read(pin)==0 ) do end
  49.  
  50. c=0
  51. while (gpio_read(pin)==1 and c<100) do c=c+1 end
  52.  
  53. --acquisition loop
  54. for j = 1, 40, 1 do
  55. while (gpio_read(pin)==1 and bitlength<10 ) do
  56. bitlength=bitlength+1
  57. end
  58. bitStream[j]=bitlength
  59. bitlength=0
  60. --bus will always let up eventually, don't bother with timeout
  61. while (gpio_read(pin)==0) do end
  62. end
  63.  
  64. --DHT data acquired, process.
  65.  
  66. for i = 1, 8, 1 do
  67. if (bitStream[i+0] > 2) then
  68. Humidity = Humidity+2^(8-i)
  69. end
  70. end
  71. for i = 1, 8, 1 do
  72. if (bitStream[i+8] > 2) then
  73. HumidityDec = HumidityDec+2^(8-i)
  74. end
  75. end
  76. for i = 1, 8, 1 do
  77. if (bitStream[i+16] > 2) then
  78. Temperature = Temperature+2^(8-i)
  79. end
  80. end
  81. for i = 1, 8, 1 do
  82. if (bitStream[i+24] > 2) then
  83. TemperatureDec = TemperatureDec+2^(8-i)
  84. end
  85. end
  86. for i = 1, 8, 1 do
  87. if (bitStream[i+32] > 2) then
  88. Checksum = Checksum+2^(8-i)
  89. end
  90. end
  91. ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF
  92.  
  93. print ("Temperature: "..Temperature.."."..TemperatureDec)
  94. print ("Humidity: "..Humidity.."."..HumidityDec)
  95. print ("ChecksumReceived: "..Checksum)
  96. print ("ChecksumTest: "..ChecksumTest)
  97. end
  98.  
  99. --- Get temp and send data to thingspeak.com
  100. function sendData()
  101. getTemp()
  102. -- conection to thingspeak.com
  103. print("Sending data to thingspeak.com")
  104. conn=net.createConnection(net.TCP, 0)
  105. conn:on("receive", function(conn, payload) print(payload) end)
  106. -- api.thingspeak.com 184.106.153.149
  107. conn:connect(80,'184.106.153.149')
  108. conn:send("GET /update?key=Aqui a chave API&field1="..Temperature.."."..TemperatureDec.."&field2="..Humidity.."."..HumidityDec.." HTTP/1.1\r\n")
  109. conn:send("Host: api.thingspeak.com\r\n")
  110. conn:send("Accept: */*\r\n")
  111. conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
  112. conn:send("\r\n")
  113. conn:on("sent",function(conn)
  114. print("Closing connection")
  115. conn:close()
  116. end)
  117. conn:on("disconnection", function(conn)
  118. print("Got disconnection...")
  119. end)
  120. end
  121. -- send data every X ms to thing speak
  122. tmr.alarm(2, 6000, 1, function() sendData() end )
  123.  
  124. -- Carregue primeiro o code acima
  125. -- Em seguida o code abaixo
  126.  
  127. print("Setting up WIFI...")
  128. wifi.setmode(wifi.STATION)
  129. --modify according your wireless router settings
  130. wifi.sta.config("Aqui o nome da sua rede","Aqui a senha da sua rede")
  131. wifi.sta.connect()
  132. tmr.alarm(1, 1000, 1, function()
  133. if wifi.sta.getip()== nil then
  134. print("IP unavaiable, Waiting...")
  135. else
  136. tmr.stop(1)
  137. print("Config done, IP is "..wifi.sta.getip())
  138. dofile("dht11.lua")
  139. end
  140. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement