From bbcce2d64dac0580d31255d313c0e60c13fc9d48 Mon Sep 17 00:00:00 2001 From: Pospa Date: Sun, 9 Aug 2015 15:18:08 +0200 Subject: [PATCH] AP mode support added --- init.lua | 174 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 96 insertions(+), 78 deletions(-) diff --git a/init.lua b/init.lua index 45aaeab..9456096 100644 --- a/init.lua +++ b/init.lua @@ -1,78 +1,96 @@ --- 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 - -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('MAC: ',wifi.sta.getmac()) -print('chip: ',node.chipid()) -print('heap: ',node.heap()) - -wifi.ap.config(wifiConfig.accessPointConfig) -wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd) -wifiConfig = nil -collectgarbage() - --- 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 = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'} -for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end - -compileAndRemoveIfNeeded = nil -serverFiles = nil -collectgarbage() - --- Connect to the WiFi access point. --- Once the device is connected, you may start the HTTP server. - -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) - -- Uncomment to automatically start the server in port 80 - --dofile("httpserver.lc")(80) - end - tmr.stop(0) - joinCounter = nil - joinMaxAttempts = nil - collectgarbage() - end - -end) - +-- Begin WiFi configuration + +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.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.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.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()..')') + +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('heap: ',node.heap()) + +wifiConfig = nil +collectgarbage() + +-- 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 = {'httpserver.lua', 'httpserver-basicauth.lua', 'httpserver-conf.lua', 'httpserver-b64decode.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-header.lua', 'httpserver-error.lua'} +for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end + +compileAndRemoveIfNeeded = nil +serverFiles = nil +collectgarbage() + +-- Connect to the WiFi access point. +-- Once the device is connected, you may start the HTTP server. + +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()l) then + dofile("httpserver.lc")(80) +end + +