8、nodeMCU学习笔记--wifi模块·下
2016-11-09 本文已影响4493人
谢mingmin
闲言碎语
前面介绍了station模式。在这个模式下,需要输入ssid和密码,如果把ssid和密码固化到代码里面显然有点不可取。幸好nodeMCU支持ap模型,可以给手机连接。我只需要将其设置成wifi.STATIONAP工作模式即可。具体怎么做,我们一步一步来。
为了使用户体验更好,本文会使用enduser setup模块。
模块函数
序号 | 函数名 | 参数 | 返回值 |
---|---|---|---|
1 | wifi.ap.config() | table | nil |
2 | wifi.ap.deauth() | 字符串 | true |
3 | wifi.ap.getbroadcast() | 空 | 字符串 |
4 | wifi.ap.getclient() | 空 | table |
5 | wifi.ap.getip() | 空 | 字符串 |
6 | wifi.ap.getmac() | 空 | 字符串 |
7 | wifi.ap.setip() | table | true / false |
8 | wifi.ap.setmac() | 字符串 | true / false |
9 | wifi.ap.dhcp.config() | table | pool_startip, pool_endip |
10 | wifi.ap.dhcp.start() | 空 | boolean |
11 | wifi.ap.dhcp.stop() | 空 | boolean |
- .ap.config配置ap,总共有7参数,除了ssid和pwd,其他都有默认值。
- .ap.deauth移除接入ap的节点。可以指定mac地址,不传入参数则全部移除。
- .ap.dhcp.config配置dhcp的ip池。
- .ap.dhcp.start启动dhcp。
- .ap.dhcp.stop停止dhcp。
其他就只是一下get函数和set函数,使用方法和station类似,不多说。下面接着说enduser setup模块,很简单,就3个函数。
序号 | 函数名 | 参数 | 返回值 |
---|---|---|---|
1 | enduser_setup.manual() | true / false | true / false |
2 | enduser_setup.start() | [onConnected()], [onError(err_num, string)], [onDebug(string)] | nil |
3 | enduser_setup.stop() | 空 | nil |
- 配置enduser是否为手动模式。如果为自动,则会配置一个开放的ap,在连入WiFi后暂停。
综合小例子
这里需要把wifi设置为station&ap混合模式。使用.ap.config配置ap信息,再把enduser_setup设置成手动模式。
wifi.setmode(wifi.STATIONAP)
wifi.ap.config({ssid="nodeMCU", pwd="12345678", auth=wifi.WPA2_PSK})
enduser_setup.manual(true)
print("ap ip:"..wifi.ap.getip())
print("ap mac:"..wifi.ap.getmac())
print("sta mac:"..wifi.sta.getmac())
enduser_setup.start(
function()
print("sta ip:" .. wifi.sta.getip())
wifi.setmode(wifi.STATION)
end,
function(err, str)
print("enduser_err:" .. str)
enduser_stop()
end
)
![](https://img.haomeiwen.com/i3379069/ff948ef8825b2df7.png)
![](https://img.haomeiwen.com/i3379069/2a6efc4701cbb79e.png)
一点问题
enduser_setup模块使用起来不是特别舒服,时不时页面没反应。连接到笔记本共享出来的WiFi后,修改WiFi密码后,页面依旧提示连接成功。感觉可以自己实现各小型的web server,或许可以改善体验。