on_message 函数正在接收未发送的值

on_message function is receiving values that are not being sent

我如何接收 MQTT 并将其传递到我设置的 bash 脚本?我收到的值在我发送的消息和收到消息之间不断增加,我无法判断它是否已执行 bash 脚本并且它不会打印其后的行以进行检查,所以我怀疑之前出了什么问题。

它打印的最后一行是:

voltage = "voltage = %s" %volts
print(voltage)

我已经尝试将代码放入 on_message 函数中以使用 MQTT 消息制作 bash 脚本。

我在终端中有 运行 bash 脚本以确保其功能正常。 我创建了一个 python 脚本,我在调用脚本时手动输入了值并且它起作用了。我基本上把这个脚本放在 on_message 函数中。


#! /usr/bin/python
import os, time, sys, datetime, paho.mqtt.client as mqtt
print("Starting script")
#while True:
def on_connect(client, userdata, flags, rc):
        print("Connected with result code "+str(rc))
        client.subscribe("$SYS/#")



def on_message(client, userdata,message):
        print("message received")
        split = str(message.payload.decode("utf-8"))
        value = str(datetime.datetime.now())
        value = """  "%s" """ %value
        split = split.split(',')
        print(split)
        num = str(982123456)
        RFID = "RFID = %s" %num
        start = "Super secret start sequence"
        username = """ "Secret name" """
        password = """ "Secret password" """
        volts = split[0]
        voltage = "voltage = %s" %volts
        print(voltage)
        if split[2] == "None":
                lat = 22
                long = 141
                print("IF NONE")
        else:
                lat = split[2]
                long = split[3]
                print("IF SOMEThING")
        os.system(start +  RFID + "," +  "DateTime = %s" %value + "," + "lat =" + lat + "," + "long =" + long + "," + voltage +  "," + "username = " +  username  + "," + "password = " + password + ")'" )
        print(start +  RFID + "," +  "DateTime = %s" %value + "," + "lat =" + lat + "," + "long =" + long + "," + voltage +  "," + "username = " +  username  + "," + "password = " + password + ")'" )
        print(split)
client= mqtt.Client("demo",clean_session=True, userdata=None, transport="tcp")
#       print("Connecting...")
client.connect("localhost")
#client.loop_start()
client.subscribe("test")
#       print("subscribed")
print("Checker")
client.on_message=on_message
print("Client Message:")
print(client.on_message)
client.on_connect=on_connect
#       print("loop")
client.loop_forever()
#       time.sleep(0.1)

我预计:

message received
['4.69172', 'None', 'None', 'None']
voltage = 4.69172
*response from bash script*

但我得到:

Connected with result code 0
message received
['mosquitto version 1.4.15']
voltage = mosquitto version 1.4.15
message received
['Wed', ' 13 Feb 2019 00:27:01 +0000']
voltage = Wed
message received
['3626777 seconds']
voltage = 3626777 seconds
message received
['4']
voltage = 4
message received
['6']
voltage = 6
message received
['2']
voltage = 2
message received
['2']
voltage = 2
message received
['2']
voltage = 2
message received
['2']
voltage = 2
message received
['0']
voltage = 0
message received
['49']
voltage = 49
message received
['6762187']
voltage = 6762187
message received
['10594382']
voltage = 10594382

您订阅了多个主题。

首先,您订阅了主题 test,我认为该主题具有您期望的数据。

但在 on_connect 函数中,您还订阅了 $SYS/#,这将 return 根据 MQTT 代理的内部状态(例如正常运行时间、已连接的客户端、发送或接收的消息数。如果您删除此行,您应该只会收到您期望的消息。