Fix some typos
This commit is contained in:
parent
71058e6b44
commit
0120924403
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
local function getMimeType(ext)
|
local function getMimeType(ext)
|
||||||
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
|
-- A few MIME types. Keep list short. If you need something that is missing, let's add it.
|
||||||
local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", josn="application/json", png = "image/png"}
|
local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", json = "application/json", png = "image/png"}
|
||||||
if mt[ext] then return mt[ext] else return "text/plain" end
|
if mt[ext] then return mt[ext] else return "text/plain" end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
160
httpserver.lua
160
httpserver.lua
@ -1,79 +1,81 @@
|
|||||||
-- httpserver
|
-- httpserver
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
-- Starts web server in the specified port.
|
-- Starts web server in the specified port.
|
||||||
return function (port)
|
return function (port)
|
||||||
|
|
||||||
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
|
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
|
||||||
s:listen(
|
s:listen(
|
||||||
port,
|
port,
|
||||||
function (connection)
|
function (connection)
|
||||||
|
|
||||||
-- This variable holds the thread used for sending data back to the user.
|
-- This variable holds the thread used for sending data back to the user.
|
||||||
-- We do it in a separate thread because we need to yield when sending lots
|
-- We do it in a separate thread because we need to yield when sending lots
|
||||||
-- of data in order to avoid overflowing the mcu's buffer.
|
-- of data in order to avoid overflowing the mcu's buffer.
|
||||||
local connectionThread
|
local connectionThread
|
||||||
|
|
||||||
local function onGet(connection, uri)
|
local function onGet(connection, uri)
|
||||||
local fileServeFunction = nil
|
local fileServeFunction = nil
|
||||||
if #(uri.file) > 32 then
|
if #(uri.file) > 32 then
|
||||||
-- nodemcu-firmware cannot handle long filenames.
|
-- nodemcu-firmware cannot handle long filenames.
|
||||||
uri.args['code'] = 400
|
uri.args['code'] = 400
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
else
|
else
|
||||||
local fileExists = file.open(uri.file, "r")
|
local fileExists = file.open(uri.file, "r")
|
||||||
file.close()
|
file.close()
|
||||||
if not fileExists then
|
if not fileExists then
|
||||||
uri.args['code'] = 404
|
uri.args['code'] = 404
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
elseif uri.isScript then
|
elseif uri.isScript then
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
fileServeFunction = dofile(uri.file)
|
fileServeFunction = dofile(uri.file)
|
||||||
else
|
else
|
||||||
uri.args['file'] = uri.file
|
uri.args['file'] = uri.file
|
||||||
uri.args['ext'] = uri.ext
|
uri.args['ext'] = uri.ext
|
||||||
fileServeFunction = dofile("httpserver-static.lc")
|
fileServeFunction = dofile("httpserver-static.lc")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
connectionThread = coroutine.create(fileServeFunction)
|
connectionThread = coroutine.create(fileServeFunction)
|
||||||
--print("Thread created", connectionThread)
|
--print("Thread created", connectionThread)
|
||||||
coroutine.resume(connectionThread, connection, uri.args)
|
coroutine.resume(connectionThread, connection, uri.args)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onReceive(connection, payload)
|
local function onReceive(connection, payload)
|
||||||
-- print(payload) -- for debugging
|
-- print(payload) -- for debugging
|
||||||
-- parse payload and decide what to serve.
|
-- parse payload and decide what to serve.
|
||||||
local req = dofile("httpserver-request.lc")(payload)
|
local req = dofile("httpserver-request.lc")(payload)
|
||||||
print("Requested URI: " .. req.request)
|
print("Requested URI: " .. req.request)
|
||||||
if req.methodIsValid then
|
if req.methodIsValid then
|
||||||
if req.method == "GET" then onGet(connection, req.uri)
|
if req.method == "GET" then onGet(connection, req.uri)
|
||||||
else dofile("httpserver-static.lc")(conection, {code=501}) end
|
else dofile("httpserver-static.lc")(conection, {code=501}) end
|
||||||
else
|
else
|
||||||
dofile("httpserver-static.lc")(conection, {code=400})
|
dofile("httpserver-static.lc")(conection, {code=400})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onSent(connection, payload)
|
local function onSent(connection, payload)
|
||||||
local connectionThreadStatus = coroutine.status(connectionThread)
|
local connectionThreadStatus = coroutine.status(connectionThread)
|
||||||
-- print (connectionThread, "status is", connectionThreadStatus)
|
-- print (connectionThread, "status is", connectionThreadStatus)
|
||||||
if connectionThreadStatus == "suspended" then
|
if connectionThreadStatus == "suspended" then
|
||||||
-- Not finished sending file, resume.
|
-- Not finished sending file, resume.
|
||||||
-- print("Resume thread", connectionThread)
|
-- print("Resume thread", connectionThread)
|
||||||
coroutine.resume(connectionThread)
|
coroutine.resume(connectionThread)
|
||||||
elseif connectionThreadStatus == "dead" then
|
elseif connectionThreadStatus == "dead" then
|
||||||
-- We're done sending file.
|
-- We're done sending file.
|
||||||
-- print("Done thread", connectionThread)
|
-- print("Done thread", connectionThread)
|
||||||
connection:close()
|
connection:close()
|
||||||
connectionThread = nil
|
connectionThread = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
connection:on("receive", onReceive)
|
connection:on("receive", onReceive)
|
||||||
connection:on("sent", onSent)
|
connection:on("sent", onSent)
|
||||||
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port)
|
if wifi.sta.getip() then print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port)
|
||||||
return s
|
else print("nodemcu-httpserver running at http://" .. wifi.ap.getip() .. ":" .. port)
|
||||||
|
end
|
||||||
end
|
return s
|
||||||
|
|
||||||
|
end
|
||||||
|
|||||||
89
init.lua
89
init.lua
@ -1,38 +1,51 @@
|
|||||||
-- Tell the chip to connect to the access point
|
-- Tel--l the chip to connect to the access point
|
||||||
wifi.setmode(wifi.STATION)
|
--wifi.setmode(wifi.STATIONAP)
|
||||||
print('set mode=STATION (mode='..wifi.getmode()..')')
|
wifi.setmode(wifi.STATION)
|
||||||
print('MAC: ',wifi.sta.getmac())
|
print('set (mode='..wifi.getmode()..')')
|
||||||
print('chip: ',node.chipid())
|
print('MAC: ',wifi.sta.getmac())
|
||||||
print('heap: ',node.heap())
|
print('chip: ',node.chipid())
|
||||||
wifi.sta.config("Internet","")
|
print('heap: ',node.heap())
|
||||||
|
|
||||||
-- Compile server code and remove original .lua files.
|
local joincounter = 0
|
||||||
-- This only happens the first time afer the .lua files are uploaded.
|
|
||||||
|
cfg={}
|
||||||
local compileAndRemoveIfNeeded = function(f)
|
cfg.ssid="ESP-"..node.chipid()
|
||||||
if file.open(f) then
|
cfg.pwd="ESP-"..node.chipid()
|
||||||
file.close()
|
wifi.ap.config(cfg)
|
||||||
node.compile(f)
|
cfg = nil
|
||||||
file.remove(f)
|
|
||||||
end
|
-- Compile server code and remove original .lua files.
|
||||||
end
|
-- This only happens the first time afer the .lua files are uploaded.
|
||||||
|
|
||||||
local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'}
|
local compileAndRemoveIfNeeded = function(f)
|
||||||
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
if file.open(f) then
|
||||||
|
file.close()
|
||||||
compileAndRemoveIfNeeded = nil
|
node.compile(f)
|
||||||
serverFiles = nil
|
file.remove(f)
|
||||||
|
end
|
||||||
-- Connect to the WiFi access point. Once the device is connected,
|
end
|
||||||
-- you may start the HTTP server.
|
|
||||||
tmr.alarm(0, 3000, 1, function()
|
local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'}
|
||||||
if wifi.sta.getip() == nil then
|
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
||||||
print("Connecting to AP...")
|
|
||||||
else
|
compileAndRemoveIfNeeded = nil
|
||||||
tmr.stop(0)
|
serverFiles = nil
|
||||||
print('IP: ',wifi.sta.getip())
|
|
||||||
-- Uncomment to automatically start the server in port 80
|
-- Connect to the WiFi access point. Once the device is connected,
|
||||||
-- dofile("httpserver.lc")(80)
|
-- you may start the HTTP server.
|
||||||
end
|
tmr.alarm(0, 3000, 1, function()
|
||||||
end)
|
|
||||||
|
if wifi.sta.getip() == nil and joincounter < 5 then
|
||||||
|
print("Connecting to AP...")
|
||||||
|
joincounter = joincounter +1
|
||||||
|
else
|
||||||
|
tmr.stop(0)
|
||||||
|
-- print('IP: ',wifi.sta.getip())
|
||||||
|
-- Uncomment to automatically start the server in port 80
|
||||||
|
joincounter = nil
|
||||||
|
collectgarbage()
|
||||||
|
dofile("httpserver.lc")(80)
|
||||||
|
end
|
||||||
|
|
||||||
|
end)
|
||||||
|
|
||||||
|
|||||||
32
makefile
32
makefile
@ -1,32 +0,0 @@
|
|||||||
######################################################################
|
|
||||||
# User configuration
|
|
||||||
######################################################################
|
|
||||||
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
|
|
||||||
NODEMCU-UPLOADER=../nodemcu-uploader/nodemcu-uploader.py
|
|
||||||
# Serial port
|
|
||||||
PORT=/dev/cu.usbserial-A602HRAZ
|
|
||||||
|
|
||||||
######################################################################
|
|
||||||
# End of user config
|
|
||||||
######################################################################
|
|
||||||
HTTP_FILES := $(wildcard http/*)
|
|
||||||
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-error.lua
|
|
||||||
|
|
||||||
# Print usage
|
|
||||||
usage:
|
|
||||||
@echo "make upload_http to upload files to be served"
|
|
||||||
@echo "make upload_server to upload the server code and init.lua"
|
|
||||||
@echo "make upload to upload all"
|
|
||||||
|
|
||||||
# Upload HTTP files only
|
|
||||||
upload_http: $(HTTP_FILES)
|
|
||||||
@$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))
|
|
||||||
|
|
||||||
# Upload httpserver lua files (init and server module)
|
|
||||||
upload_server: $(LUA_FILES)
|
|
||||||
@$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))
|
|
||||||
|
|
||||||
# Upload all
|
|
||||||
upload: $(LUA_FILES) $(HTTP_FILES)
|
|
||||||
@$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user