NodeMCU入门(4):搭建Web服务器,配置网络连接
准备工作
1.NodeMCU模块
2.ESPlorer v0.2.0-rc6
3.NodeMCU-HTTP-Server
搭建web服务器
下载https://github.com/wangzexi/NodeMCU-HTTP-Server文件,并上传到NodeMCU中
修改init.lua文件,可参看NodeMCU-HTTP-Server Example
-- init.lua ---------------------
-- wifi
--------------------- print('Setting up WIFI...')
wifi.setmode(wifi.STATION)
wifi.sta.config('WX401901', 'smyh1234')
wifi.sta.autoconnect() tmr.alarm(, , tmr.ALARM_AUTO, function()
if wifi.sta.getip() == nil then
print('Waiting for IP ...')
else
print('IP is ' .. wifi.sta.getip())
tmr.stop()
end
end) -- Serving static files
dofile('httpServer.lua')
httpServer:listen() -- 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)
现在是工作在STATION模式下,保存后可以看到获取的IP地址,在浏览器中输入http://192.168.100.100就可以看到一个简单的页面了。
修改成 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配置无线连接账号和密码
这里借用NodeMCU之旅(四):实现Web配置页面的配置页面。
首先,删除上一步中上传的测试文件 404.html和doge.jpg
然后,上传新的配置页面
最后,修改init.lua文件去掉演示web相关的路由配置添加两个新的路由:
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) status = 'STA_CONNECTING'
status_sleep=
tmr.alarm(TMR_WIFI, , tmr.ALARM_AUTO, function()
status_sleep=status_sleep+
--当设置的账号密码错误时,定时器不会停止,所以这里添加了超时检查
if(status_sleep==10) then
res:type('application/json')
res:send('{"status":"timeout"}')
tmr.stop(TMR_WIFI)
end if status ~= 'STA_CONNECTING' then
res: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
}
end
res: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修改账号密码。
-- init.lua ----------------------
--define
---------------------
IO_BLINK = TMR_WIFI =
TMR_BLINK =
TMR_BTN = gpio.mode(IO_BLINK, gpio.OUTPUT) ---------------------
-- blink
---------------------
blink = nil
tmr.register(TMR_BLINK, , tmr.ALARM_AUTO, function()
gpio.write(IO_BLINK, blink.i % )
tmr.interval(TMR_BLINK, blink[blink.i + ])
blink.i = (blink.i + ) % #blink
end) function blinking(param)
if type(param) == 'table' then
blink = param
blink.i =
tmr.interval(TMR_BLINK, )
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...')
wifi.setmode(wifi.STATIONAP )
wifi.sta.config('WX401901', 'smyh1234')
wifi.sta.autoconnect() tmr.alarm(, , tmr.ALARM_AUTO, function()
if wifi.sta.getip() == nil then
print('Waiting for IP ...')
else
print('IP is ' .. wifi.sta.getip())
tmr.stop()
end
end) status=nil wifi.sta.eventMonReg(wifi.STA_WRONGPWD, function()
blinking({, , , })
status = 'STA_WRONGPWD'
print(status)
end) wifi.sta.eventMonReg(wifi.STA_APNOTFOUND, function()
blinking({, })
status = 'STA_APNOTFOUND'
print(status)
end) wifi.sta.eventMonReg(wifi.STA_CONNECTING, function(previous_State)
blinking({, })
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() ---------------------
-- http
---------------------
dofile('httpServer.lua')
httpServer:listen() 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) status = 'STA_CONNECTING'
status_sleep=
tmr.alarm(TMR_WIFI, , tmr.ALARM_AUTO, function()
status_sleep=status_sleep+ if(status_sleep==) then
res:type('application/json')
res:send('{"status":"timeout"}')
tmr.stop(TMR_WIFI)
end if status ~= 'STA_CONNECTING' then
res: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
}
end
res:type('application/json')
res:send(sjson.encode(aptable))
end)
end)
init.lua
相关资源
本文是在NodeMCU之旅(三):响应配置按钮、NodeMCU之旅(四):实现Web配置页面 基础之上的学习过程,感谢原作者。
NodeMCU入门(4):搭建Web服务器,配置网络连接的更多相关文章
- 轻松使用Nginx搭建web服务器
如果读者以前做过web开发的话,就应该知道如何去搭建一个web服务器来跑你的web站点,在windows下你可能会选择去用IIS,十分的快捷,在linux下,你可能首先会想到apache,“一哥”( ...
- 记录一些服务端术语和搭建web服务器
菜单快捷导航 服务端常用术语 搭建web服务器和配置虚拟主机 记录一些服务端方面的常用术语 1.CS架构和BS架构 1.1 CS架构 CS(Client/Server),基于安装包类型的桌面或手机软件 ...
- CentOS 6.2下搭建Web服务器
1Centos 6.2下搭建web服务器 如今,Linux在Web应用越来越广,许多企业都采用Linux来搭建Web服务器,这样即节省了购买正版软件的费用,而且还能够提高服务器的安全性. 之前我们介绍 ...
- 使用 Node.js 搭建 Web 服务器
使用Node.js搭建Web服务器是学习Node.js比较全面的入门教程,因为实现Web服务器需要用到几个比较重要的模块:http模块.文件系统.url解析模块.路径解析模块.以及301重定向技术等, ...
- Mac上一条命令搭建web服务器
实际测试工作中偶尔会需要搭建Web服务器环境,由于Mac OS X自带了Apache和PHP环境,只需要简单的启动就可以. 开启Apache 开启Web服务器的方法有两种(默认启动端口号是80): 打 ...
- Android手机用KSWEB搭建Web服务器成功安装WordPress
之前部落分享的几个免费Web服务器软件都是用来安装在本地电脑上,搭建Apache.PhpMyAdmin.MySQL等网站运行环境,然后我们就可以在电脑上测试运行Wordpress.Discuz! 论坛 ...
- centos7 搭建WEB服务器
centos7 搭建WEB服务器 2017年09月17日 09:44:50 逝然1994 阅读数:18321 标签: centosapacheweb服务器 更多 个人分类: centos服务器简单配置 ...
- CentOS 6.3下搭建Web服务器
准备前的工作: 1.修改selinux配置文件(/etc/sysconfig/selinux) 关闭防火墙 (1)把SELINUX=enforcing注释掉 (2)并添加SELINUX=disable ...
- Ubuntu 搭建Web服务器(MySQL+PHP+Apache)详细教程
Ubuntu 搭建Web服务器(MySQL+PHP+Apache)详细教程 看了好多人的博客,有的不全 or 有问题,整理了一下,适合小白 新手先整理几个小问题 1.为啥使用 Linux 搭建服务器? ...
随机推荐
- 从花式swap引出的pointer aliasing问题
上次,一个同学问我,你知不知道可以不用引入中间变量就可以实现swap? 我说,我知道,可以用加减法或者异或实现,像是这样 void mySwap(int &x,int &y) { x= ...
- Java--定时器问题
定时器问题 定时器属于基本的基础组件,不管是用户空间的程序开发,还是内核空间的程序开发,很多时候都需要有定时器作为基础组件的支持.一个定时器的实现需要具备以下四种基本行为:添加定时器.取消定时器.定时 ...
- express4.x的使用
①.安装 npm install -g express ②.创建应用 express [目录] 会在目录下生成 node_modules, 存放所有的项目依赖库.(每个项目管理自己的依赖,与Ma ...
- 生产环境中使用Docker Swarm的一些建议
译者按: 实践中会发现,生产环境中使用单个Docker节点是远远不够的,搭建Docker集群势在必行.然而,面对Kubernetes, Mesos以及Swarm等众多容器集群系统,我们该如何选择呢?它 ...
- Struts2之Action接收请求参数和拦截器
技术分析之在Struts2框架中使用Servlet的API 1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Ac ...
- asp.net 母版-页脚制作
1.母版创建流程略过. 2.创建母版页css:Site.css body { } .linkButton{ text-decoration:none; color:whitesmoke; } 3.母版 ...
- 像写C#一样编写java代码
JDK8提供了非常多的便捷用法和语法糖,其编码效率几乎接近于C#开发,maven则是java目前为止最赞的jar包管理和build工具,这两部分内容都不算多,就合并到一起了. 愿编写java代码的过程 ...
- CSS3特效----图片动态提示效果
需掌握的知识点: 1.掌握两个HTML5新标签figure以及figcaption的用法 2.掌握transform的属性特点,并能熟练运用 3.学会通过transition及transform配合, ...
- HDU4497GCD and LMC最大公约数与最小公倍数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 题目大意: 求gcd(x,y,z)=G且lcm(x,y,z)=L的方法数. 题目分析: 起初这 ...
- 学习笔记:javascript 表单对象(form)
Form 对象属性 属性 描述 acceptCharset 服务器可接受的字符集. action 设置或返回表单的 action 属性. enctype 设置或返回表单用来编码内容的 MIME 类型. ...