- Thu Sep 24, 2015 4:25 am
#29765
It's the wrong time sequence of commands in your program. First, you have to wait for a connection, send, and you can not close the connection before everything is sent.
There is a functional sketch.
Code: Select all-- Tested with Lua NodeMCU 0.9.6 (nodemcu_float_0.9.6-dev_20150704.bin)
-- Minimal period for data send to api.thingspeak.com is 15s
--****************************************************************
ssid="yourSSID" -- your router SSID
pass="yourPassword" -- your router password
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,pass,1)
apiKey="yourAPIwriteKey" -- set your thingspeak.com key
tempin=21.3
hum=52.5
tempout=18.7
-- send data to https://api.thingspeak.com
conn = net.createConnection(net.TCP, 0)
conn:connect(80,'api.thingspeak.com') -- This row is good, but only for newer firmware
--conn:connect(80,'184.106.153.149') -- This is worse, but it also works well with the older firmware.
conn:on("connection",
function(conn)
print("Connection!")
conn:send('GET /update?key='..apiKey..
'&headers=false'..
'&field1='..tempin..
'&field2='..hum..
'&field3='..tempout..
' HTTP/1.1\r\n'..
'Host: api.thingspeak.com\r\nAccept: */*\r\n'..
'User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
end)
conn:on("sent",
function(conn)
print("Sent!")
--conn:close() -- You can enable this row for skip thingspeak.com answer
end)
conn:on("receive",
function(conn, payload)
print("Receive!")
print(payload)
conn:close()
end)
conn:on("disconnection",
function(conn)
print("Disconnection!")
end)