2.網路功能
#wifi模块 ##常量 wifi.STATION, wifi.SOFTAP, wifi.STATIONAP
wifi.setmode(mode)
####描述 设置wifi的工作模式。
####语法 wifi.setmode(mode)
####参数 mode: 取值为:wifi.STATION, wifi.SOFTAP or wifi.STATIONAP
####返回值 返回设置之后的mode值
####示例
wifi.setmode(wifi.STATION)
####参见 - wifi.getmode()
wifi.getmode(mode)
####描述 获取wifi的工作模式。
####语法 wifi.getmode()
####参数 nil
####返回值 返回wifi的工作模式
####示例
print(wifi.getmode())
####参见 - wifi.setmode()
wifi.startsmart()
####描述 开始自动配置,如果配置成功自动设置ssid和密码。
####语法 wifi.startsmart(channel, function succeed_callback())
####参数
channel: 1~13, 启动寻找的初始频段,如果为nil默认值为6频段。每个频段搜寻20s。
succeed_callback: 配置成功的回调函数,配置成功并连接至AP后调用此函数。
####返回值 nil
####示例
wifi.startsmart(6, function() end)
####参见 - wifi.stopsmart()
wifi.stopsmart()
####描述 停止配置。
####语法 wifi.stopsmart()
####参数 nil
####返回值 nil
####示例
wifi.stopsmart()
####参见 - wifi.startsmart()
#wifi.sta 子模块
wifi.sta.config()
####描述 设置station模式下的ssid和password。
####语法 wifi.sta.config(ssid, password)
####参数
ssid: 字符串,长度小于32字节。
password: 字符串,长度小于64字节。
####返回值 nil
####示例
wifi.sta.config("myssid","mypassword")
####参见 - wifi.sta.connect()
wifi.sta.connect()
####描述 station模式下连接AP。
####语法 wifi.sta.connect()
####参数 nil
####返回值 nil
####示例
wifi.sta.connect()
####参见 - wifi.sta.disconnect()
wifi.sta.disconnect()
####描述 station模式下与AP断开连接。
####语法 wifi.sta.disconnect()
####参数 nil
####返回值 nil
####示例
wifi.sta.disconnect()
####参见 - wifi.sta.config()
wifi.sta.autoconnect()
####描述 station模式下自动连接。
####语法 wifi.sta.autoconnect(auto)
####参数 auto: 0:取消自动连接,1:使能自动连接。
####返回值 nil
####示例
wifi.sta.autoconnect()
####参见 - wifi.sta.config()
wifi.sta.getip()
####描述 station模式下获取ip
####语法 wifi.sta.getip()
####参数 nil
####返回值 ip地址字符串,如:"192.168.0.111"
若ip地址为0.0.0.0,则返回nil
####示例
-- print current ip print(wifi.sta.getip())
####参见 - wifi.sta.getmac()
wifi.sta.getmac()
####描述 station模式下获取mac地址。
####语法 wifi.sta.getmac()
####参数 nil
####返回值 mac地址字符串,如:"18-33-44-FE-55-BB"
####示例
-- 打印当前的mac地址 print(wifi.sta.getmac())
####参见 - wifi.sta.getip()
wifi.sta.getap()
####描述 扫描并列出ap,结果以一个lua table为参数传递给回调函数。
####语法 wifi.sta.getap(function(table))
####参数 function(table): 当扫描结束时,调用此回调函数
扫描结果是一个lua table,key为ap的ssid,value为其他信息,格式:authmode,rssi,bssid,channel
####返回值 nil
####示例
-- print ap list function listap(t) for k,v in pairs(t) do print(k.." : "..v) end end wifi.sta.getap(listap)
####参见 - wifi.sta.getip()
wifi.sta.status()
####描述 station模式下获取当前连接状态。
####语法 wifi.sta.status()
####参数 nil
####返回值 number: 0~5 0: STATION_IDLE, 1: STATION_CONNECTING, 2: STATION_WRONG_PASSWORD, 3: STATION_NO_AP_FOUND, 4: STATION_CONNECT_FAIL, 5: STATION_GOT_IP.
####参见 -
#wifi.ap 子模块
wifi.ap.config()
####描述 设置ap模式下的ssid和password
####语法 wifi.ap.config(cfg)
####参数 cfg: 设置AP的lua table
####示例:
cfg={} cfg.ssid="myssid" cfg.pwd="mypwd" wifi.ap.config(cfg)
####返回值 nil
####示例
wifi.ap.config(ssid, 'password')
####参见 -
wifi.ap.getip()
####描述 ap模式下获取ip
####语法 wifi.ap.getip()
####参数 nil
####返回值 ip地址字符串,如:"192.168.0.111"
若ip地址为0.0.0.0,则返回nil
####示例
wifi.ap.getip()
####参见 - wifi.ap.getmac()
wifi.ap.getmac()
####描述 ap模式下获取mac地址。
####语法 wifi.ap.getmac()
####参数 nil
####返回值 mac地址字符串,如:"1A-33-44-FE-55-BB"
####示例
wifi.ap.getmac()
####参见 - wifi.ap.getip()
#net 模块 ##常量 net.TCP, net.UDP
net.createServer()
####描述 创建一个server。
####语法 net.createServer(type, timeout)
####参数 type: 取值为:net.TCP 或者 net.UDP
timeout: 1~28800, 当为tcp服务器时,客户端的超时时间设置。
####返回值 net.server子模块
####示例
net.createServer(net.TCP, 30)
####参见 - net.createConnection()
net.createConnection()
####描述 创建一个client。
####语法 net.createConnection(type, secure)
####参数 type: 取值为:net.TCP 或者 net.UDP
secure: 设置为1或者0, 1代表安全连接,0代表普通连接。
####返回值 net.server子模块
####示例
net.createConnection(net.UDP, 0)
####参见 - net.createServer()
#net.server 子模块
listen()
####描述 侦听指定ip地址的端口。
####语法 net.server.listen(port,[ip],function(net.socket))
####参数 port: 端口号
ip:ip地址字符串,可以省略
function(net.socket): 连接创建成功的回调函数,可以作为参数传给调用函数。
####返回值 nil
####示例
-- 创建一个server sv=net.createServer(net.TCP, 30) -- 30s 超时 -- server侦听端口80,如果收到数据将数据打印至控制台,并向远端发送‘hello world’ sv:listen(80,function(c) c:on("receive", function(c, pl) print(pl) end) c:send("hello world") end)
####参见 - net.createServer()
close()
####描述 关闭server
####语法 net.server.close()
####参数 nil
####返回值 nil
####示例
-- 创建server sv=net.createServer(net.TCP, 5) -- 关闭server sv:close()
####参见 - net.createServer()
#net.socket 子模块
connect()
####描述 连接至远端。
####语法 connect(port, ip/domain)
####参数 port: 端口号
ip: ip地址或者是域名字符串
####返回值 nil
####参见 - net.socket:on()
send()
####描述 通过连接向远端发送数据。
####语法 send(string, function(sent))
####参数 string: 待发送的字符串
function(sent): 发送字符串后的回调函数。
####返回值 nil
####参见 - net.socket:on()
on()
####描述 向事件注册回调函数。
####语法 on(event, function cb())
####参数 event: 字符串,取值为: "connection", "reconnection", "disconnection", "receive", "sent"
function cb(net.socket, [string]): 回调函数。第一个参数是socket.
如果事件是"receive", 第二个参数则为接收到的字符串。
####返回值 nil
####示例
sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) end ) sk:connect(80,"192.168.0.66") sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
####参见 - net.createServer()
close()
####描述 关闭socket。
####语法 close()
####参数 nil
####返回值 nil
####参见 - net.createServer()
dns()
####描述 获取当前域的ip
####语法 dns(domain, function(net.socket, ip))
####参数 domain: 当前域的名称
function (net.socket, ip): 回调函数。第一个参数是socket,第二个参数是当前域的ip字符串。
####返回值 nil
####示例
sk=net.createConnection(net.TCP, 0) sk:dns("www.nodemcu.com",function(conn,ip) print(ip) end) sk = nil
####参见 - net.createServer()
#mqtt 模块 ##常量
mqtt.Client()
####描述 创建一个mqtt client.
####语法 mqtt.Client(clientid, keepalive, user, pass)
####参数 clientid: mqtt客户端id.
keepalive: 保持连接的时间,单位:秒.
user: 用户名,字符串.
pass: 密码,字符串.
####返回值 mqtt客户端.
####示例
-- 创建一个mqtt client,保持连接包时间120s. m = mqtt.Client("clientid", 120, "user", "password") --设置Last Will和Testament (可选).--如果mqtt client不发送保持连接包,服务器会向标题"/lwt"发送一个qos = 0, retain = 0, data = "offline"的消息. m:lwt("/lwt", "offline", 0, 0) m:on("connect", function(con) print ("connected") end) m:on("offline", function(con) print ("offline") end) -- 接收到消息事件 m:on("message", function(conn, topic, data) print(topic .. ":" ) if data ~= nil then print(data) endend) -- 如果需要安全连接,则m:connect("192.168.11.118", 1880, 1) m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end) -- 订阅"/topic"消息,qos = 0 m:subscribe("/topic",0, function(conn) print("subscribe success") end) -- 向"/topic"标题发送消息,消息设置:data = hello, QoS = 0, retain = 0 m:publish("/topic","hello",0,0, function(conn) print("sent") end) m:close(); -- 或者可以再次调用m:connect()
####参见 -
#mqtt client子模块
mqtt:lwt()
####描述 设置Last Will和Testament (可选)
如果mqtt client不发送保持连接包,服务器会向标题"/lwt"发送一个qos = 0, retain = 0, data = "offline"的消息.
####语法 mqtt:lwt(topic, message, qos, retain)
####参数 topic: 需要发布消息的标题,字符串类型.
message: 需要发布的消息, Buffer或者字符串.
qos: qos值, 默认值为0.
retain: 保留标志,默认值为0.
####返回值 nil.
####示例
####参见 -
mqtt:connect()
####描述 连接到mqtt服务器.
####语法 mqtt:connect( host, port, secure, function(client) )
####参数 host: 主机域名或者ip地址,字符串类型.
port: 服务器端口号.
secure: 0 或者 1, 默认值为0.
function(client): 连接成功的回调函数.
####返回值 nil.
####示例
####参见 -
mqtt:close()
####描述 关闭mqtt连接.
####语法 mqtt:close()
####参数 nil
####返回值 nil.
####示例
####参见 -
mqtt:publish()
####描述 发布一个消息.
####语法 mqtt:publish( topic, payload, qos, retain, function(client) )
####参数 topic: 需要发布消息的标题, 字符串类型.
message: 需要发布的消息, 字符串类型.
qos: qos值, 默认值为0.
retain: 保留标志, 默认值为0.
function(client): 发送成功的回调函数,如果接收到PUBACK回调函数解除.
####返回值 nil.
####示例
####参见 -
mqtt:subscribe()
####描述 订阅一个或者多个标题的消息.
####语法 mqtt:subscribe(topic, qos, function(client, topic, message))
####参数 topic: 需要订阅消息的标题.
qos: 订阅消息的qos值, 默认值为0
function(client, topic, message): 接收消息的回调函数,接收后即解除.
####返回值 nil.
####示例
####参见 -
mqtt:on()
####描述 注册mqtt事件的回调函数.
####语法 mqtt:on(event, function(client, [topic], [message]))
####参数 event: 字符串,取值为: "connect", "message", "offline"
function cb(client, [topic], [message]): 事件触发的回调函数. 第一个参数是mqtt client.
如果事件是"message", 第二个和第三个参数分别是标题和消息内容,字符串类型.
####返回值 nil.
####示例