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

时间:2019-07-08 04:38:43

标签: python mqtt publish-subscribe paho

如何接收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

1 个答案:

答案 0 :(得分:0)

您已订阅多个主题。

首先,您订阅主题test,我认为该主题包含您期望的数据。

但是在on_connect函数中,您还订阅了$SYS/#,它将根据MQTT代理的内部状态返回许多不同的消息,例如正常运行时间,已连接的客户端数量,消息数量。发送或接收。如果删除此行,则只会收到您期望的消息。