local api_host = 'api.thingspeak.com'
local api_path = '/update'
local get_template = 'GET '..api_path..'?api_key='..api_key
local function update_factory(getstring)
return function (s)
if debug then print('Connected!') end
if debug then print('Sending: '..getstring) end
s:send(getstring..'\r\n\r\n')
end
end
local function sendUpdate(getstring)
local s = net.createConnection(net.TCP, 1)
if debug then print("socket created...") end
s:connect(443,api_host)
if debug then print ("connect requested...") end
s:on("connection", update_factory(getstring) )
s:on("receive", function (s, r) if debug then print("Return from API: "..r) s:close() end end)
end
function M.update(data)
local getstring = get_template
for i,v in ipairs(data) do
if i < 9 then
if debug then print('Field '..i..':'..v) end
getstring = getstring.."&field"..i.."="..v
end
end
if debug then print('Preparing connection') end
sendUpdate(getstring)
endI set debug to true in the global scope, and It gets as far as "connect requested..." but never actually seems to connect. Not in a position where I can get a packet capture right now, any thoughts?
One thing I thought is maybe s falls out of scope and gets GC'd after sendUpdate ends?