美文网首页nodemcu
7、nodeMCU学习笔记--wifi模块·中

7、nodeMCU学习笔记--wifi模块·中

作者: 谢mingmin | 来源:发表于2016-11-06 10:28 被阅读5799次

闲言碎语

上一篇水文中简单的介绍了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
  1. .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")
走,冲浪去

相关文章

网友评论

  • 635d8054bc41:我试着把SSID 和password 改成我的路由器也是同样报错
  • 635d8054bc41:你好,我把你程序复制过去会报错说[C]: in function 'config'
    init.lua:3: in main chunk
    这是为什么,小白,很希望你能指点一下
    谢mingmin:@世華 init.lua 出错,看看config函数是不是改了
  • 2e72401c54b0:你好,把上面您的例子复制过来,运行后报错:
    lua: init.lua:10: attempt to call field 'eventMonReg' (a nil value)
    stack traceback:
    init.lua:10: in main chunk
    [C]: ?

    新手,不懂这个啥意思啊
    谢mingmin:@世華 要么函数名改了,要么参数传了空,看文档,看文档。api可能改了
    635d8054bc41:@谢mingmin 我也遇到这个报错了,怎么解决:joy:
    谢mingmin:@快乐就好_65ce 在 文件的 第 10 行报错了,后面的stack是栈信息
  • zldiy:if(wifi.sta.sethostname("NodeMcu") == true) then
    print("host name successfully changed")
    else print("host name was not changed")
    end
    zldiy:我看了GitHub的commit,老外说的是历程的问题?还是固件的问题?
    谢mingmin: @zldiy 新固件有bug,等合并
    zldiy:这个是实验用的脚本;
  • zldiy:看了很多关于大侠的文字;今天鼓起勇气向您请教问题: 我今天在使用WiFi模块时,发现 wifi.sta.sethostname("nodemcu") 用之前原来带的固件,根本不运行。又重新服务器编译固件后,再使用esplorer出现这样的错误;怎么解?
    这句总是出错;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
    zldiy:@谢mingmin 是不是维护这个固件的人很少?
    谢mingmin:我用新固件也有这个问题,在GitHub上面提问了,看看后面有没有人解答:joy:
  • a24a39e2e4e7:我的设备显示的名字不是Node-MCU,什么原因呢
    a24a39e2e4e7:@谢mingmin 我用这句程序设置了wifi.sta.sethostname("Node-MCU")
    谢mingmin: @a24a39e2e4e7 名字可以自己改啊,没有就是有个默认值
  • e3e2c6c1291f:改了ssid和密码,然后连接手机热点接不上怎么办:joy: 出了两warning
    e3e2c6c1291f:@谢mingmin 还是说因为热点名字是中文不行吗:no_mouth:
    e3e2c6c1291f:@谢mingmin
    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.
    谢mingmin: @大白小白都不白 warning内容是什么啊
  • springmax:为什么没有反应。 @谢mingmin
    谢mingmin: @springmax 大哥啊,什么没反应啊,不说清楚我怎么会知道
  • Luat物联网通信模块:可以关注下Luat开源项目,基于Air200 GPRS模块(价格才12块多),基于Lua开发应用软件,超级容易上手。 而且开发点阵LCD 的UI 非常方便,有丰富的底层库,支持中文很方便。

本文标题:7、nodeMCU学习笔记--wifi模块·中

本文链接:https://www.haomeiwen.com/subject/sbzwuttx.html