As the title says... Chat on...

User avatar
By Julius Uza
#47500 Hello,

here what I made. This code reads infor from DHT11 and sends to thingspeak.
The problem is, after reboot I get correct readings and all other readings are the same, how to clear readings after sending them to thingspeak?
My workaround for now is to point to not existing lua script after sending info to thingspeak and board reboots :)
Here is my code, can you help me:
Code: Select allPIN = 1 --  data pin
DHT= require("dht_lib")
DHT.read(PIN)

t = DHT.getTemperature()
h = DHT.getHumidity()

function getTemp()
while ((t == nil) or (h==nil)) do
  print(".")
  DHT.read(PIN)
  tmr.delay(1000000)
  t = DHT.getTemperature()
  h = DHT.getHumidity()
end
 
-- temperature in degrees Celsius  and Farenheit
print("Temperature: "..((t-(t % 10)) / 10).."."..(t % 10).." deg C")
-- humidity
print("Humidity: "..((h - (h % 10)) / 10).."."..(h % 10).."%")
 
-- release module
DHT = nil
package.loaded["dht_lib"]=nil   
end
--- Get temp and send data to thingspeak.com
function sendData()
getTemp()
-- conection to thingspeak.com
print("Sending data to thingspeak.com")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=QWERTY&field1="..((t-(t % 10)) / 10).."."..(t % 10).."&field2="..((h - (h % 10)) / 10).."."..(h % 10).." HTTP/1.1\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
                      print("Closing connection")
                      conn:close()
                  end)
conn:on("disconnection", function(conn)
                      print("Got disconnection...")
  end)
end
-- send data every X ms to thing speak
tmr.alarm(2, 60000, 1, function() sendData() end )