Improved for easier configuration, better messages, and slightly improved memory handling

This commit is contained in:
Marcos Kirsch 2015-04-18 23:21:01 -05:00
parent 7bd9b266be
commit 99be566549

View File

@ -1,17 +1,34 @@
-- Tel--l the chip to connect to the access point -- Begin WiFi configuration
--wifi.setmode(wifi.STATIONAP)
wifi.setmode(wifi.STATION) 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
wifiConfig.stationPointConfig = {}
wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join
wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network
-- Tell the chip to connect to the access point
wifi.setmode(wifiConfig.mode)
print('set (mode='..wifi.getmode()..')') print('set (mode='..wifi.getmode()..')')
print('MAC: ',wifi.sta.getmac()) print('MAC: ',wifi.sta.getmac())
print('chip: ',node.chipid()) print('chip: ',node.chipid())
print('heap: ',node.heap()) print('heap: ',node.heap())
wifi.ap.config(wifiConfig.accessPointConfig)
local cfg={} wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd)
cfg.ssid="ESP-"..node.chipid() wifiConfig = nil
cfg.pwd="ESP-"..node.chipid() collectgarbage()
wifi.ap.config(cfg)
cfg = nil -- 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 afer the .lua files are uploaded.
@ -19,9 +36,10 @@ cfg = nil
local compileAndRemoveIfNeeded = function(f) local compileAndRemoveIfNeeded = function(f)
if file.open(f) then if file.open(f) then
file.close() file.close()
print(f) print('Compiling:', f)
node.compile(f) node.compile(f)
file.remove(f) file.remove(f)
collectgarbage()
end end
end end
@ -30,24 +48,29 @@ for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
compileAndRemoveIfNeeded = nil compileAndRemoveIfNeeded = nil
serverFiles = nil serverFiles = nil
collectgarbage()
-- Connect to the WiFi access point. Once the device is connected, -- Connect to the WiFi access point.
-- you may start the HTTP server. -- Once the device is connected, you may start the HTTP server.
local joincounter = 0 local joinCounter = 0
local joinMaxAttempts = 5
tmr.alarm(0, 3000, 1, function() tmr.alarm(0, 3000, 1, function()
if wifi.sta.getip() == nil and joinCounter < joinMaxAttempts then
if wifi.sta.getip() == nil and joincounter < 5 then print('Connecting to WiFi Access Point ...')
print("Connecting to AP...") joinCounter = joinCounter +1
joincounter = joincounter +1 else
else if joinCounter == joinMaxAttempts then
print('Faild to connect to WiFi Access Point.')
else
print('IP: ',wifi.sta.getip())
-- Uncomment to automatically start the server in port 80
--dofile("httpserver.lc")(80)
end
tmr.stop(0) tmr.stop(0)
-- print('IP: ',wifi.sta.getip()) joinCounter = nil
-- Uncomment to automatically start the server in port 80 joinMaxAttempts = nil
joincounter = nil
collectgarbage() collectgarbage()
dofile("httpserver.lc")(80)
end end
end) end)