Esp8266 Lua语言Api方法使用

定时器 timer

函数名参数返回值功能
tmr.create()nonetimer object创建一个动态定时器对象
tmr.softwd()timeout_snil提供一个简单的软件看门狗,需要在它到期之前重新武装或禁用,否则系统将重新启动。
tmr.wdclr()nilnil清除看门狗(喂狗)
tmr.time()noneseconds系统正常运行时间,以秒为单位,可能是绕圈运行
tmr.now()nilnumber系统计数器函数
tmr.delay()usnil延时函数
tmr.ccount()nilnumber获取CPU计数寄存器的值

最新的固件在使用timer定时器时,需要先定义

函数名参数返回值功能
Timer ObjectMethods
tobj:interval()interval_msnil更改已注册计时器的过期时间间隔
tobj:alarm()interval_ms, mode, func布尔值周期函数
tobj:register()interval_ms, mode, func()nil配置一个计时器,并注册要在到期时调用的回调函数。
tobj:start()
tobj:stop()停止一个正在运行的计时器,但不取消它的注册。
tobj:unregister()Nonenil停止计时器(如果正在运行)并注销关联的回调。

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, pullupnil设置io口模式
gpio.write()pin,levelnil设置io口电平高低
gpio.read()pin0 or 1读取io口电平高低
gpio.trig()pin, type, function(level)nil外部中断函数

image-20210723135828521

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]niluart 的事件回调函数
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,passwordnil设置 station 模式下的ssid 和 password
wifi.sta.connect()nilnilstation 模式下连接 AP
wifi.sta.getip()nilip or nil返回自己的ip地址
wifi.ap.config(cfg)cfg: 设置 AP 的 lua tablenil设置 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, timeoutnet.server 子模块创建一个 server
net.createConnection(type, secure)type, securenet.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")  --向服务器发送数据
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Crazy_August

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值