From 4d4442318deff79702df10dc5559840ec7d5220a Mon Sep 17 00:00:00 2001
From: Marcos Kirsch
Date: Mon, 15 Feb 2016 14:20:28 -0600
Subject: [PATCH] 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.
---
http/args.lua | 29 ++++++++++++++---------------
http/file_list.lua | 29 +++++++++++++----------------
http/garage_door_opener.lua | 2 ++
http/node_info.lua | 10 +++-------
4 files changed, 32 insertions(+), 38 deletions(-)
diff --git a/http/args.lua b/http/args.lua
index c9310c4..734a535 100644
--- a/http/args.lua
+++ b/http/args.lua
@@ -1,26 +1,25 @@
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")
- connection:send('Arguments')
- connection:send('')
- connection:send('Arguments
')
+ dofile("httpserver-header.lc")(connection, 200, 'html')
- local form = [===[
+ connection:send([===[
+ ArgumentsArguments
- ]===]
+ ]===])
+ coroutine.yield()
- connection:send(form)
-
- connection:send('Received the following values:
')
- connection:send("\n")
- for name, value in pairs(args) do
- connection:send('- ' .. name .. ': ' .. tostring(value) .. "
\n")
+ if args["submit"] ~= nil then
+ connection:send("Received the following values:
")
+ coroutine.yield()
+ for name, value in pairs(args) do
+ connection:send('- ' .. name .. ': ' .. tostring(value) .. "
\n")
+ coroutine.yield()
+ end
end
- connection:send("
\n")
- connection:send('')
+ connection:send("
\n")
end
diff --git a/http/file_list.lua b/http/file_list.lua
index 384d088..3628dd0 100644
--- a/http/file_list.lua
+++ b/http/file_list.lua
@@ -1,30 +1,27 @@
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")
- connection:send('Server File Listing')
- connection:send('')
+ dofile("httpserver-header.lc")(connection, 200, 'html')
+
+ connection:send([===[
+ Server File Listing
+
+ Server File Listing
+ ]===])
coroutine.yield()
- connection:send('Server File Listing
')
local remaining, used, total=file.fsinfo()
- connection:send("Total size: " .. total .. " bytes
\n")
- connection:send("In Use: " .. used .. " bytes
\n")
- connection:send("Free: " .. remaining .. " bytes
\n")
-
- connection:send("\n")
- connection:send("Files:
\n")
- connection:send("
\n")
+ connection:send("Total size: " .. total .. " bytes
\n" ..
+ "In Use: " .. used .. " bytes
\n" ..
+ "Free: " .. remaining .. " bytes
\n" ..
+ "\nFiles:
\n
\n")
+ coroutine.yield()
for name, size in pairs(file.list()) do
-
local isHttpFile = string.match(name, "(http/)") ~= nil
if isHttpFile then
local url = string.match(name, ".*/(.*)")
connection:send(' - ' .. url .. " (" .. size .. " bytes)
\n")
- -- this list could be very long, so we'll yield in order to avoid overflowing the send buffer.
coroutine.yield()
end
end
- connection:send("
\n")
- connection:send("
\n")
- connection:send('')
+ connection:send("\n\n")
end
diff --git a/http/garage_door_opener.lua b/http/garage_door_opener.lua
index e5dc874..610854a 100644
--- a/http/garage_door_opener.lua
+++ b/http/garage_door_opener.lua
@@ -17,6 +17,7 @@ local function pushTheButton(connection, pin)
-- 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")
+ coroutine.yield()
connection:send('{"error":0, "message":"OK"}')
end
@@ -27,6 +28,7 @@ return function (connection, args)
elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2
else
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"}')
end
end
diff --git a/http/node_info.lua b/http/node_info.lua
index fdd8868..dac2c3f 100644
--- a/http/node_info.lua
+++ b/http/node_info.lua
@@ -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)
connection:send("".. attr .. ": " .. val .. "
\n")
+ coroutine.yield()
end
return function (connection, args)
- collectgarbage()
- sendHeader(connection)
+ dofile("httpserver-header.lc")(connection, 200, 'html')
connection:send('A Lua script sampleNode info
')
+ coroutine.yield()
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info();
sendAttr(connection, "NodeMCU version" , majorVer.."."..minorVer.."."..devVer)
sendAttr(connection, "chipid" , chipid)