Skip to content

Instantly share code, notes, and snippets.

@zhujunsan
Created January 8, 2015 13:36
Show Gist options
  • Save zhujunsan/36231aba250f63dda7a8 to your computer and use it in GitHub Desktop.
Save zhujunsan/36231aba250f63dda7a8 to your computer and use it in GitHub Desktop.
Http Request Lib on NodeMCU
DEBUG = true
function getWpath(address, port, path)
if conn_busy == false or conn_busy == nil then
conn_busy = true
local conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
if (DEBUG) print("received") end
if string.len(payload) < 1460 then conn:close() end
print(payload)
end)
if (DEBUG)
conn:on("reconnection", function(conn)
print("reconnected")
end)
conn:on("disconnection", function(conn)
print("disconnected")
conn_busy = false
end)
conn:on("sent", function(conn)
print("sent")
end)
end
conn:on("connection", function(conn)
if (DEBUG) print("connected") end
conn:send("GET /"..path.." HTTP/1.1\r\n"
.. "Host: " .. address .. "\r\n"
.. "Connection: close\r\n"
.. "User-Agent: NodeMCU-" .. node.chipid() .. "\r\n"
.. "\r\n")
end)
conn:connect(port, address)
end
end
function get(address)
getWpath(address, 80, "?uptime="..tmr.now())
end
print('"get" function set!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment