rgbmatrixscreen/nodemcu/init.lua
2016-05-01 16:44:30 -07:00

91 lines
3.1 KiB
Lua

-- Begin WiFi configuration
local wifiConfig = {}
-- wifi.STATION -- station: join a WiFi network
-- wifi.AP -- access point: create a WiFi network
-- wifi.wifi.STATIONAP -- both station and access point
wifiConfig.mode = wifi.STATIONAP -- both station and access point
wifiConfig.accessPointConfig = {}
wifiConfig.accessPointConfig.ssid = "ESP-"..node.chipid() -- Name of the SSID you want to create
wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters
-- default connection setup, just in case we don't have one saved.
wifiConfig.stationPointConfig = {}
wifiConfig.stationPointConfig.ssid = "Scalar24" -- Name of the WiFi network you want to join
wifiConfig.stationPointConfig.pwd = "Fb274Gh@12G1" -- Password for the WiFi network
wifiConfig.mDNS = {}
wifiConfig.mDNS.hostname = "rvoots-nameplate" -- default hostname
wifiConfig.mDNS.description = "Ryan Voot's name plate at his desk" -- what avahi-browse will describe this as
-- Tell the chip to connect to the access point
wifi.setmode(wifiConfig.mode)
print('set (mode='..wifi.getmode()..')')
print('MAC: ',wifi.sta.getmac())
print('chip: ',node.chipid())
print('heap: ',node.heap())
-- End WiFi configuration
-- Compile server code and remove original .lua files.
-- This only happens the first time afer the .lua files are uploaded.
local compileAndRemoveIfNeeded = function(f)
if file.open(f) then
file.close()
print('Compiling:', f)
node.compile(f)
file.remove(f)
collectgarbage()
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'}
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
compileAndRemoveIfNeeded = nil
serverFiles = nil
-- Check for a wifi config file
if file.open("wificonfig.lc") then
file.close()
print('Found config.')
wifiConfig.stationPointConfig = dofile("wificonfig.lc")
end
-- Start the APs once we hit here
wifi.ap.config(wifiConfig.accessPointConfig)
wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd)
mdns.register(wifiConfig.mDNS.hostname, wifiConfig.mDNS)
wifiConfig = nil
collectgarbage()
-- Connect to the WiFi access point.
-- We run an AP, and connect to an AP. We need to start the servers early.
dofile("telnet.lc")(23)
dofile("httpserver.lc")(80)
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)