闲言碎语
上一篇水文中简单的介绍了wifi模块中的几个配置相关的函数。这篇文章准备介绍station模式下相关的函数。station模式下,nodemcu可以连接入wifi网络,成为一个局域网设备。
模块函数
station部分的函数,数量也超过了10个。虽然看起来很多,但是关键也就那么几个函数。
序号 | 函数名 | 参数 | 返回值 |
---|---|---|---|
1 | wifi.sta.autoconnect() | 0 / 1 | nil |
2 | wifi.sta.config() | ssid, password[, auto[, bssid] | nil |
3 | wifi.sta.connect() | 空 | nil |
4 | wifi.sta.disconnect() | 空 | nil |
5 | wifi.sta.eventMonReg() | wifi_status[, function([previous_state])] | nil |
6 | wifi.sta.eventMonStart() | [ms] | nil |
7 | wifi.sta.eventMonStop() | [unregister_all] | nil |
8 | wifi.sta.getap() | [[cfg], format,] callback(table) | nil |
9 | wifi.sta.getbroadcast() | nil | 字符串 |
10 | wifi.sta.getconfig() | nil | ssid, password, bssid_set, bssid |
11 | wifi.sta.gethostname() | nil | 字符串 |
12 | wifi.sta.getip() | nil | 字符串 |
13 | wifi.sta.getmac() | nil | 字符串 |
13 | wifi.sta.getrssi() | nil | 字符串 |
14 | wifi.sta.sethostname() | 字符串 | true / false |
15 | wifi.sta.setip() | table | true / false |
16 | wifi.sta.setmac() | 字符串 | true / false |
17 | wifi.sta.status() | nil | 0~5 |
- .sta.autoconnect用于设置是否自动连接;
- .sta.config用来设置接入路由的信息,该信息只有被修改才会变动,掉电信息依然在。auto参数默认为1,即自动连接。当周围有其他同名ssid时,可以通过bssid参数指定接入某mac地址路由。
wifi.sta.config("ssid", "password")
- .sta.connect和.sta.disconnect用于接入或者断开连接。当在.sta.config设置了手动连接时,才需要使用.sta.connect;
- .sta.eventMonReg用于注册状态监听。总共有6种状态;
wifi.sta.eventMonReg(wifi.STA_IDLE, function()
end)
wifi.sta.eventMonReg(wifi.STA_IDLE) --注销回调
- .sta.eventMonStart用于启动状态监听,可选参数是回调时间间隔,默认150ms;
- .sta.eventMonStop用于暂停状态监听,如果可选参数传入1,则暂停并注销回调;
- .sta.getap用于扫描ap列表。参数cfg是lua的table数据类型,参数format用于选择table的格式,callback(table)是回调函数。
- .sta.getbroadcast用于获取广播地址;
- .sta.getconfig用于获取配置信息;
- .sta.gethostname用于获取主机名
- .sta.getip用于获取ip地址、掩码、网关;
- .sta.getmac用于获取mac地址;
- .sta.getrssi用于获取连接点的rssi。如果未接入网络则返回nil;
- .sta.sethostname用于设置主机名,只能包含数字、字母、-,且不能超过32字符,第一个和最后一个不能是下划线;
- .sta.setip用于设置ip地址、掩码、网关;
- .sta.setmac用于设置mac地址;
- .sta.status用于获取状态。
综合小例子
nodeMCU提供的API还是蛮简洁的,几句话就可以实现wifi上网。这里先使用.sta.sethostname设置nodeMCU模块的名字,方便与其他设备区分(比如,手机)。注册一个状态(wifi.STA_GOTIP)监听,当连入wifi的时候会触发回调。最后使用.sta.config接入网络,相当于平时用手机输入ssid和密码。为了方便,我用笔记本共享wifi来给nodeMCU接入。
wifi.sta.sethostname("Node-MCU")
print(wifi.sta.gethostname())
function printap(ap)
for k, v in pairs(ap) do
print(k.." : "..v)
end
end
wifi.sta.eventMonReg(wifi.STA_GOTIP, function()
print(wifi.sta.getip())
wifi.sta.getrssi()
wifi.sta.getap(printap)
end)
wifi.sta.eventMonStart()
wifi.sta.config("wifitest", "kwmb566687")
![](https://img.haomeiwen.com/i3379069/1c7cb77be126d3fc.png)
网友评论
init.lua:3: in main chunk
这是为什么,小白,很希望你能指点一下
lua: init.lua:10: attempt to call field 'eventMonReg' (a nil value)
stack traceback:
init.lua:10: in main chunk
[C]: ?
新手,不懂这个啥意思啊
print("host name successfully changed")
else print("host name was not changed")
end
这句总是出错;wifi.lua:1: bad argument #1 to 'sethostname' (Invalid hostname)
stack traceback:
[C]: in function 'sethostname'
wifi.lua:1: in main chunk
[C]: in function 'dofile'
stdin:1: in main chunk
Warning, deprecated API! wifi.sta.eventmonreg() is replaced by wifi.eventmon.register(). It will be removed in the next version. See documentation for details.
Warning, deprecated API! Argument style station configuration is replaced by table style station configuration. It will be removed in the next version. See documentation for details.