Lua: 同时发布不同传感器数据到主题

我能够使用 Lua 代码逐个地发布不同传感器的数据(如温度值和运动检测值)到一个特定主题(topic/temp/motion),并在我的 Android 应用程序上进行订阅,但我不能同时发布到同一主题或子主题。希望能得到一些想法或示例。 以下是使用的一些主要 Lua 代码片段。

orgID = "quickstart" -- IoT Foundation organization ID
broker = "test.mosquitto.org"   --orgID..".messaging.internetofthings.ibmcloud.com" -- IoTF 服务的 IP 或主机名
mqttPort = 1883 -- MQTT 端口(默认为 1883:非安全)
userID = "" -- 用于起步的空白
userPWD = "" -- 用于起步的空白
macID = "18fe34e1b007" -- 唯一设备 ID 或以太网 Mac 地址 <==== 修改此项!
clientID = ":esp8266:18fe34e1b007" -- 客户端 ID
count = 0 -- mqtt_do 周期测试次数
mqttState = 0 -- 状态控制
topic = "topic/temp/motion"
led = 4
--gpio.mode(led,gpio.OUTPUT)
--dht 传感器设置------------------------------
 pin = 1
-- PIR 初始化部分
pir = 2
x= 0 -- 用于以“0”或“1”形式发送运动检测信息的变量

function DHT_do()
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
--gpio.write(led, gpio.LOW)
end

 function mqtt_do()
 count = count + 1 -- tmr.alarm 计数器
------------------------------------------- pir 条件代码
if gpio.read(pir) ~= last_state then
        last_state = gpio.read(pir)
        if last_state == 1 then
        print("ON")
        x = 1
        gpio.write(led,gpio.HIGH)
        else
        print("OFF")
        x = 0
       gpio.write(led,gpio.LOW)

m:publish(topic,x, 0, 0,
 function(conn)
  print(x)
  print("temp_data:"..temp)
点赞