Tullio_IRL
Tullio_IRL

Reputation: 99

python paho-mqtt loops & persistan connection to broker

I've written some python3 code to do the following in sequence;

  1. check if internet up
  2. if internet up connect to MQTT broker
  3. publish message to pub_topic every 30mins
  4. and loop to step one & repeat

Also, I'd like the MQTT connection to be persistent so that incoming messages can be received.

What's happening is that after a few iterations the MQTT connection drops out and eventually the programme crashes.

Below is my while loop code snippet;

while True:
        try:
                if not internet_up():
                        time.sleep(20)

                client = mqtt.Client()
                client.on_connect = on_connect
                client.on_message = on_message
                client.connect(broker, 1883, 60)
                client.loop_start()


                if internet_up():
                        print(read_temp())
                        client.publish(pub_topic, read_temp())
                        time.sleep(60*30)


        except Exception as err:
                logging.info(err)
                pass

A. Is there an obvious error here?

B. I don't feel this is optimal, how could this be made more efficient, pyhonic or corrected?

C. Also, what is the difference between loop_start() & loop_forever() ? I've read the documentation and it's not obvious to me.

Upvotes: 1

Views: 9813

Answers (1)

blp
blp

Reputation: 608

Building off of barny's comment, I suspect the problem here is that you are repeatedly calling client.loop_start() without ever calling client.loop_stop() or client.disconnect().

To test this I wrote a test script using the mosquitto test server:

from time import sleep

import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
   client.subscribe('$SYS/broker/load/messages/received/1min')


def messages_received(client, userdata, message):
   print(message.payload)


while True:
   client = mqtt.Client()
   client.loop_start()
   client.message_callback_add('$SYS/broker/load/messages/received/1min', messages_received)
   client.on_connect = on_connect
   client.connect("test.mosquitto.org", 1883, 60)
   sleep(13)

This script failed within a few minutes. However, if I changed the last section to include client.loop_stop() and client.disconnect() like this:

while True:
  client = mqtt.Client()
  client.loop_start()
  client.message_callback_add('$SYS/broker/load/messages/received/1min', messages_received)
  client.on_connect = on_connect
  client.connect("test.mosquitto.org", 1883, 60)
  sleep(3)
  client.disconnect()
  client.loop_stop()
  sleep(10)

It ran indefinitely. I'm new to both python and paho, so there may be another issue here... also I only tested this with test.mosquitto.org so there may be broker specific issues... but to make your existing code work I would try at least adding client.loop_stop inside your loop.

However, since you want the connection to be persistent, you might want to consider using client.loop_forever() and placing your while loop into an on_connect callback or something similar to that.

Hope that helps!

Upvotes: 1

Related Questions