user1424074
user1424074

Reputation: 147

on_disconnect isn't being triggered python

I've tried a number of iterations of this code. This is the last attempt and I'm stumped. It never sees the published message in addition to not getting the on disconnect.

#!/usr/bin/python3
import paho.mqtt.client as mqtt  #import the client1
import time

def on_message(client, userdata, message):
  print("received message =",str(message.payload.decode("utf-8")))

def on_disconnect(client, userdata, flags, rc):
    client.connected_flag=False #set flag
    print("disconnected OK")

def on_connect(client, userdata, flags, rc):
    if rc==0:
        client.connected_flag=True #set flag
        print("connected OK")
        client.subscribe("Phone/announcet")#subscribe
        print("subscribed OK")
    else:
        print("Bad connection Returned code=",rc)

mqtt.Client.connected_flag=False#create flag in class
broker="192.168.1.71"
client = mqtt.Client()             #create new instance 
print("Connecting to broker ",broker)
client.username_pw_set(username="zzz",password="xxx")
while True:
    client.on_connect=on_connect  #bind call back function
    client.on_disconnect=on_disconnect
    client.connect(broker)      #connect to broker
    client.loop_start()
    while not client.connected_flag: #wait in loop
        print("In wait loop")
        time.sleep(1)
    print("in Main Loop")
    time.sleep(2)
    client.loop_stop()    #Stop loop 
    client.disconnect() # disconnect
    print("After disconnect")

Here is what the output looks like:

Connecting to broker  192.168.1.71
In wait loop
connected OK
subscribed OK
in Main Loop
After disconnect
in Main Loop
connected OK
subscribed OK
After disconnect
in Main Loop
connected OK
subscribed OK
After disconnect

Thanks, Jim

Upvotes: 1

Views: 4289

Answers (1)

hardillb
hardillb

Reputation: 59731

First you are not setting the on_message callback on the client, this is why you are getting no messages.

Second there is no benefit setting the callbacks inside the loop, they are better moved outside the main loop.

Third you are stopping the loop before calling disconnect, since all the callbacks run on the client event loop the on_disconnect will never get called. You also have an extra argument in the on_disconnect, there should be no flags.

I'm also not sure your connected_flag will ever work, it should be moved to a global variable

#!/usr/bin/python3
import paho.mqtt.client as mqtt  #import the client1
import time

connected_flag=False

def on_message(client, userdata, message):
    print("received message =",str(message.payload.decode("utf-8")))

def on_disconnect(client, userdata, rc):
    global connected_flag 
    connected_flag=False #set flag
    print("disconnected OK")

def on_connect(client, userdata, flags, rc):
    global connected_flag
    if rc==0:
        connected_flag=True #set flag
        print("connected OK")
        client.subscribe("Phone/announcet") #subscribe
        print("subscribed OK")
    else:
        print("Bad connection Returned code=",rc)

broker="192.168.1.71"
client = mqtt.Client()             #create new instance 
print("Connecting to broker ",broker)
client.username_pw_set(username="zzz",password="xxx")
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect

while True:
    client.connect(broker)      #connect to broker
    client.loop_start()
    while not connected_flag: #wait in loop
        print("In wait loop")
        time.sleep(1)
    print("in Main Loop")
    time.sleep(3)
    print(connected_flag)
    client.disconnect() # disconnect
    print("After disconnect")
    print(connected_flag)
    client.loop_stop()

Upvotes: 1

Related Questions