Initially everything works ok. I'm getting a soil moisture from a hygrometer and successfully post to the API. However, after some time it simply jumps to "disconnection" instead of going to my "connection" callback and doesn't send the data. I left it running all night and it occasionally works again but normally only once or twice.
My code is as such:
function sendData()
connout = nil
connout = net.createConnection(net.TCP, 0)
print("Moisture:"..adc.read(0)..".")
connout:on("receive", function(connout, payloadout)
if (string.find(payloadout, "Status: 200 OK") ~= nil) then
print("Posted OK");
end
end)
connout:on("connection", function(connout, payloadout)
print ("Posting...");
connout:send("GET /update?api_key=[KEY]&field1=" .. adc.read(0)
.. " HTTP/1.1\r\n"
.. "Host: api.thingspeak.com\r\n"
.. "Connection: close\r\n"
.. "Accept: */*\r\n"
.. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
.. "\r\n")
print("Sent");
end)
connout:on("disconnection", function(connout, payloadout)
print("Disconnection");
connout:close();
collectgarbage();
end)
print("================");
print("Starting...");
connout:connect(80,'184.106.153.149')
end
sendData()
tmr.alarm(0, 60000, 1, function() sendData() end )
I've tried everything i can think of and many code examples from the web. Any idea what could be doing this? Their API limit is 15 seconds so i don't think it's that.
Any help is greatly appreciated.