0

Currently, I'm trying to make a simple application using MQTT, Python, and OpenHab. So, I just want to connect to MQTT server, subscribe to the topic and read data/message placed there. Everthing works fine but with "limitations". Python client able to connect to MQTT, subscribe and... BOOM! Nothing! I'm able, to read messages from the subscribed topic but I need to update topic after client connection. Without re-updating topic data after client connection, I will not be able to see anything even if there is real data. So, in brief

  • Python client (paho MQTT 1.3v) connects to MQTT (mosquitto) server
  • subscribes to specified topic (want to see current topic data here)
  • nothing happens until someone will re-update topic.

How can I read topic data without re-updating that topic?

Here is my code class MQTTBroker(object):

def __init__(self, Trigger, ipAddress, userName, password, fileNameTopic, volumeTopic, enabledTopic):
    self.ipaddress = ipAddress
    self.username = userName
    self.password = password
    self.topic = topic
    self.fileNameTopic = fileNameTopic
    self.volumeTopic = volumeTopic
    self.enabledTopic = enabledTopic
    self.state = 0
    self.client = mqtt.Client()
    self.client.on_connect = self.on_connect
    self.client.on_message = self.on_message
    self.logger = logging.getLogger(__name__)
    self.client.enable_logger(logger)

    self.client.connect(self.ipaddress, 1883, 60)
    self.client.loop_start()

def __exit__(self, exc_type, exc_val, exc_tb):
    self.client.loop_stop()

# The callback for when the client receives a CONNACK response from the server.
def on_connect(self, client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    self.client.subscribe(self.fileNameTopic, 0)
    self.client.subscribe(self.volumeTopic, 0)
    self.client.subscribe(self.enabledTopic, 0)

# The callback for when a PUBLISH message is received from the server.
def on_message(self, client, userdata, msg):
    self.state = msg.payload
    if msg.topic == self.fileNameTopic:
        Trigger.change_file_name(msg.payload)
    elif msg.topic == self.volumeTopic:
        Trigger.change_volume(msg.payload)
    elif msg.topic == self.enabledTopic:
        Trigger.change_state(msg.payload)
user922871
  • 435
  • 2
  • 6
  • 17

1 Answers1

4

MQTT doesn't work that way, messages are not "read" from topics.

Under normal circumstances you subscribe, then wait until a new message is published, at this point the broker will deliver the new message to the subscriber.

If you want to receive the last message published to a topic at the point when you subscribe then you need to make sure the message is published with the retained flag set to true. When this flag is set on the message (by the publisher) the broker will store that message and deliver it at the point a new subscriber connects.

You've not include the code for the publisher so I can't point out what to change, but the paho doc should explain: https://pypi.python.org/pypi/paho-mqtt/1.1#publishing

hardillb
  • 54,545
  • 11
  • 67
  • 105