O_Air724UG_Luat巴法云MQTT图传测试


O_嵌入式专题目录


ImgT1Task.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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()