说明: 在这个项目中,AIR780E模块使用MQTT协议,用于将传感器数据上传到云服务器。设备作为MQTT终端发布消息到服务器上的一个主题,其他客户端(如小程序或移动说明)可以订阅这个主题以接收数据。关键部分代码:PROJECT = "mqttdemo"
VERSION = "1.0.0"
PROJECT = "gnsstest"
VERSION = "1.0.1"-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "adcdemo"
_G.sys = require("sys")
_G.sysplus = require("sysplus")
local mpu6xxx = require "mpu6xxx"
i2cid = 0i2c_speed = i2c.FASTlocal
mqtt_host = "www.visionexpand.com.cn"
local mqtt_port = 1883local
mqtt_isssl = falselocal
client_id = "emqx"
local pub_topic = "$thing/up/property/IQMPOB8BI9/temp/humi"
local sub_topic = "mqtt"
local uart2_data = "text"
local adc_pin_1=1local data_pub={ }-- libgnss库初始化
libgnss.clear() -- 清空数据,兼初始化
if wdt then wdt.init(9000) sys.timerLoopStart(wdt.feed, 3000)
endlocal uartid = 2
local result = uart.setup( uartid, 115200, 8, 1)
sys.taskInit(function()
if rtos.bsp() == "AIR780E" then device_id = mobile.imei() sys.waitUntil("IP_READY", 30000)
pub_topic = "$thing/up/property/IQMPOB8BI9/temp/humi" sub_topic = "mqtt"
end log.info("mqtt", "pub", pub_topic) log.info("mqtt", "sub", sub_topic)
local mqttc = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl, nil)
mqttc:auth(client_id, nil, nil) mqttc:autoreconn(true, 3000) mqttc:on(function(mqtt_client, event, data, payload)
if event == "conack"
then sys.publish("mqtt_conack") mqtt_client:subscribe(sub_topic)
elseif event == "recv"
then log.info("mqtt", "received", "topic", data, "payload", payload)
elseif event == "sent"
then log.info("mqtt", "sent", "pkgid", data)
end
end)
mqttc:connect()
sys.waitUntil("mqtt_conack")