Changed wifi config to STATION, which I think would be most common. Reworked starting the server until after IP address is acquired. Added mdns conditonal mdns registration (thank you firmware people\!)
This commit is contained in:
parent
472af17b18
commit
2167d7290f
72
init.lua
72
init.lua
@ -5,7 +5,7 @@ local wifiConfig = {}
|
|||||||
-- wifi.STATION -- station: join a WiFi network
|
-- wifi.STATION -- station: join a WiFi network
|
||||||
-- wifi.SOFTAP -- access point: create a WiFi network
|
-- wifi.SOFTAP -- access point: create a WiFi network
|
||||||
-- wifi.wifi.STATIONAP -- both station and access point
|
-- wifi.wifi.STATIONAP -- both station and access point
|
||||||
wifiConfig.mode = wifi.STATIONAP -- both station and access point
|
wifiConfig.mode = wifi.STATION
|
||||||
|
|
||||||
wifiConfig.accessPointConfig = {}
|
wifiConfig.accessPointConfig = {}
|
||||||
wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create
|
wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create
|
||||||
@ -23,7 +23,7 @@ wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi
|
|||||||
-- Tell the chip to connect to the access point
|
-- Tell the chip to connect to the access point
|
||||||
|
|
||||||
wifi.setmode(wifiConfig.mode)
|
wifi.setmode(wifiConfig.mode)
|
||||||
print('set (mode='..wifi.getmode()..')')
|
--print('set (mode='..wifi.getmode()..')')
|
||||||
|
|
||||||
if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
|
if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
|
||||||
print('AP MAC: ',wifi.ap.getmac())
|
print('AP MAC: ',wifi.ap.getmac())
|
||||||
@ -44,7 +44,7 @@ collectgarbage()
|
|||||||
-- End WiFi configuration
|
-- End WiFi configuration
|
||||||
|
|
||||||
-- Compile server code and remove original .lua files.
|
-- Compile server code and remove original .lua files.
|
||||||
-- This only happens the first time afer the .lua files are uploaded.
|
-- This only happens the first time after server .lua files are uploaded.
|
||||||
|
|
||||||
local compileAndRemoveIfNeeded = function(f)
|
local compileAndRemoveIfNeeded = function(f)
|
||||||
if file.open(f) then
|
if file.open(f) then
|
||||||
@ -74,32 +74,48 @@ serverFiles = nil
|
|||||||
collectgarbage()
|
collectgarbage()
|
||||||
|
|
||||||
-- Connect to the WiFi access point.
|
-- Connect to the WiFi access point.
|
||||||
-- Once the device is connected, you may start the HTTP server.
|
-- Once the device is connected, start the HTTP server.
|
||||||
|
|
||||||
|
startServer = function(ip, hostname)
|
||||||
|
local serverPort = 80
|
||||||
|
if (dofile("httpserver.lc")(serverPort)) then
|
||||||
|
print("nodemcu-httpserver running at:")
|
||||||
|
print(" http://" .. ip .. ":" .. serverPort)
|
||||||
|
if (mdns) then
|
||||||
|
mdns.register(hostname, { description="A tiny server", service="http", port=serverPort, location='Earth' })
|
||||||
|
print (' http://' .. hostname .. '.local.:' .. serverPort)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
|
if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
|
||||||
local joinCounter = 0
|
local joinCounter = 0
|
||||||
local joinMaxAttempts = 5
|
local joinMaxAttempts = 5
|
||||||
tmr.alarm(0, 3000, 1, function()
|
tmr.alarm(0, 3000, 1, function()
|
||||||
local ip = wifi.sta.getip()
|
local ip = wifi.sta.getip()
|
||||||
if ip == nil and joinCounter < joinMaxAttempts then
|
if (not ip) then ip = wifi.ap.getip() end
|
||||||
print('Connecting to WiFi Access Point ...')
|
if ip == nil and joinCounter < joinMaxAttempts then
|
||||||
joinCounter = joinCounter +1
|
print('Connecting to WiFi Access Point ...')
|
||||||
else
|
joinCounter = joinCounter + 1
|
||||||
if joinCounter == joinMaxAttempts then
|
else
|
||||||
print('Failed to connect to WiFi Access Point.')
|
print("IP = " .. ip)
|
||||||
else
|
if joinCounter == joinMaxAttempts then
|
||||||
print('IP: ',ip)
|
print('Failed to connect to WiFi Access Point.')
|
||||||
end
|
else
|
||||||
tmr.stop(0)
|
if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
|
||||||
joinCounter = nil
|
-- Second parameter is for mDNS (aka Zeroconf aka Bonjour) registration.
|
||||||
joinMaxAttempts = nil
|
-- If the mdns module is compiled in the firmware, advertise the server with this name.
|
||||||
collectgarbage()
|
-- If no mdns is compiled, then parameter is ignored.
|
||||||
end
|
startServer(ip, "nodemcu")
|
||||||
end)
|
end
|
||||||
end
|
end
|
||||||
|
tmr.stop(0)
|
||||||
-- Uncomment to automatically start the server in port 80
|
joinCounter = nil
|
||||||
if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
|
joinMaxAttempts = nil
|
||||||
--dofile("httpserver.lc")(80)
|
collectgarbage()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
startServer()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user