Fix example server-side scripts to work on new nodemcu-firmware. Specifically, call coroutine.yield() after each connection:send() except the last one, since we can no longer queue sends.

This commit is contained in:
Marcos Kirsch 2016-02-15 14:20:28 -06:00
parent 4ff7b2b78d
commit 4d4442318d
4 changed files with 32 additions and 38 deletions

View File

@ -1,26 +1,25 @@
return function (connection, args) return function (connection, args)
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n") dofile("httpserver-header.lc")(connection, 200, 'html')
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Arguments</title></head>')
connection:send('<body>')
connection:send('<h1>Arguments</h1>')
local form = [===[ connection:send([===[
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Arguments</title></head><body><h1>Arguments</h1>
<form method="GET"> <form method="GET">
First name:<br><input type="text" name="firstName"><br> First name:<br><input type="text" name="firstName"><br>
Last name:<br><input type="text" name="lastName"><br> Last name:<br><input type="text" name="lastName"><br>
<input type="radio" name="sex" value="male" checked>Male<input type="radio" name="sex" value="female">Female<br> <input type="radio" name="sex" value="male" checked>Male<input type="radio" name="sex" value="female">Female<br>
<input type="submit" value="Submit"> <input type="submit" name="submit" value="Submit">
</form> </form>
]===] ]===])
coroutine.yield()
connection:send(form) if args["submit"] ~= nil then
connection:send("<h2>Received the following values:</h2><ul>")
connection:send('<h2>Received the following values:</h2>') coroutine.yield()
connection:send("<ul>\n")
for name, value in pairs(args) do for name, value in pairs(args) do
connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n") connection:send('<li><b>' .. name .. ':</b> ' .. tostring(value) .. "<br></li>\n")
coroutine.yield()
end
end end
connection:send("</ul>\n") connection:send("</ul>\n</body></html>")
connection:send('</body></html>')
end end

View File

@ -1,30 +1,27 @@
return function (connection, args) return function (connection, args)
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n") dofile("httpserver-header.lc")(connection, 200, 'html')
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>')
connection:send('<body>') connection:send([===[
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>
<body>
<h1>Server File Listing</h1>
]===])
coroutine.yield() coroutine.yield()
connection:send('<h1>Server File Listing</h1>')
local remaining, used, total=file.fsinfo() local remaining, used, total=file.fsinfo()
connection:send("<b>Total size: </b> " .. total .. " bytes<br/>\n") connection:send("<b>Total size: </b> " .. total .. " bytes<br/>\n" ..
connection:send("<b>In Use: </b> " .. used .. " bytes<br/>\n") "<b>In Use: </b> " .. used .. " bytes<br/>\n" ..
connection:send("<b>Free: </b> " .. remaining .. " bytes<br/>\n") "<b>Free: </b> " .. remaining .. " bytes<br/>\n" ..
"<p>\n<b>Files:</b><br/>\n<ul>\n")
connection:send("<p>\n") coroutine.yield()
connection:send("<b>Files:</b><br/>\n")
connection:send("<ul>\n")
for name, size in pairs(file.list()) do for name, size in pairs(file.list()) do
local isHttpFile = string.match(name, "(http/)") ~= nil local isHttpFile = string.match(name, "(http/)") ~= nil
if isHttpFile then if isHttpFile then
local url = string.match(name, ".*/(.*)") local url = string.match(name, ".*/(.*)")
connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n") connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
-- this list could be very long, so we'll yield in order to avoid overflowing the send buffer.
coroutine.yield() coroutine.yield()
end end
end end
connection:send("</ul>\n") connection:send("</ul>\n</p>\n</body></html>")
connection:send("</p>\n")
connection:send('</body></html>')
end end

View File

@ -17,6 +17,7 @@ local function pushTheButton(connection, pin)
-- Send back JSON response. -- Send back JSON response.
connection:send("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n") connection:send("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n")
coroutine.yield()
connection:send('{"error":0, "message":"OK"}') connection:send('{"error":0, "message":"OK"}')
end end
@ -27,6 +28,7 @@ return function (connection, args)
elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2 elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2
else else
connection:send("HTTP/1.0 400 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n") connection:send("HTTP/1.0 400 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n")
coroutine.yield()
connection:send('{"error":-1, "message":"Bad door"}') connection:send('{"error":-1, "message":"Bad door"}')
end end
end end

View File

@ -1,16 +1,12 @@
local function sendHeader(connection)
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
end
local function sendAttr(connection, attr, val) local function sendAttr(connection, attr, val)
connection:send("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n") connection:send("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n")
coroutine.yield()
end end
return function (connection, args) return function (connection, args)
collectgarbage() dofile("httpserver-header.lc")(connection, 200, 'html')
sendHeader(connection)
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head><body><h1>Node info</h1><ul>') connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head><body><h1>Node info</h1><ul>')
coroutine.yield()
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info(); majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info();
sendAttr(connection, "NodeMCU version" , majorVer.."."..minorVer.."."..devVer) sendAttr(connection, "NodeMCU version" , majorVer.."."..minorVer.."."..devVer)
sendAttr(connection, "chipid" , chipid) sendAttr(connection, "chipid" , chipid)