nodemcu 自建服务器,NodeMCU入门(4):搭建Web服务器,配置网络连接(示例代码)

准备工作

1.NodeMCU模块

2.ESPlorer v0.2.0-rc6

3.NodeMCU-HTTP-Server

搭建web服务器

cf0a6a11febf425488d1dc344489e8fe.jpg

修改init.lua文件,可参看NodeMCU-HTTP-Server Example

eff25b418af04a6a96c3ab8fa9d54fb7.jpg

edab2d63eded4e6388c3a7db084d3b6b.jpg

--init.lua

-----------------------wifi---------------------

print(\'Setting up WIFI...\')

wifi.setmode(wifi.STATION)

wifi.sta.config(\'WX401901\', \'smyh1234\')

wifi.sta.autoconnect(1)

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())

tmr.stop(1)end

end)--Serving static files

dofile(\'httpServer.lua\')

httpServer:listen(80)--Custom API--Get text/html

httpServer:use(\'/welcome\', function(req, res)

res:send(\'Hello\' .. req.query.name) --/welcome?name=doge

end)--Get file

httpServer:use(\'/doge\', function(req, res)

res:sendFile(\'doge.jpg\')end)--Get json

httpServer:use(\'/json\', function(req, res)

res:type(\'application/json\')

res:send(\'{"doge": "smile"}\')end)--Redirect

httpServer:use(\'/redirect\', function(req, res)

res:redirect(\'doge.jpg\')end)

View Code

现在是工作在STATION模式下,保存后可以看到获取的IP地址,在浏览器中输入http://192.168.100.100就可以看到一个简单的页面了。

3e65ef2f7a724cbf8e34280265ed4163.jpg

修改成 wifi.setmode(wifi.STATIONAP) 模式,出现了内存不足的错误,无视它。

E:M 248

not enough memory

stack traceback:

[C]: in function \'dofile\'

init.lua:22: in main chunk

[C]: in function \'dofile\'

stdin:1: in main chunk

现在NodeMCU有两个IP,一个是作为STATION的192.168.100.100,另外一个是作为AP的192.168.4.1,这时在无线列表中会多出一个AI-THINKER_AC72F4,用手机或电脑连上后浏览器中输入http://192.168.4.1 也可以看到页面。

实现Web配置无线连接账号和密码

首先,删除上一步中上传的测试文件 404.html和doge.jpg

a8d5681f88ec458f961d9846811a24ca.jpg

然后,上传新的配置页面

93011034e9ba4a0494c6e1590152c1e3.jpg

最后,修改init.lua文件去掉演示web相关的路由配置添加两个新的路由:

httpServer:use(\'/config\', function(req, res)if req.query.ssid ~= nil and req.query.pwd ~= nil thenwifi.sta.config(req.query.ssid, req.query.pwd)

status= \'STA_CONNECTING\'status_sleep=0tmr.alarm(TMR_WIFI,1000, tmr.ALARM_AUTO, function()

status_sleep=status_sleep+1

--当设置的账号密码错误时,定时器不会停止,所以这里添加了超时检查if(status_sleep==10) then

res:type(\'application/json\')

res:send(\'{"status":"timeout"}\')

tmr.stop(TMR_WIFI)

end

if status ~= \'STA_CONNECTING\' thenres:type(\'application/json\')

res:send(\'{"status":"\' .. status .. \'"}\')

tmr.stop(TMR_WIFI)end

end)end

end)--Get json

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

}endres:type(\'application/json\')

--原来是cjson.encode(aptable),因为构建固件时没有找到cjson用了sjson,所以这里改成sjson

res:send(sjson.encode(aptable))end)end)

当请求的方法里出现异常时前端看到的是404错误,这个坑让我一直怀疑是httpServer.lua的问题

保存后,访问http://192.168.100.100,修改成一个错误的密码,点击连接网络,Led灯开始闪烁然后模块重启常亮,表示修改失败。

* Sending index.html

* Finished index.html

PANIC: unprotected error in call to Lua API (httpServer.lua:77: not connected)

还得切换Wifi到AI-THINKER_AC72F4,通过http://192.168.4.1修改账号密码。

cce256b69255497a8ee5cece50537bcf.jpg

eff25b418af04a6a96c3ab8fa9d54fb7.jpg

edab2d63eded4e6388c3a7db084d3b6b.jpg

--init.lua

------------------------define---------------------

IO_BLINK = 4TMR_WIFI= 4TMR_BLINK= 5TMR_BTN= 6gpio.mode(IO_BLINK, gpio.OUTPUT)-----------------------blink---------------------

blink = niltmr.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) % #blinkend)functionblinking(param)if type(param) == \'table\' thenblink=param

blink.i= 0tmr.interval(TMR_BLINK,1)

running, _=tmr.state(TMR_BLINK)if running ~= true thentmr.start(TMR_BLINK)end

elsetmr.stop(TMR_BLINK)

gpio.write(IO_BLINK, paramorgpio.LOW)end

end

-----------------------wifi---------------------

print(\'Setting up WIFI...\')

wifi.setmode(wifi.STATIONAP )

wifi.sta.config(\'WX401901\', \'smyh1234\')

wifi.sta.autoconnect(1)

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())

tmr.stop(1)end

end)

status=nilwifi.sta.eventMonReg(wifi.STA_WRONGPWD,function()

blinking({100, 100 , 100, 500})

status= \'STA_WRONGPWD\'

print(status)end)

wifi.sta.eventMonReg(wifi.STA_APNOTFOUND,function()

blinking({2000, 2000})

status= \'STA_APNOTFOUND\'

print(status)end)

wifi.sta.eventMonReg(wifi.STA_CONNECTING,function(previous_State)

blinking({300, 300})

status= \'STA_CONNECTING\'

print(status)end)

wifi.sta.eventMonReg(wifi.STA_GOTIP,function()

blinking()

status= \'STA_GOTIP\'

print(status, wifi.sta.getip())end)

wifi.sta.eventMonStart(1000)-----------------------http---------------------

dofile(\'httpServer.lua\')

httpServer:listen(80)

httpServer:use(\'/config\', function(req, res)if req.query.ssid ~= nil and req.query.pwd ~= nil thenwifi.sta.config(req.query.ssid, req.query.pwd)

status= \'STA_CONNECTING\'status_sleep=0tmr.alarm(TMR_WIFI,1000, tmr.ALARM_AUTO, function()

status_sleep=status_sleep+1

if(status_sleep==10) thenres:type(\'application/json\')

res:send(\'{"status":"timeout"}\')

tmr.stop(TMR_WIFI)end

if status ~= \'STA_CONNECTING\' thenres:type(\'application/json\')

res:send(\'{"status":"\' .. status .. \'"}\')

tmr.stop(TMR_WIFI)end

end)end

end)--Get json

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

}endres:type(\'application/json\')

res:send(sjson.encode(aptable))end)end)

init.lua

相关资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值