Created
January 8, 2015 13:36
-
-
Save zhujunsan/36231aba250f63dda7a8 to your computer and use it in GitHub Desktop.
Http Request Lib on NodeMCU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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