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:
Marcos Kirsch 2016-12-26 13:51:40 -06:00
parent 472af17b18
commit 2167d7290f

View File

@ -5,7 +5,7 @@ local wifiConfig = {}
-- wifi.STATION -- station: join a WiFi network
-- wifi.SOFTAP -- access point: create a WiFi network
-- 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.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
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
print('AP MAC: ',wifi.ap.getmac())
@ -44,7 +44,7 @@ collectgarbage()
-- End WiFi configuration
-- 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)
if file.open(f) then
@ -74,21 +74,40 @@ serverFiles = nil
collectgarbage()
-- 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
local joinCounter = 0
local joinMaxAttempts = 5
tmr.alarm(0, 3000, 1, function()
local ip = wifi.sta.getip()
if (not ip) then ip = wifi.ap.getip() end
if ip == nil and joinCounter < joinMaxAttempts then
print('Connecting to WiFi Access Point ...')
joinCounter = joinCounter +1
joinCounter = joinCounter + 1
else
print("IP = " .. ip)
if joinCounter == joinMaxAttempts then
print('Failed to connect to WiFi Access Point.')
else
print('IP: ',ip)
if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
-- Second parameter is for mDNS (aka Zeroconf aka Bonjour) registration.
-- If the mdns module is compiled in the firmware, advertise the server with this name.
-- If no mdns is compiled, then parameter is ignored.
startServer(ip, "nodemcu")
end
end
tmr.stop(0)
joinCounter = nil
@ -96,10 +115,7 @@ if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
collectgarbage()
end
end)
end
-- Uncomment to automatically start the server in port 80
if (not not wifi.sta.getip()) or (not not wifi.ap.getip()) then
--dofile("httpserver.lc")(80)
else
startServer()
end