Сравните предыдущее и текущее опубликованное значение по теме MQTT - PullRequest
0 голосов
/ 23 октября 2018

Мой вопрос, как сравнить текущее опубликованное значение с предыдущим опубликованным значением для каждой темы и решить, следует ли отправлять команду на устройство для включения / выключения.Я хочу отправить команду на свет, если состояние изменилось с предыдущего состояния, в противном случае команда не будет отправлена.У меня есть следующий код.

import paho.mqtt.client as mqtt #import the client1
import time
def on_message(client, userdata, message):
    print("message received " ,str(message.payload.decode("utf-8")))    
    print("message topic=", message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)

def on_connect(client, userdata, flags, rc):    
    print("Connected with result code "+str(rc))    

broker_address="127.0.0.1"
print("creating new instance")
client = mqtt.Client("P1") 
client.on_connect=on_connect
client.on_message=on_message 
print("connecting to broker")
client.connect(broker_address) #connect to broker
client.loop_start()
print("Subscribing to all topics")
client.subscribe("#")
print("Publishing message to topic","house/bulbs/bulb1")
client.publish("house/bulbs/bulb1","OFF",qos=0,retain=true)
client.publish("house/bulbs/bulb1","ON",qos=0,retain=true)

print("Publishing message to topic", "house/bulbs/bulb2")
client.publish("house/bulbs/bulb2", "ON")
client.publish("house/bulbs/bulb2", "ON")
time.sleep(4) # wait
client.loop_stop() #stop the loop

1 Ответ

0 голосов
/ 23 октября 2018

Единственный способ сделать это - сохранить состояние в вашем коде и сравнить входящие сообщения с этим сохраненным значением.

import paho.mqtt.client as mqtt #import the client1

bulb1 = OFF
bulb2 = OFF

def on_message(client, userdata, message):
    global bulb1
    global bulb2
    print("message received " ,str(message.payload.decode("utf-8")))    
    print("message topic=", message.topic)
    print("message qos=",message.qos)
    print("message retain flag=",message.retain)

    if message.topic == 'house/bulbs/bulb1':
      if bulb1 != str(message.payload.decode("utf-8")):
          #State changed
          bulb1 = str(message.payload.decode("utf-8"))

def on_connect(client, userdata, flags, rc):    
    print("Connected with result code "+str(rc))  
    client.subscribe("#")  

broker_address="127.0.0.1"
print("creating new instance")
client = mqtt.Client("P1") 
client.on_connect=on_connect
client.on_message=on_message 
print("connecting to broker")
client.connect(broker_address) #connect to broker
client.loop_start()


print("Publishing message to topic","house/bulbs/bulb1")
client.publish("house/bulbs/bulb1","OFF",qos=0,retain=true)
client.publish("house/bulbs/bulb1","ON",qos=0,retain=true)

print("Publishing message to topic", "house/bulbs/bulb2")
client.publish("house/bulbs/bulb2", "ON")
client.publish("house/bulbs/bulb2", "ON")
time.sleep(4) # wait
client.loop_stop() #stop the loop
...