AP mode support added

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

View File

@ -3,7 +3,7 @@
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
@ -11,6 +11,11 @@ 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.accessPointIpConfig = {}
wifiConfig.accessPointIpConfig.ip = "192.168.111.1"
wifiConfig.accessPointIpConfig.netmask = "255.255.255.0"
wifiConfig.accessPointIpConfig.gateway = "192.168.111.1"
wifiConfig.stationPointConfig = {} wifiConfig.stationPointConfig = {}
wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join wifiConfig.stationPointConfig.ssid = "Internet" -- Name of the WiFi network you want to join
wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi network
@ -19,12 +24,20 @@ wifiConfig.stationPointConfig.pwd = "" -- Password for the WiFi
wifi.setmode(wifiConfig.mode) wifi.setmode(wifiConfig.mode)
print('set (mode='..wifi.getmode()..')') print('set (mode='..wifi.getmode()..')')
print('MAC: ',wifi.sta.getmac())
if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
print('AP MAC: ',wifi.ap.getmac())
wifi.ap.config(wifiConfig.accessPointConfig)
wifi.ap.setip(wifiConfig.accessPointIpConfig)
end
if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
print('Client MAC: ',wifi.sta.getmac())
wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd, 1)
end
print('chip: ',node.chipid()) print('chip: ',node.chipid())
print('heap: ',node.heap()) print('heap: ',node.heap())
wifi.ap.config(wifiConfig.accessPointConfig)
wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd)
wifiConfig = nil wifiConfig = nil
collectgarbage() collectgarbage()
@ -53,26 +66,31 @@ collectgarbage()
-- Connect to the WiFi access point. -- Connect to the WiFi access point.
-- Once the device is connected, you may start the HTTP server. -- Once the device is connected, you may start the HTTP server.
local joinCounter = 0 if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
local joinMaxAttempts = 5 local joinCounter = 0
tmr.alarm(0, 3000, 1, function() local joinMaxAttempts = 5
local ip = wifi.sta.getip() tmr.alarm(0, 3000, 1, function()
if ip == nil and joinCounter < joinMaxAttempts then local ip = wifi.sta.getip()
print('Connecting to WiFi Access Point ...') if ip == nil and joinCounter < joinMaxAttempts then
joinCounter = joinCounter +1 print('Connecting to WiFi Access Point ...')
else joinCounter = joinCounter +1
if joinCounter == joinMaxAttempts then else
print('Failed to connect to WiFi Access Point.') if joinCounter == joinMaxAttempts then
else print('Failed to connect to WiFi Access Point.')
print('IP: ',ip) else
-- Uncomment to automatically start the server in port 80 print('IP: ',ip)
--dofile("httpserver.lc")(80) end
end tmr.stop(0)
tmr.stop(0) joinCounter = nil
joinCounter = nil joinMaxAttempts = nil
joinMaxAttempts = nil collectgarbage()
collectgarbage() end
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
end)