定时器 timer
函数名 | 参数 | 返回值 | 功能 |
---|---|---|---|
tmr.create() | none | timer object | 创建一个动态定时器对象 |
tmr.softwd() | timeout_s | nil | 提供一个简单的软件看门狗,需要在它到期之前重新武装或禁用,否则系统将重新启动。 |
tmr.wdclr() | nil | nil | 清除看门狗(喂狗) |
tmr.time() | none | seconds | 系统正常运行时间,以秒为单位,可能是绕圈运行 |
tmr.now() | nil | number | 系统计数器函数 |
tmr.delay() | us | nil | 延时函数 |
tmr.ccount() | nil | number | 获取CPU计数寄存器的值 |
最新的固件在使用timer定时器时,需要先定义
函数名 | 参数 | 返回值 | 功能 |
---|---|---|---|
Timer Object | Methods | ||
tobj:interval() | interval_ms | nil | 更改已注册计时器的过期时间间隔 |
tobj:alarm() | interval_ms, mode, func | 布尔值 | 周期函数 |
tobj:register() | interval_ms, mode, func() | nil | 配置一个计时器,并注册要在到期时调用的回调函数。 |
tobj:start() | |||
tobj:stop() | 停止一个正在运行的计时器,但不取消它的注册。 | ||
tobj:unregister() | None | nil | 停止计时器(如果正在运行)并注销关联的回调。 |
timer函数说明
1.1 tmr.alarm()
周期函数,也叫闹钟函数,参数说明如下
tobj:alarm()(interval_ms, mode, function(dosomething) )
interval_ms:
——number:定时的时间,单位为毫秒
mode:
tmr.ALARM_SINGLE aone-shot alarm (and no need to call unregister()) (一次)
tmr.ALARM_SEMI manually repeating alarm (call start() to restart) (手动)
tmr.ALARM_AUTO automatically repeating alarm (自动重复)function():
——函数名:时间到了需要做的函数
if not tmr.create():alarm(5000, tmr.ALARM_SINGLE, function() print("hey there") end) then print("whoopsie") end
1.2 tobj:register()
通常搭配mytimer:start()函数使用
mytimer = tmr.create() mytimer:register(5000, tmr.ALARM_SINGLE, function() print("hey there") end) mytimer:start()
GPIO模块
函数名 | 参数 | 返回值 | 功能 |
---|---|---|---|
gpio.mode() | pin, mode, pullup | nil | 设置io口模式 |
gpio.write() | pin,level | nil | 设置io口电平高低 |
gpio.read() | pin | 0 or 1 | 读取io口电平高低 |
gpio.trig() | pin, type, function(level) | nil | 外部中断函数 |
gpio函数说明
2.1.1
gpio.mode(pin, mode, pullup)
pin:
——number,0~12,io口标号
mode:
——gpio.OUTPUT(输出模式)
——gpio.INPUT(输入模式)
——gpio.INT(中断模式)
pullup:(可选参数)
——gpio.PULLUP(上拉模式)
——gpio.FLOAT(浮动模式,默认为浮动)2.1.2
gpio.write(pin,level)
pin:
——number,0~12,io口标号
level:
——gpio.HIGH(输出高电平)
——gpio.LOW(输出低电平)2.1.3
gpio.read(pin)
pin:
——number,0~12,io口标号
2.1.4
gpio.trig(pin, type, function(level))
pin:
——number,0~12,io口标号
type: 触发方式
——up: 上升沿触发
——down:下降沿触发
——both:双边沿触发
——low:低电平触发
——high:高电平触发
function(level):中断回掉函数,如果省略则默认调用上次调用的函数
——level:中断时,io口的电平高低,低电平为0
,高电平为1
pin = 7 time = 0 gpio.mode(pin,gpio.INT,gpio.PULLUP) function fun(level) if level == 1 then print(tmr.now()-time) gpio.trig(pin , "down") else time = tmr.now() gpio.trig(pin , "up") end end gpio.trig(pin, "down",fun)
UART 串口模块
函数名 | 参数 | 返回值 | 功能 |
---|---|---|---|
uart.setup() | id, baud, databits, parity, stopbits, echo | 返回波特率 | 串口设置 |
uart.on() | method, function, [run_input] | nil | uart 的事件回调函数 |
uart.write( ) | id, string1, string2… | nil | 串口输出 |
串口函数说明
3.1.1
uart.setup(id, baud, databits, parity, stopbits, echo)
id = 0, 只支持一个串口
baud 为波特率
databits = 5, 6, 7, 8。表示字节长度
parity = 0(none)。
stopbits = 1(1 stopbit), 2(2 stopbit).
echo = 0(关闭回显)。
uart.setup(0, 115200, 8, 0, 1, 1 ) 此时的波特率为115200,改变波特率就改变这一项即可,其他的不要变, 注意:波特率设置一定要写在代码的最后,不然会出错。
3.1.2
uart.on(method, function, [run_input])
method = “data”, 表示 uart 接收到了数据
function 为回调函数, “data” 的回调函数签名为 function(data) end
run_input: 0 或 1, 0 表示从 uart 输入的 data 不经过 lua 解释器执行, 1 表示输
入的行会被送到 lua 解释器执行。uart.on("data", function(data) print("receive from uart:", data) if data=="quit" then uart.on("data") end end , 0)
3.1.3
uart.write( id, string1, string2… )
id = 0, 只支持一个串口
string1:需要写入的字符串。
一般都用print()函数代替此函数
WiFi 模块
函数名 | 参数 | 返回值 | 功能 |
---|---|---|---|
wifi.setmode(mode) | mode | 返回设置之后的 mode 值 | 设置 wifi 的工作模式 |
wifi.sta.config(ssid, password) | ssid,password | nil | 设置 station 模式下的ssid 和 password |
wifi.sta.connect() | nil | nil | station 模式下连接 AP |
wifi.sta.getip() | nil | ip or nil | 返回自己的ip地址 |
wifi.ap.config(cfg) | cfg: 设置 AP 的 lua table | nil | 设置 ap 模式下的 ssid 和 password |
WiFi模块函数说明
4.1.1
wifi.setmode(mode)
参数:mode: 取值为: wifi.STATION(连接模式), wifi.SOFTAP(热点模式) wifi.STATIONAP(二者共存)
4.1.2
wifi.sta.config(ssid, password)
方法: (1) wifi.sta.config("myssid","mypassword") (2) cfg={} cfg.ssid="myssid" cfg.pwd="mypwd" wifi.sta.config(cfg)
4.1.3
wifi.sta.connect()
配置好要连接的wifi后使用次函数启动连接 wifi.sta.connect()
4.1.4
wifi.sta.getip()
连接成功后返回自己的ip地址,否则返回nil 下面是一个完整示例 wifi.setmode(wifi.STATION) wifi.sta.config("账号","密码") wifi.sta.connect() tmr.alarm(1, 1000, 1, function() if wifi.sta.getip()== nil then print("IP unavaiable, Waiting...") else tmr.stop(1) print("Config done, IP is "..wifi.sta.getip()) end end)
4.1.5
wifi.ap.config(cfg)
cfg={} cfg.ssid="myssid" cfg.pwd="mypwd" wifi.ap.config(cfg)
wifi.ap.config(ssid, ‘password’)
Net模块
函数名 | 参数 | 返回值 | 功能 |
---|---|---|---|
net.createServer(type, timeout) | type, timeout | net.server 子模块 | 创建一个 server |
net.createConnection(type, secure) | type, secure | net.server 子模块 | 创建一个 client |
1.net.server 子模块
服务器模式独有的函数
语法: net.server.listen(port,[ip],function(net.socket))
listen(port)
功能:侦听指定 ip 地址的端口
port: 端口号
ip:ip 地址字符串,可以省略
function(net.socket):连接创建成功的回调函数,可以作为参数:传给调用函数.
2.net.socket 子模块
客户端模式独有函数
语法: connect(port, ip/domain)
功能: 连接至远端
port: 端口号
ip: ip 地址或者是域名字符串
共有的函数
语法: send(string, function(sent))
send(string)
功能:通过连接向远端发送数据
string: 待发送的字符串
function(sent): 发送字符串后的回调函数
语法: net.server.close()
close()
功能:关闭 server
语法: on(event, function(c))
功能:向事件注册回调函数
event: 字符串,取值为: "connection", 连接成功时
"reconnection",重新连接成功时
"disconnection",连接断开时
"receive",接收到消息时
"sent",发送消息时
function (net.socket, [string]): 回调函数。第一个参数:是 socket.如果事件是"receive", 第二个参数:则为接收到的字符串。
>
例如:
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: keepalive\r\nAccept: */*\r\n\r\n")
TCP通信方式
Net模块函数说明
5.1.1
net.createServer(type, timeout):服务器模式
5.1.2
net.createConnection(type, secure):客户端模式
type: 取值为: net.TCP 或者 net.UDP
timeout: 1~28800, 当为 tcp 服务器时,客户端的超时时间设置
secure: 设置为 1 或者 0, 1 代表安全连接, 0 代表普通连接
服务器例子
sv = net.createServer(net.TCP, 30) --创建一个服务器,客户端连接后30s内没有通信自动断开客户端 sv:listen(80,function(c) --监听80端口,当有客户端连接后,创建客户端对象c c:on("receive", function(c, msg) --当接收到客户端发送过来的消息触发此函数 print(msg)--打印接收到的消息 end) c:send("hello world") --向客户端发送消息“hello world” c:close() --断开客户端的连接 end)
客户端例子
sk = net.createConnection(net.TCP, 0) --创捷一个tcp客户端 sk:connect(80,"192.168.1.1") --连接IP地址和端口80 sk:on("receive", function(sv, c) --当接收到服务器发送过来的消息是触发此函数 print(c)--打印接收到的消息 end ) sk:send("hello world") --向服务器发送数据