Working on getting lua http scripts working
This commit is contained in:
parent
d06d7fb29e
commit
4ec2ae7574
9 changed files with 157 additions and 104 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -7,3 +7,6 @@
|
||||||
[submodule "extern/nodemcu-uploader"]
|
[submodule "extern/nodemcu-uploader"]
|
||||||
path = extern/nodemcu-uploader
|
path = extern/nodemcu-uploader
|
||||||
url = https://github.com/kmpm/nodemcu-uploader
|
url = https://github.com/kmpm/nodemcu-uploader
|
||||||
|
[submodule "extern/nodemcu-httpserver"]
|
||||||
|
path = extern/nodemcu-httpserver
|
||||||
|
url = https://github.com/marcoskirsch/nodemcu-httpserver
|
||||||
|
|
1
extern/nodemcu-httpserver
vendored
Submodule
1
extern/nodemcu-httpserver
vendored
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit b84739dc1bee35800f933a78ebd56492503b29ae
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
basicAuth = {}
|
basicAuth = {}
|
||||||
|
|
||||||
function basicAuth.authenticate(header)
|
|
||||||
conf = dofile("httpserver-conf.lc")
|
|
||||||
-- Parse basic auth http header.
|
-- Parse basic auth http header.
|
||||||
-- Returns the username if header contains valid credentials,
|
-- Returns the username if header contains valid credentials,
|
||||||
-- nil otherwise.
|
-- nil otherwise.
|
||||||
|
function basicAuth.authenticate(header)
|
||||||
|
local conf = dofile("httpserver-conf.lc")
|
||||||
local credentials_enc = header:match("Authorization: Basic ([A-Za-z0-9+/=]+)")
|
local credentials_enc = header:match("Authorization: Basic ([A-Za-z0-9+/=]+)")
|
||||||
if not credentials_enc then
|
if not credentials_enc then
|
||||||
return nil
|
return nil
|
||||||
|
@ -16,13 +16,15 @@ function basicAuth.authenticate(header)
|
||||||
local credentials = dofile("httpserver-b64decode.lc")(credentials_enc)
|
local credentials = dofile("httpserver-b64decode.lc")(credentials_enc)
|
||||||
local user, pwd = credentials:match("^(.*):(.*)$")
|
local user, pwd = credentials:match("^(.*):(.*)$")
|
||||||
if user ~= conf.auth.user or pwd ~= conf.auth.password then
|
if user ~= conf.auth.user or pwd ~= conf.auth.password then
|
||||||
|
print("httpserver-basicauth: User \"" .. user .. "\": Access denied.")
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
print("httpserver-basicauth: User \"" .. user .. "\" authenticated.")
|
print("httpserver-basicauth: User \"" .. user .. "\": Authenticated.")
|
||||||
return user
|
return user
|
||||||
end
|
end
|
||||||
|
|
||||||
function basicAuth.authErrorHeader()
|
function basicAuth.authErrorHeader()
|
||||||
|
local conf = dofile("httpserver-conf.lc")
|
||||||
return "WWW-Authenticate: Basic realm=\"" .. conf.auth.realm .. "\""
|
return "WWW-Authenticate: Basic realm=\"" .. conf.auth.realm .. "\""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,16 +4,19 @@
|
||||||
|
|
||||||
return function (connection, req, args)
|
return function (connection, req, args)
|
||||||
|
|
||||||
local function sendHeader(connection, code, errorString, extraHeaders, mimeType)
|
-- @TODO: would be nice to use httpserver-header.lua
|
||||||
connection:send("HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
|
local function getHeader(connection, code, errorString, extraHeaders, mimeType)
|
||||||
for i, header in ipairs(extraHeaders) do
|
local header = "HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n"
|
||||||
connection:send(header .. "\r\n")
|
for i, extraHeader in ipairs(extraHeaders) do
|
||||||
|
header = header .. extraHeader .. "\r\n"
|
||||||
end
|
end
|
||||||
connection:send("connection: close\r\n\r\n")
|
header = header .. "connection: close\r\n\r\n"
|
||||||
|
return header
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Error " .. args.code .. ": " .. args.errorString)
|
print("Error " .. args.code .. ": " .. args.errorString)
|
||||||
args.headers = args.headers or {}
|
args.headers = args.headers or {}
|
||||||
sendHeader(connection, args.code, args.errorString, args.headers, "text/html")
|
connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html"))
|
||||||
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
|
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
return function (connection, code, extension)
|
return function (connection, code, extension, isGzipped)
|
||||||
|
|
||||||
local function getHTTPStatusString(code)
|
local function getHTTPStatusString(code)
|
||||||
local codez = {[200]="OK", [400]="Bad Request", [404]="Not Found",}
|
local codez = {[200]="OK", [400]="Bad Request", [404]="Not Found",}
|
||||||
|
@ -12,24 +12,18 @@ return function (connection, code, extension)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getMimeType(ext)
|
local function getMimeType(ext)
|
||||||
local gzip = false
|
|
||||||
-- 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", json = "application/json", png = "image/png", xml = "text/xml"}
|
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", xml = "text/xml"}
|
||||||
-- add comressed flag if file ends with gz
|
if mt[ext] then return mt[ext] else return "text/plain" end
|
||||||
if ext:find("%.gz$") then
|
|
||||||
ext = ext:sub(1, -4)
|
|
||||||
gzip = true
|
|
||||||
end
|
|
||||||
if mt[ext] then contentType = mt[ext] else contentType = "text/plain" end
|
|
||||||
return {contentType = contentType, gzip = gzip}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local mimeType = getMimeType(extension)
|
local mimeType = getMimeType(extension)
|
||||||
|
|
||||||
connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType["contentType"] .. "\r\n")
|
connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nnCache-Control: private, no-store\r\n")
|
||||||
if mimeType["gzip"] then
|
if isGzipped then
|
||||||
connection:send("Content-Encoding: gzip\r\n")
|
connection:send("Cache-Control: max-age=2592000\r\nContent-Encoding: gzip\r\n")
|
||||||
end
|
end
|
||||||
connection:send("Connection: close\r\n\r\n")
|
connection:send("Connection: close\r\n\r\n")
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -33,41 +33,39 @@ end
|
||||||
|
|
||||||
local function parseFormData(body)
|
local function parseFormData(body)
|
||||||
local data = {}
|
local data = {}
|
||||||
print("Parsing Form Data")
|
--print("Parsing Form Data")
|
||||||
for kv in body.gmatch(body, "%s*&?([^=]+=[^&]+)") do
|
for kv in body.gmatch(body, "%s*&?([^=]+=[^&]+)") do
|
||||||
local key, value = string.match(kv, "(.*)=(.*)")
|
local key, value = string.match(kv, "(.*)=(.*)")
|
||||||
|
--print("Parsed: " .. key .. " => " .. value)
|
||||||
print("Parsed: " .. key .. " => " .. value)
|
|
||||||
data[key] = uri_decode(value)
|
data[key] = uri_decode(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getRequestData(payload)
|
local function getRequestData(payload)
|
||||||
local requestData
|
local requestData
|
||||||
return function ()
|
return function ()
|
||||||
print("Getting Request Data")
|
--print("Getting Request Data")
|
||||||
if requestData then
|
if requestData then
|
||||||
return requestData
|
return requestData
|
||||||
else
|
else
|
||||||
local mimeType = string.match(payload, "Content%-Type: (%S+)\r\n")
|
--print("payload = [" .. payload .. "]")
|
||||||
local body_start = payload:find("\r\n\r\n", 1, true)
|
local mimeType = string.match(payload, "Content%-Type: ([%w/-]+)")
|
||||||
local body = payload:sub(body_start, #payload)
|
local bodyStart = payload:find("\r\n\r\n", 1, true)
|
||||||
|
local body = payload:sub(bodyStart, #payload)
|
||||||
payload = nil
|
payload = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|
||||||
--print("mimeType = [" .. mimeType .. "]")
|
--print("mimeType = [" .. mimeType .. "]")
|
||||||
|
--print("bodyStart = [" .. bodyStart .. "]")
|
||||||
|
--print("body = [" .. body .. "]")
|
||||||
if mimeType == "application/json" then
|
if mimeType == "application/json" then
|
||||||
print("JSON: " .. body)
|
--print("JSON: " .. body)
|
||||||
requestData = cjson.decode(body)
|
requestData = cjson.decode(body)
|
||||||
elseif mimeType == "application/x-www-form-urlencoded" then
|
elseif mimeType == "application/x-www-form-urlencoded" then
|
||||||
requestData = parseFormData(body)
|
requestData = parseFormData(body)
|
||||||
else
|
else
|
||||||
requestData = {}
|
requestData = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
return requestData
|
return requestData
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -94,7 +92,12 @@ local function parseUri(uri)
|
||||||
filename,ext = filename:match("(.+)%.(.+)")
|
filename,ext = filename:match("(.+)%.(.+)")
|
||||||
table.insert(fullExt,1,ext)
|
table.insert(fullExt,1,ext)
|
||||||
end
|
end
|
||||||
r.ext = table.concat(fullExt,".")
|
if #fullExt > 1 and fullExt[#fullExt] == 'gz' then
|
||||||
|
r.ext = fullExt[#fullExt-1]
|
||||||
|
r.isGzipped = true
|
||||||
|
elseif #fullExt >= 1 then
|
||||||
|
r.ext = fullExt[#fullExt]
|
||||||
|
end
|
||||||
r.isScript = r.ext == "lua" or r.ext == "lc"
|
r.isScript = r.ext == "lua" or r.ext == "lc"
|
||||||
r.file = uriToFilename(r.file)
|
r.file = uriToFilename(r.file)
|
||||||
return r
|
return r
|
||||||
|
@ -103,6 +106,7 @@ end
|
||||||
-- Parses the client's request. Returns a dictionary containing pretty much everything
|
-- Parses the client's request. Returns a dictionary containing pretty much everything
|
||||||
-- the server needs to know about the uri.
|
-- the server needs to know about the uri.
|
||||||
return function (request)
|
return function (request)
|
||||||
|
--print("Request: \n", request)
|
||||||
local e = request:find("\r\n", 1, true)
|
local e = request:find("\r\n", 1, true)
|
||||||
if not e then return nil end
|
if not e then return nil end
|
||||||
local line = request:sub(1, e - 1)
|
local line = request:sub(1, e - 1)
|
||||||
|
|
|
@ -3,29 +3,32 @@
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
return function (connection, req, args)
|
return function (connection, req, args)
|
||||||
dofile("httpserver-header.lc")(connection, 200, args.ext)
|
|
||||||
--print("Begin sending:", args.file)
|
--print("Begin sending:", args.file)
|
||||||
|
--print("node.heap(): ", node.heap())
|
||||||
|
dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped)
|
||||||
-- Send file in little chunks
|
-- Send file in little chunks
|
||||||
local continue = true
|
local continue = true
|
||||||
|
local size = file.list()[args.file]
|
||||||
local bytesSent = 0
|
local bytesSent = 0
|
||||||
|
-- Chunks larger than 1024 don't work.
|
||||||
|
-- https://github.com/nodemcu/nodemcu-firmware/issues/1075
|
||||||
|
local chunkSize = 1024
|
||||||
while continue do
|
while continue do
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|
||||||
-- NodeMCU file API lets you open 1 file at a time.
|
-- NodeMCU file API lets you open 1 file at a time.
|
||||||
-- So we need to open, seek, close each time in order
|
-- So we need to open, seek, close each time in order
|
||||||
-- to support multiple simultaneous clients.
|
-- to support multiple simultaneous clients.
|
||||||
file.open(args.file)
|
file.open(args.file)
|
||||||
file.seek("set", bytesSent)
|
file.seek("set", bytesSent)
|
||||||
local chunk = file.read(256)
|
local chunk = file.read(chunkSize)
|
||||||
file.close()
|
file.close()
|
||||||
if chunk == nil then
|
|
||||||
continue = false
|
|
||||||
else
|
|
||||||
coroutine.yield()
|
|
||||||
connection:send(chunk)
|
connection:send(chunk)
|
||||||
bytesSent = bytesSent + #chunk
|
bytesSent = bytesSent + #chunk
|
||||||
chunk = nil
|
chunk = nil
|
||||||
--print("Sent" .. args.file, bytesSent)
|
--print("Sent: " .. bytesSent .. " of " .. size)
|
||||||
end
|
if bytesSent == size then continue = false end
|
||||||
end
|
end
|
||||||
--print("Finished sending: ", args.file)
|
--print("Finished sending: ", args.file)
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,21 +9,37 @@ return function (port)
|
||||||
port,
|
port,
|
||||||
function (connection)
|
function (connection)
|
||||||
|
|
||||||
-- This variable holds the thread used for sending data back to the user.
|
-- This variable holds the thread (actually a Lua coroutine) 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 send in little chunks and wait for the onSent event
|
||||||
-- of data in order to avoid overflowing the mcu's buffer.
|
-- before we can send more, or we risk overflowing the mcu's buffer.
|
||||||
local connectionThread
|
local connectionThread
|
||||||
|
|
||||||
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
|
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
|
||||||
|
|
||||||
local function onRequest(connection, req)
|
local function startServing(fileServeFunction, connection, req, args)
|
||||||
|
connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args)
|
||||||
|
fileServeFunction(bufferedConnection, req, args)
|
||||||
|
-- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing.
|
||||||
|
if not bufferedConnection:flush() then
|
||||||
|
connection:close()
|
||||||
|
connectionThread = nil
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
local BufferedConnectionClass = dofile("httpserver-connection.lc")
|
||||||
|
local bufferedConnection = BufferedConnectionClass:new(connection)
|
||||||
|
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
|
||||||
|
if not status then
|
||||||
|
print("Error: ", err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function handleRequest(connection, req)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
local method = req.method
|
local method = req.method
|
||||||
local uri = req.uri
|
local uri = req.uri
|
||||||
local fileServeFunction = nil
|
local fileServeFunction = nil
|
||||||
|
|
||||||
print("Method: " .. method);
|
|
||||||
|
|
||||||
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, errorString = "Bad Request"}
|
uri.args = {code = 400, errorString = "Bad Request"}
|
||||||
|
@ -38,9 +54,9 @@ return function (port)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
if fileExists then
|
if fileExists then
|
||||||
print("gzip variant exists, serving that one")
|
--print("gzip variant exists, serving that one")
|
||||||
uri.file = uri.file .. ".gz"
|
uri.file = uri.file .. ".gz"
|
||||||
uri.ext = uri.ext .. ".gz"
|
uri.isGzipped = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -51,7 +67,7 @@ return function (port)
|
||||||
fileServeFunction = dofile(uri.file)
|
fileServeFunction = dofile(uri.file)
|
||||||
else
|
else
|
||||||
if allowStatic[method] then
|
if allowStatic[method] then
|
||||||
uri.args = {file = uri.file, ext = uri.ext}
|
uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped}
|
||||||
fileServeFunction = dofile("httpserver-static.lc")
|
fileServeFunction = dofile("httpserver-static.lc")
|
||||||
else
|
else
|
||||||
uri.args = {code = 405, errorString = "Method not supported"}
|
uri.args = {code = 405, errorString = "Method not supported"}
|
||||||
|
@ -59,8 +75,7 @@ return function (port)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
connectionThread = coroutine.create(fileServeFunction)
|
startServing(fileServeFunction, connection, req, uri.args)
|
||||||
coroutine.resume(connectionThread, connection, req, uri.args)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onReceive(connection, payload)
|
local function onReceive(connection, payload)
|
||||||
|
@ -69,16 +84,32 @@ return function (port)
|
||||||
local auth
|
local auth
|
||||||
local user = "Anonymous"
|
local user = "Anonymous"
|
||||||
|
|
||||||
|
-- as suggest by anyn99 (https://github.com/marcoskirsch/nodemcu-httpserver/issues/36#issuecomment-167442461)
|
||||||
|
-- Some browsers send the POST data in multiple chunks.
|
||||||
|
-- Collect data packets until the size of HTTP body meets the Content-Length stated in header
|
||||||
|
if payload:find("Content%-Length:") or bBodyMissing then
|
||||||
|
if fullPayload then fullPayload = fullPayload .. payload else fullPayload = payload end
|
||||||
|
if (tonumber(string.match(fullPayload, "%d+", fullPayload:find("Content%-Length:")+16)) > #fullPayload:sub(fullPayload:find("\r\n\r\n", 1, true)+4, #fullPayload)) then
|
||||||
|
bBodyMissing = true
|
||||||
|
return
|
||||||
|
else
|
||||||
|
--print("HTTP packet assembled! size: "..#fullPayload)
|
||||||
|
payload = fullPayload
|
||||||
|
fullPayload, bBodyMissing = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
collectgarbage()
|
||||||
|
|
||||||
-- 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(req.method .. ": " .. req.request)
|
||||||
if conf.auth.enabled then
|
if conf.auth.enabled then
|
||||||
auth = dofile("httpserver-basicauth.lc")
|
auth = dofile("httpserver-basicauth.lc")
|
||||||
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
|
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
|
||||||
end
|
end
|
||||||
|
|
||||||
if user and req.methodIsValid and (req.method == "GET" or req.method == "POST" or req.method == "PUT") then
|
if user and req.methodIsValid and (req.method == "GET" or req.method == "POST" or req.method == "PUT") then
|
||||||
onRequest(connection, req)
|
handleRequest(connection, req)
|
||||||
else
|
else
|
||||||
local args = {}
|
local args = {}
|
||||||
local fileServeFunction = dofile("httpserver-error.lc")
|
local fileServeFunction = dofile("httpserver-error.lc")
|
||||||
|
@ -89,8 +120,7 @@ return function (port)
|
||||||
else
|
else
|
||||||
args = {code = 400, errorString = "Bad Request"}
|
args = {code = 400, errorString = "Bad Request"}
|
||||||
end
|
end
|
||||||
connectionThread = coroutine.create(fileServeFunction)
|
startServing(fileServeFunction, connection, req, args)
|
||||||
coroutine.resume(connectionThread, connection, req, args)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -100,7 +130,10 @@ return function (port)
|
||||||
local connectionThreadStatus = coroutine.status(connectionThread)
|
local connectionThreadStatus = coroutine.status(connectionThread)
|
||||||
if connectionThreadStatus == "suspended" then
|
if connectionThreadStatus == "suspended" then
|
||||||
-- Not finished sending file, resume.
|
-- Not finished sending file, resume.
|
||||||
coroutine.resume(connectionThread)
|
local status, err = coroutine.resume(connectionThread)
|
||||||
|
if not status then
|
||||||
|
print(err)
|
||||||
|
end
|
||||||
elseif connectionThreadStatus == "dead" then
|
elseif connectionThreadStatus == "dead" then
|
||||||
-- We're done sending file.
|
-- We're done sending file.
|
||||||
connection:close()
|
connection:close()
|
||||||
|
@ -109,14 +142,23 @@ return function (port)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function onDisconnect(connection, payload)
|
||||||
|
if connectionThread then
|
||||||
|
connectionThread = nil
|
||||||
|
collectgarbage()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
connection:on("receive", onReceive)
|
connection:on("receive", onReceive)
|
||||||
connection:on("sent", onSent)
|
connection:on("sent", onSent)
|
||||||
|
connection:on("disconnection", onDisconnect)
|
||||||
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
-- false and nil evaluate as false
|
-- false and nil evaluate as false
|
||||||
local ip = wifi.sta.getip()
|
local ip = wifi.sta.getip()
|
||||||
if not ip then ip = wifi.ap.getip() end
|
if not ip then ip = wifi.ap.getip() end
|
||||||
|
if not ip then ip = "unknown IP" end
|
||||||
print("nodemcu-httpserver running at http://" .. ip .. ":" .. port)
|
print("nodemcu-httpserver running at http://" .. ip .. ":" .. port)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
|
@ -39,22 +39,23 @@ local compileAndRemoveIfNeeded = function(f)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local serverFiles = {'telnet.lua', 'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua', 'wificonfig.lua'}
|
local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
|
||||||
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
||||||
|
|
||||||
compileAndRemoveIfNeeded = nil
|
compileAndRemoveIfNeeded = nil
|
||||||
serverFiles = nil
|
serverFiles = nil
|
||||||
|
|
||||||
-- Check for a wifi config file
|
-- Check for a wifi config file
|
||||||
if file.open("wificonfig.lc") then
|
if file.open("wificonfig.lua") then
|
||||||
file.close()
|
file.close()
|
||||||
print('Found config.')
|
print('Found config.')
|
||||||
wifiConfig.stationPointConfig = dofile("wificonfig.lc")
|
wifiConfig.stationPointConfig = dofile("wificonfig.lua")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Start the APs once we hit here
|
-- Start the APs once we hit here
|
||||||
wifi.ap.config(wifiConfig.accessPointConfig)
|
wifi.ap.config(wifiConfig.accessPointConfig)
|
||||||
wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd)
|
wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd)
|
||||||
|
wifi.ap.dhcp.start()
|
||||||
wifiConfig = nil
|
wifiConfig = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|
||||||
|
@ -73,9 +74,9 @@ tmr.alarm(0, 3000, 1, function()
|
||||||
else
|
else
|
||||||
if joinCounter == joinMaxAttempts then
|
if joinCounter == joinMaxAttempts then
|
||||||
print('Failed to connect to WiFi Access Point.')
|
print('Failed to connect to WiFi Access Point.')
|
||||||
mdns.register("rvoots-nameplate", {})
|
|
||||||
else
|
else
|
||||||
print('IP: ',ip)
|
print('IP: ',ip)
|
||||||
|
mdns.register("rvoots-nameplate")
|
||||||
end
|
end
|
||||||
tmr.stop(0)
|
tmr.stop(0)
|
||||||
joinCounter = nil
|
joinCounter = nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue