1 -- LuaTools需要PROJECT和VERSION这两个信息
6 本demo需要mqtt库, 大部分能联网的设备都具有这个库
9 本demo演示的是 OneNet Studio, 注意区分
10 https://open.iot.10086.cn/studio/summary
11 https://open.iot.10086.cn/doc/v5/develop/detail/iot_platform
15 _G
.sys
= require("sys")
16 --[[特别注意, 使用mqtt库需要下列语句]]
17 _G
.sysplus
= require("sysplus")
20 -- Air780E的AT固件默认会为开机键防抖, 导致部分用户刷机很麻烦
21 if rtos
.bsp() == "EC618" and pm
and pm
.PWK_MODE
then
22 pm
.power(pm
.PWK_MODE
, false)
26 ----------------------------------------------
28 mqtt_host
= "mqtts.heclouds.com"
31 local pid
= "KzUSNA8BPh" -- 产品id
32 local device
= "abcdef" -- 设备名称, 按需设置, 如果是Cat.1系列通常用mobile.imei()
33 local device_secret
= "21BhRAyik1mGCWoFNMFj+INgCr7QZw/pDTXnHpRaf3U=" -- 设备密钥
34 client_id
, user_name
, password
= iotauth
.onenet(pid
, device
, device_secret
)
36 -- 下面是常用的topic, 完整topic可参考 https://open.iot.10086.cn/doc/v5/develop/detail/639
37 pub_topic
= "$sys/" .. pid
.. "/" .. device
.. "/thing/property/post"
38 sub_topic
= "$sys/" .. pid
.. "/" .. device
.. "/thing/property/set"
39 pub_custome
= "$sys/" .. pid
.. "/" .. device
.. "/custome/up"
40 pub_custome_reply
= "$sys/" .. pid
.. "/" .. device
.. "/custome/up_reply"
41 sub_custome
= "$sys/" .. pid
.. "/" .. device
.. "/custome/down/+"
42 sub_custome_reply
= "$sys/" .. pid
.. "/" .. device
.. "/custome/down_reply/"
43 ------------------------------------------------
51 -- sys.timerLoopStart(wdt.feed, 3000)
54 function on_downlink(topic
, payload
)
57 -- 方式一, 将数据publish出去, 在另外一个sys.subscribe内处理
58 sys
.publish("mqtt_payload", data
, payload
)
60 -- 方式二, 将数据加入队列, 然后通知task处理
61 -- table.insert(payloads, {topic, payload})
64 if payload
.startsWith("{") and payload
.endsWith("}") then
66 local jdata
= json
.decode(payload
)
69 if topic
== sub_topic
then
70 local params
= jdata
.params
72 local power_switch
= jdata
.params
.power_switch
74 if power_switch
== 1 then
78 log.info("led", "off")
90 sys
.taskInit(function()
94 log.info("bsp", "本固件未包含mqtt库, 请查证")
97 local device_id
= mcu
.unique_id():toHex()
98 -----------------------------
100 ----------------------------
101 if wlan
and wlan
.connect
then
102 -- wifi 联网, ESP32系列均支持
103 local ssid
= "luatos1234"
104 local password
= "12341234"
105 log.info("wifi", ssid
, password
)
108 -- LED = gpio.setup(12, 0, gpio.PULLUP)
110 wlan
.setMode(wlan
.STATION
) -- 默认也是这个模式,不调用也可以
111 device_id
= wlan
.getMac()
112 wlan
.connect(ssid
, password
, 1)
115 -- mobile.simid(2) -- 自动切换SIM卡
116 LED
= gpio
.setup(27, 0, gpio
.PULLUP
)
117 device_id
= mobile
.imei()
119 -- w5500 以太网, 当前仅Air105支持
120 w5500
.init(spi
.HSPI_0
, 24000000, pin
.PC14
, pin
.PC01
, pin
.PC00
)
121 w5500
.config() -- 默认是DHCP模式
122 w5500
.bind(socket
.ETH0
)
123 LED
= gpio
.setup(62, 0, gpio
.PULLUP
)
128 -- 其他不认识的bsp, 循环提示一下吧
131 log.info("bsp", "本bsp可能未适配网络层, 请查证")
135 sys
.waitUntil("IP_READY")
136 sys
.publish("net_ready", device_id
)
139 sys
.taskInit(function()
141 sys
.waitUntil("net_ready")
143 -- 打印一下上报(pub)和下发(sub)的topic名称
147 log.info("mqtt", "pub", pub_topic
)
148 log.info("mqtt", "sub", sub_topic
)
149 log.info("mqtt", mqtt_host
, mqtt_port
, client_id
, user_name
, password
)
151 -------------------------------------
152 -------- MQTT 演示代码 --------------
153 -------------------------------------
155 mqttc
= mqtt
.create(nil, mqtt_host
, mqtt_port
, mqtt_isssl
, ca_file
)
157 mqttc
:auth(client_id
, user_name
, password
) -- client_id必填,其余选填
158 -- mqttc:keepalive(240) -- 默认值240s
159 mqttc
:autoreconn(true, 3000) -- 自动重连机制
161 mqttc
:on(function(mqtt_client
, event
, data
, payload
)
163 log.info("mqtt", "event", event
, mqtt_client
, data
, payload
)
164 if event
== "conack" then
166 sys
.publish("mqtt_conack")
169 topics
[sub_topic
] = 2
173 if pub_custome_reply
then
174 topics
[pub_custome_reply
] = 1
178 topics
[sub_custome
] = 1
180 -- mqtt_client:subscribe(sub_topic, 2)--单主题订阅
181 mqtt_client
:subscribe(topics
) -- 多主题订阅
182 elseif event
== "recv" then
183 -- 打印收到的内容, 时间生产环境建议注释掉, 不然挺多的
184 log.info("mqtt", "downlink", "topic", data
, "payload", payload
)
185 on_downlink(data
, payload
)
186 elseif event
== "sent" then
187 log.info("mqtt", "sent", "pkgid", data
)
188 -- elseif event == "disconnect" then
190 -- mqtt_client:connect()
194 -- mqttc自动处理重连, 除非自行关闭
196 sys
.waitUntil("mqtt_conack")
198 -- 演示等待其他task发送过来的上报信息
199 local ret
, topic
, data
, qos
= sys
.waitUntil("mqtt_pub", 300000)
201 -- 提供关闭本while循环的途径, 不需要可以注释掉
202 if topic
== "close" then
205 mqttc
:publish(topic
, data
, qos
)
207 -- 如果没有其他task上报, 可以写个空等待
208 -- sys.wait(60000000)
214 -- 这里演示在另一个task里上报数据, 会定时上报数据,不需要就注释掉
215 sys
.taskInit(function()
216 local qos
= 1 -- QOS0不带puback, QOS1是带puback的
219 log.info("准备发布数据", mqttc
and mqttc
:ready())
220 -- onenet 使用的是OneJson, 就是规范化的Json结构
221 -- https://open.iot.10086.cn/doc/v5/develop/detail/508
222 if mqttc
and mqttc
:ready() then
224 data
["id"] = tostring(mcu
.ticks())
228 -- 上传一个Power属性, 类型是整型, 值为1
229 data
["params"]["Power"] = {
232 -- 上传一个WF属性, 类型浮点, 值为2.35
233 data
["params"]["Power2"] = {
237 data
["params"]["Power3"] = {
241 -- ==========================================================
242 -- 鉴于onenet支持定位功能, 这里增加基站数据数据的上报
243 -- 下面的基站定位与合宙提供的基站定位是不同的, 按需选用
244 -- 每日300w配额, 是通过额外的HTTP API获取坐标的 https://open.iot.10086.cn/doc/v5/develop/detail/722
247 local infos
= mobile
.getCellInfo()
250 for _
, v
in pairs(infos
) do
256 networkType
= 5, -- LTE,
260 table.insert(LBS
, cell
)
262 data
["params"]["$OneNET_LBS"] = {
267 -- 还有wifi定位,但wifi联网设备才有
268 if wlan
and wlan
.connect
and wlan
.ready() then
270 macs
= "FC:D7:33:55:92:6A,-77|B8:F8:83:E6:24:DF,-60"
272 data
["params"]["$OneNET_LBS_WIFI"] = {
276 -- ==========================================================
277 local updata
= json
.encode(data
)
278 log.info("mqtt", "待上报数据", updata
)
279 local pkgid
= mqttc
:publish(pub_topic
, updata
, qos
)
285 -- -- ======================================================================
286 -- -- -- 以下是演示与uart结合, 简单的mqtt-uart透传实现,不需要就注释掉
288 -- uart.setup(uart_id, 9600)
289 -- uart.on(uart_id, "receive", function(id, len)
292 -- local tmp = uart.read(uart_id)
293 -- if not tmp or #tmp == 0 then
296 -- data = data .. tmp
298 -- log.info("uart", "uart收到数据长度", #data)
299 -- sys.publish("mqtt_pub", pub_custome, data)
301 -- sys.subscribe("mqtt_payload", function(topic, payload)
302 -- if topic == sub_custome then
303 -- log.info("uart", "uart发送数据长度", #payload)
304 -- uart.write(1, payload)
307 -- ======================================================================
309 -- 用户代码已结束---------------------------------------------
312 -- sys.run()之后后面不要加任何语句!!!!!