Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I wrote basic MQTT python-client. But it disconnect ungracefully without giving any error. When i look event log file i found it's connected and same time is disconnected ungracefully.

import paho.mqtt.client as mqtt
Broker = "192.168.43.246"
def on_connect(client, userdata, flages, rc):
        print("Connected with result code=",rc)
client = mqtt.Client(client_id="Client123", clean_session=False, userdata=None)
client.on_connect = on_connect
client.connect(Broker, 1883, 60)

Here i attached a snap of event log also.

@MikeScotty's answer is not right. What's missing is starting the MQTT network loop. The loop needs to be started to handle sending keep alive packets, doing multi leg handshakes for QoS 1/2 messages and to handle incoming subscriptions.

If you just want to stay connected or 10 seconds then something like this will work

import paho.mqtt.client as mqtt
import time
Broker = "192.168.43.246"
def on_connect(client, userdata, flages, rc):
        print("Connected with result code=",rc)
client = mqtt.Client(client_id="Client123", clean_session=False, userdata=None)
client.on_connect = on_connect
client.connect(Broker, 1883, 60)
client.loop_start() # starts network loop on separate thread
time.sleep(10) # optionally wait some time
client.disconnect() # disconnect gracefully
client.loop_stop() # stops network loop

If you want to stay connected forever (or until killed) then you have 2 options

client.connect(Broker, 1883, 60) client.loop_forever()

This will start the network loop in the foreground (and as such block until killed).

client.connect(Broker, 1883, 60) while True: client.loop() # runs one iteration of the network loop # do something else client.connect(Broker, 1883, 60) client.loop_start() while True: # do something else def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to MQTT Broker!") else: print("Failed to connect, return code %d\n", rc) client = mqtt_client.Client(client_id) client.username_pw_set(username, password) client.on_connect = on_connect client.connect(broker, port) return client For python that is not useable code. You probably need stackoverflow.com/editing-help But you also should add a prose explanation of how that code works and why it solves the problem, because of How to Answer. – Yunnosch Sep 9, 2022 at 12:12 Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Sep 14, 2022 at 7:38

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.