O_嵌入式专题目录
ImgT1Task.lua
module(..., package.seeall)
require "pins"
require "http"
require "mqtt"
require "misc"
local sdMountFlag = 0
local sendFlag = 1
local mqttSubscribeFlag = 0
local doorState = 0 --0:Close 1:Open
local host, port = "bemfa.com", 9501
local lastDoorState = doorState
local mqttc = mqtt.client("7d54f85af42976ee3c2693e6xxxxxxxx", 300, "", "") --user/passwd
local imgUrl = ""
function gpio19IntFnc(msg)
if msg==cpu.INT_GPIO_POSEDGE then
log.info("ImgT1Task.gpio19IntFnc","Up!!!!!")
else
--下降沿中断
log.info("ImgT1Task.gpio19IntFnc","Down!!!!!")
sendFlag = 0
end
end
getGpio19Fnc = pins.setup(pio.P0_19,gpio19IntFnc)
function gpio18IntFnc(msg)
if msg==cpu.INT_GPIO_POSEDGE then
log.info("ImgT1Task.gpio18IntFnc","Up!!!!!")
doorState = 1
else
log.info("ImgT1Task.gpio18IntFnc","Down!!!!!")
doorState = 0
end
end
getGpio18Fnc = pins.setup(pio.P0_18,gpio18IntFnc,pio.PULLUP)
local function cbFnc(result,prompt,head,body) --socket回调函数
log.info("ImgT1Task.cbFnc","result prompt:",result,prompt)
if result and head then
for k,v in pairs(head) do
log.info("ImgT1Task.cbFnc",k..": "..v)
end
end
if result and body then
log.info("ImgT1Task.cbFnc","BodyLen:"..body:len())
log.info("ImgT1Task.cbFnc","Body:"..body)
if prompt == "200" then
log.info("ImgT1Task.cbFnc","Format url!!!!!")
local tjsondata,tresult,errinfo = json.decode(body)
if tresult and type(tjsondata)=="table" then
imgUrl = tjsondata["url"]
log.info("ImgT1Task.decode url",imgUrl)
else
log.info("ImgT1Task.decode error",errinfo)
end
end
end
end
function SendImg()
if sdMountFlag == 0 then --加载SD卡
if io.mount(io.SDCARD) then
sdMountFlag = 1
log.info("ImgT1Task.sendImg","Mount SD success!!!!!")
log.info("ImgT1Task.sendImg","SD size: "..rtos.get_fs_free_size(1,1).." KB")
else
log.error("ImgT1Task.sendImg","Mount SD fail!!!!!")
end
end
if sendFlag == 0 then
if mqttSubscribeFlag == 1 then
log.info("People!!!!!")
mqttc:publish("luatT1","Door-P",1)
end
local filehandle = io.open("/sdcard0/0/1.jpg", "r")
if filehandle then --打开文件并发送
log.info("ImgT1Task.sendImg","File exist!!!!!")
filehandle:close()
if socket.isReady() then
http.request("POST",
"http://images.bemfa.com/upload/v1/upimages.php",
nil,
{
['Content-Type']="image/jpg",
['Authorization']="7d54f85af42976ee3c2693e6xxxxxxxx",
['Authtopic']="esp32img",
--['Wechatmsg']="Pic come!",
},
{{file="/sdcard0/0/1.jpg"}},
30000,
cbFnc,
nil)
sendFlag = 1
end
else
log.error("ImgT1Task.sendImg","File not exist or wrog format!!!!!")
end
end
end
sys.taskInit(function()
while true do
if mqttSubscribeFlag == 0 then --mqtt订阅未连接
while not socket.isReady() do sys.wait(1000) end --检查socket状态
log.info("ImgT1Task.MQTT","Sock is ready!!!!!")
while not mqttc:connect(host, port) do sys.wait(2000) end --检查mqtt连接
log.info("ImgT1Task.MQTT","MQTT connected!!!!!")
if mqttc:subscribe("luatT1") then --mqtt订阅
log.info("ImgT1Task.MQTT","MQTT subscribe success!!!!!")
if mqttc:publish("luatT1","Air724ugT1",1) then
mqttSubscribeFlag = 1
end
end
else --mqtt订阅已连接
while true do
local r, data, param = mqttc:receive(2000, "luatT1")
if r then --收到回复报文
log.info("ImgT1Task.MQTT","这是收到了服务器下发的消息:", data.payload or "nil")
if data.payload then --发送机器状态
if data.payload == "lockCheck" then
mqttc:publish("luatT1","luatT1-Online",1)
end
if data.payload == "Door-O" then
log.info("Open door!!!!!")
mqttc:publish("luatT1","Door-1",1)
end
if data.payload == "Door-C" then
log.info("Close door!!!!!")
mqttc:publish("luatT1","Door-0",1)
end
end
elseif data == "luatT1" then
log.info("ImgT1Task.MQTT","这是收到了订阅的消息和参数显示:", data, param)
mqttc:publish("luatT1",string.format("Air724ugT1 response "..param),1)
elseif data == "timeout" then
log.info("ImgT1Task.MQTT","Still waiting!!!!!")
--mqttc:publish("luatT1",string.format("Air724ugT1-"..os.time()),1)
else
log.info("ImgT1Task.MQTT","网络链接被断开")
break
end
log.info("ImgT1Task.MQTT","L:"..lastDoorState,"C:"..doorState)
if lastDoorState ~= doorState then
lastDoorState = doorState
log.info("ImgT1Task.MQTT","Current door state:"..lastDoorState)
mqttc:publish("luatT1",string.format("Door-"..lastDoorState,os.time()),1)
end
SendImg() --发送图片
if imgUrl ~= "" then --获取返回图片地址
log.info("ImgT1Task.cbFnc","Publish url!!!!!")
if mqttc:publish("luatT1",string.format("imgUrl:"..imgUrl),1) then
imgUrl = ""
end
end
end
end
--mqttc:disconnect()
--sys.wait(1000)
end
end)
main.lua
PROJECT = "IMGT1"
VERSION = "0.0.1"
require "log"
LOG_LEVEL = log.LOGLEVEL_TRACE
--LOG_LEVEL = log.LOGLEVEL_SILENT
require "sys"
require "net"
net.startQueryAll(60000, 60000)
require "netLed"
pmd.ldoset(2,pmd.LDO_VLCD)
netLed.setup(true,pio.P0_1,pio.P0_4)
--PRODUCT_KEY = "v32xEAKsGTIEQxtqgwCldp5aPlcnPs3K"
--require "update"
--update.request()
require "ImgT1Task"
sys.init(0, 0)
sys.run()
评论已关闭