diff --git a/init.lua b/init.lua index fe654a8..d7c5d76 100644 --- a/init.lua +++ b/init.lua @@ -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,32 +74,48 @@ 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 ip == nil and joinCounter < joinMaxAttempts then - print('Connecting to WiFi Access Point ...') - joinCounter = joinCounter +1 - else - if joinCounter == joinMaxAttempts then - print('Failed to connect to WiFi Access Point.') - else - print('IP: ',ip) - end - tmr.stop(0) - joinCounter = nil - joinMaxAttempts = nil - 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) + 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 + else + print("IP = " .. ip) + if joinCounter == joinMaxAttempts then + print('Failed to connect to WiFi Access Point.') + else + 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 + joinMaxAttempts = nil + collectgarbage() + end + end) +else + startServer() end