Skip to content

Commit

Permalink
require('wifi_init') to dofile('wifi_connect')
Browse files Browse the repository at this point in the history
  • Loading branch information
avaldebe committed Nov 16, 2015
1 parent 44dfaac commit 6466c00
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 102 deletions.
4 changes: 1 addition & 3 deletions lua_modules/AQmon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ status('normal')
-- low heap(?) alternative: local status=print

print('Start WiFi')
require('wifi_init').connect(wifi.STATION,wifi.MODEM_SLEEP)
-- release memory
wifi_init,package.loaded.wifi_init=nil,nil
dofile('wifi_connect.lc')(wifi.STATION,true) -- mode,sleep
status('iddle')

local api=require('keys').api
Expand Down
4 changes: 2 additions & 2 deletions lua_modules/upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
: ${CHANNEL:=37527}

(($#))||set wipe bmp180 am2321 i2d pms3003 sensor_hub \
keys_v$CHANNEL wifi_init rgbLED sendData AQmon init
keys_v$CHANNEL wifi_connect rgbLED sendData AQmon init

while (($#)); do
PORT=`ls /dev/ttyUSB? /dev/rfcomm? 2>/dev/null`
Expand All @@ -20,7 +20,7 @@ while (($#)); do
luatool.py -p $PORT -rw;;
bmp180|dht22|am2321)
luatool.py -p $PORT -rcf $opt.lua;;
wifi_init|i2d|pms3003|sendData|rgbLED|keys) #|hueLED)
wifi_connect|i2d|pms3003|sendData|rgbLED|keys) #|hueLED)
luatool.py -p $PORT -cf $opt.lua;;
app|app.*|AQmon|AQmon.*)
luatool.py -p $PORT -rcf ${opt%.*}.lua -t app.lua;;
Expand Down
98 changes: 98 additions & 0 deletions lua_modules/wifi_connect.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
--[[
wifi_connect.lua for nodemcu-devkit (ESP8266) with nodemcu-firmware
Initialize wifi in STATION mode from SSID/PASS keys stored is a separate file
Written by Álvaro Valdebenito,
a similar implementation can be found at
https://github.com/geekscape/nodemcu_esp8266/tree/master/skeleton
MIT license, http://opensource.org/licenses/MIT
]]

return function(mode,sleep)
-- mode: wifi.STATION|wifi.SOFTAP|wifi.STATIONAP
-- sleep: nil|false(wifi.NONE_SLEEP)|true(wifi.MODEM_SLEEP)

local pass,cfg={},{} -- password,AP search config.

-- callback function for wifi.sta.getap(cfg,0,listAP)
local function listAP(t) -- (SSID:'Authmode, RSSI, BSSID, Channel')
local stat={[0]='STATION_IDLE',
[1]='STATION_CONNECTING',
[2]='STATION_WRONG_PASSWORD',
[3]='STATION_NO_AP_FOUND',
[4]='STATION_CONNECT_FAIL',
[5]='STATION_GOT_IP'}
local ssid,v,rssi
for ssid,v in pairs(t) do
rssi=v:match("[^,]+,([^,]+),[^,]+,[^,]+")
if pass[ssid] and wifi.sta.status()==5 then
print((' STA Logged to: %s (%s dBm)'):format(ssid,rssi))
print((' %s %s'):format(stat[5],wifi.sta.getip()))
return -- stop search
elseif pass[ssid] then
print((' STA Loggin to: %s (%s dBm)'):format(ssid,rssi))
wifi.sta.config(ssid,pass[ssid])
local n=20
tmr.alarm(0,10000,1,function()
local s=wifi.sta.status()
if n>0 and s<=1 then
print(stat[s])
n=n-1
elseif s==5 then
tmr.stop(0)
print((' %s %s'):format(stat[5],wifi.sta.getip()))
return -- stop search
elseif n>0 then
tmr.stop(0)
print((' %s'):format(stat[s]))
else
tmr.stop(0)
print(' STA: Timed out')
end
end)
end
end
end

-- set new mode: wifi.STATION|wifi.SOFTAP|wifi.STATIONAP
if wifi.getmode()~=mode then
wifi.setmode(mode)
end

-- AP modes: wifi.SOFTAP|wifi.STATIONAP
if mode==wifi.SOFTAP or mode==wifi.STATIONAP then
cfg=require('keys').ap -- {ssid=ssid,pwd=pass}
wifi.setmode(mode)
wifi.ap.config(cfg)
print((' AP %s %s'):format(cfg.ssid,wifi.ap.getip()))
end

-- STA modes: wifi.STATION|wifi.STATIONAP
if mode==wifi.STATION or mode==wifi.STATIONAP then
local ssid,v
pass=require('keys').sta -- {ssid1=pass1,...}
if wifi.sta.status()==5 then
-- test current SSID
ssid=wifi.sta.getconfig()
cfg={ssid=ssid,bssid=nil,channel=0,show_hidden=1}
wifi.sta.getap(cfg,0,listAP)
else
-- loop over knowk SSIDs
for ssid,v in pairs(pass) do
cfg={ssid=ssid,bssid=nil,channel=0,show_hidden=1}
wifi.sta.getap(cfg,0,listAP)
end
end
-- put WiFi tranciver to sleep
if wifi.sta.status()==5 and sleep==true then
print('WiFi sleep')
wifi.sta.disconnect()
wifi.sleeptype(wifi.MODEM_SLEEP)
elseif sleep==false then
print('WiFi wakeup')
wifi.sta.connect()
wifi.sleeptype(wifi.NONE_SLEEP)
end
end
end
97 changes: 0 additions & 97 deletions lua_modules/wifi_init.lua

This file was deleted.

0 comments on commit 6466c00

Please sign in to comment.