- 通过串口发送指令控制继电器, 掉电记忆继电器状态,再次上电恢复继电器状态。
- 通过串口发送"open"打开继电器, 发送"close"关闭继电器 ,并把继电器状态写入mode.lua文件; 发送"rest"系统软件复位
- 测试通过串口发送"open"打开继电器后, 点击板子的复位按键, 系统复位, 松开后程序会获取断电前的继电器状态,恢复继电器状态
init.lua文件
RELAY_Stat = ""
RELAY_Pin = 1
gpio.mode(RELAY_Pin, gpio.OUTPUT)
if file.open("mode.lua", "r") then
RELAY_Stat = file.readline()
print(RELAY_Stat)
file.close()
end
if string.find(RELAY_Stat,"RELAY=1",1) then
gpio.write(RELAY_Pin,1)
elseif string.find(RELAY_Stat,"RELAY=0",1) then
gpio.write(RELAY_Pin,0)
end
tmr.alarm(0,4000,0,function()
dofile("file.lua")
end)
file.lua文件
UartReceCnt = 0
UartReceTempCnt = 0
UartReceData = ""
uart.on("data",0,function(data)
UartReceData = UartReceData..data
UartReceCnt = UartReceCnt + 1
end,0)
tmr.alarm(1,10,1,function()
if UartReceCnt ~= 0 then
if UartReceTempCnt == UartReceCnt then
UartReceCnt = 0
UartReceTempCnt = 0
if UartReceData == "open" then
gpio.write(RELAY_Pin,1)
if file.open("mode.lua", "w+") then
file.writeline("RELAY=1")
file.flush()
else
print("open mode.lua faild")
end
file.close()
elseif UartReceData == "close" then
gpio.write(RELAY_Pin,0)
if file.open("mode.lua", "w+") then
file.writeline("RELAY=0")
file.flush()
else
print("open mode.lua faild")
end
file.close()
elseif UartReceData == "rest" then
node.restart()
end
uart.write(0,UartReceData)
UartReceData = ""
UartReceCnt = 0
else
UartReceTempCnt = UartReceCnt
end
end
end)
演示视频:https://v.youku.com/v_show/id_XMzkxMTAyOTI0MA==.html?spm=a2h0j.11185381.listitem_page1.5~A