基于ESP8266的NodeMCU web 无线设置

之前参考了一下别人的代码。

http://www.cnblogs.com/wangzexi/p/5723853.html

此文章使用的SDK版本比较老,现在很多实现都不一样了,每天只能晚上调试一会。

进过一个星期的摸索终于基本实现了要求。

之前的SDK

wifi.sta.config('MY_SSID', 'MY_PASSWORD')
wifi.ap.config({ ssid = 'mymcu', auth = AUTH_OPEN })
wifi.setmode(wifi.STATION)

wifi.sta.autoconnect(1)

SDK 2.1.0

station_cfg={}
station_cfg.ssid="alex_mcu"
station_cfg.pwd="12345678"
station_cfg.save=true
wifi.sta.config(station_cfg)

wifi.sta.autoconnect(1)

SDK 2.1.0后,貌似不支持wifi.sta.eventMonReg和wifi.sta.eventMonStart。

后采用直接定时器查询 wifi.sta.status() 返回当前状态。

全部代码如下,其他代码请参考原来链接:

-------------
-- define
-------------
IO_LED = 1
IO_LED_AP = 2
IO_BTN_CFG = 3
IO_BLINK = 4


TMR_WIFI = 4
TMR_BLINK = 5
TMR_BTN = 6


gpio.mode(IO_LED, gpio.OUTPUT)
gpio.mode(IO_LED_AP, gpio.OUTPUT)
gpio.mode(IO_BTN_CFG, gpio.INT)
gpio.mode(IO_BLINK, gpio.OUTPUT)


-------------
-- button
-------------
function onBtnEvent()
gpio.trig(IO_BTN_CFG)
tmr.alarm(TMR_BTN, 500, tmr.ALARM_SINGLE, function()
gpio.trig(IO_BTN_CFG, 'up', onBtnEvent)
end)


switchCfg()
end
gpio.trig(IO_BTN_CFG, 'up', onBtnEvent)


gpio.write(IO_LED_AP, gpio.LOW)


function switchCfg()
if wifi.getmode() == wifi.STATION then
wifi.setmode(wifi.STATIONAP)
httpServer:listen(80)
gpio.write(IO_LED_AP, gpio.HIGH)
else
wifi.setmode(wifi.STATION)
httpServer:close()
gpio.write(IO_LED_AP, gpio.LOW)
end
end


-------------
-- blink
-------------
blink = nil
tmr.register(TMR_BLINK, 100, tmr.ALARM_AUTO, function()
gpio.write(IO_BLINK, blink.i % 2)
tmr.interval(TMR_BLINK, blink[blink.i + 1])
blink.i = (blink.i + 1) % #blink
end)


function blinking(param)
if type(param) == 'table' then
blink = param
blink.i = 0
tmr.interval(TMR_BLINK, 1)
running, _ = tmr.state(TMR_BLINK)
if running ~= true then
tmr.start(TMR_BLINK)
end
else
tmr.stop(TMR_BLINK)
gpio.write(IO_BLINK, param or gpio.LOW)
end
end


-------------
-- wifi
-------------
print('Setting up WIFI...')


--station_cfg={}
--station_cfg.ssid="alex_mcu"
--station_cfg.pwd="12345678"
--station_cfg.save=true
--wifi.sta.config(station_cfg)
--wifi.sta.autoconnect(1)
ap_cfg={}
ap_cfg.ssid="alex_mcu"
ap_cfg.pwd="12345678"
wifi.ap.config(ap_cfg)
wifi.setmode(wifi.STATIONAP)
status = nil
wifi_status = nil


tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
    if wifi.sta.getip() == nil then
        print('Waiting for IP ...')
    else
        print('IP is ' .. wifi.sta.getip())
blinking({500, 500})
tmr.stop(1)
    end
end)
-------------
-- http
-------------
dofile('httpServer.lua')
httpServer:listen(80)
httpServer:use('/config', function(req, res)
if req.query.ssid ~= nil and req.query.pwd ~= nil then
--wifi.sta.config(req.query.ssid, req.query.pwd)
sta_cfg={}
sta_cfg.ssid=req.query.ssid
sta_cfg.pwd=req.query.pwd
sta_cfg.save=true
wifi.sta.config(sta_cfg)
status = 'STA_CONNECTING'
tmr.alarm(TMR_WIFI, 1000, tmr.ALARM_AUTO, function()
wifi_status = wifi.sta.status()

if wifi_status == wifi.STA_CONNECTING then
print(status)
status = 'STA_CONNECTING'
elseif wifi_status == wifi.STA_WRONGPWD then
print(status)
status = 'STA_WRONGPWD'
elseif wifi_status == wifi.STA_APNOTFOUND then
print(status)
status = 'STA_APNOTFOUND'
elseif wifi_status == wifi.STA_GOTIP then
status = 'STA_GOTIP'
print(status, wifi.sta.getip())
end

if status ~= 'STA_CONNECTING' then
res:type('application/json')
res:send('{"status":"' .. status .. '"}')
tmr.stop(TMR_WIFI)
end
end)
end
end)


httpServer:use('/scanap', function(req, res)
wifi.sta.getap(function(table)
local aptable = {}
for ssid,v in pairs(table) do
local authmode, rssi, bssid, channel = string.match(v, "([^,]+),([^,]+),([^,]+),([^,]+)")
aptable[ssid] = {
authmode = authmode,
rssi = rssi,
bssid = bssid,
channel = channel
}
end
res:type('application/json')
res:send(sjson.encode(aptable))
end)
end)


你可能感兴趣的:(ESP8266)