Nodemcu通过网页Web设置sta.config配置

来源:互联网 发布:国泰安怎么下载数据 编辑:程序博客网 时间:2024/06/15 22:40

首先,感谢Zexi on GitHub 213行代码写出一个HttpServer真的服了。还带有GZip压缩,先膜拜大神。
HttpServer By Zexi
访问这个网址,把httpServer.lua复制下来,下面我们就要用这个文件,建立自己的配置网页。

我们的需求是,NodeMCU用AP+Station模式,启动一个叫Config的热点,用户连接后,可以通过192.168.1.1来进行访问,然后输入WIFI的SSID和密码,就能让NodeMCU自动连接!
天太晚了,不多BB,直接上代码。

init.lua

wifi.setmode(wifi.STATIONAP)cfg={}cfg.ssid="config" --我们的NodeMcu热点cfg.pwd="00000000"--密码wifi.ap.config(cfg)cfg2 ={ip="192.168.1.1",--设置IPnetmask="255.255.255.0",--子网掩码gateway="192.168.1.1"--默认网关}wifi.ap.setip(cfg2)wifi.sta.autoconnect(1)--自动连接dofile('httpServer.lua')--执行HttpServer.luahttpServer:use('/config', function(req, res)    if req.query.ssid ~= nil and req.query.pwd ~= nil then        print(req.query.ssid..req.query.pwd)        config={}        config.ssid=req.query.ssid        config.pwd=req.query.pwd        wifi.sta.config(config)    end     res:send('<head><meta charset="UTF-8"><title>配置终端</title></head><h1>您设置的wifi是:'..req.query.ssid..',请等待红灯常亮即连接完成。</h1>')end)httpServer:listen(80) --启动Server

index.html

别忘了把这个文件上传哦~

<!DOCTYPE html><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1">    <title>配置终端</title></head><body style="text-align:center;background:#f8fffbb2;">    <h1>配置页面</h1>    <form action="/config" method="get">        <label>WiFi</label>        <input type="text" name="ssid" />        <br />        <label>密码</label>        <input type="password" name="pwd" />        <br />        <input type="submit" value="确认" style="background:#678df9;height:40px;width:60px;border-radius:5px;border:none;outline:none;"/>    </form>    <p style="max-width:50vw;margin:auto;">注意:由于NodeMCU内存很小,附近热点过多时,扫描热点会造成内存不足自动重启。请手动输入WIFI信息进行配置。</p></body><style>    input{        margin-bottom:30px;    }</style></html>

这可以说是最简单的配置界面了,但是大家可以根据这个自己开发需要的功能,就需要靠自己发挥了!!

阅读全文
0 0
原创粉丝点击