AP mode support added

This commit is contained in:
Pospa 2015-08-09 15:18:08 +02:00
parent 1d3cdaf1a7
commit bbcce2d64d

174
init.lua
View File

@ -1,78 +1,96 @@
-- Begin WiFi configuration -- Begin WiFi configuration
local wifiConfig = {} local wifiConfig = {}
-- wifi.STATION -- station: join a WiFi network -- wifi.STATION -- station: join a WiFi network
-- wifi.AP -- 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.STATIONAP -- both station and access point
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
wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters wifiConfig.accessPointConfig.pwd = "ESP-"..node.chipid() -- WiFi password - at least 8 characters
wifiConfig.stationPointConfig = {} wifiConfig.accessPointIpConfig = {}
wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join wifiConfig.accessPointIpConfig.ip = "192.168.111.1"
wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network wifiConfig.accessPointIpConfig.netmask = "255.255.255.0"
wifiConfig.accessPointIpConfig.gateway = "192.168.111.1"
-- Tell the chip to connect to the access point
wifiConfig.stationPointConfig = {}
wifi.setmode(wifiConfig.mode) wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join
print('set (mode='..wifi.getmode()..')') wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network
print('MAC: ',wifi.sta.getmac())
print('chip: ',node.chipid()) -- Tell the chip to connect to the access point
print('heap: ',node.heap())
wifi.setmode(wifiConfig.mode)
wifi.ap.config(wifiConfig.accessPointConfig) print('set (mode='..wifi.getmode()..')')
wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd)
wifiConfig = nil if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
collectgarbage() print('AP MAC: ',wifi.ap.getmac())
wifi.ap.config(wifiConfig.accessPointConfig)
-- End WiFi configuration wifi.ap.setip(wifiConfig.accessPointIpConfig)
end
-- Compile server code and remove original .lua files. if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
-- This only happens the first time afer the .lua files are uploaded. print('Client MAC: ',wifi.sta.getmac())
wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd, 1)
local compileAndRemoveIfNeeded = function(f) end
if file.open(f) then
file.close() print('chip: ',node.chipid())
print('Compiling:', f) print('heap: ',node.heap())
node.compile(f)
file.remove(f) wifiConfig = nil
collectgarbage() collectgarbage()
end
end -- End WiFi configuration
local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'} -- Compile server code and remove original .lua files.
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end -- This only happens the first time afer the .lua files are uploaded.
compileAndRemoveIfNeeded = nil local compileAndRemoveIfNeeded = function(f)
serverFiles = nil if file.open(f) then
collectgarbage() file.close()
print('Compiling:', f)
-- Connect to the WiFi access point. node.compile(f)
-- Once the device is connected, you may start the HTTP server. file.remove(f)
collectgarbage()
local joinCounter = 0 end
local joinMaxAttempts = 5 end
tmr.alarm(0, 3000, 1, function()
local ip = wifi.sta.getip() local serverFiles = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'}
if ip == nil and joinCounter < joinMaxAttempts then for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
print('Connecting to WiFi Access Point ...')
joinCounter = joinCounter +1 compileAndRemoveIfNeeded = nil
else serverFiles = nil
if joinCounter == joinMaxAttempts then collectgarbage()
print('Failed to connect to WiFi Access Point.')
else -- Connect to the WiFi access point.
print('IP: ',ip) -- Once the device is connected, you may start the HTTP server.
-- Uncomment to automatically start the server in port 80
--dofile("httpserver.lc")(80) if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
end local joinCounter = 0
tmr.stop(0) local joinMaxAttempts = 5
joinCounter = nil tmr.alarm(0, 3000, 1, function()
joinMaxAttempts = nil local ip = wifi.sta.getip()
collectgarbage() if ip == nil and joinCounter < joinMaxAttempts then
end print('Connecting to WiFi Access Point ...')
joinCounter = joinCounter +1
end) 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()l) then
dofile("httpserver.lc")(80)
end