Mqtt doesn't send data to subscriber after reconnection
Asked Answered
R

1

5

I have a mqtt beoker which i am trying to connect and subscribe in python. code

client = mqtt.Client("P1",clean_session=True) #create new instance
client.on_connect = on_connect
client.on_message = on_message #attach function to callback
client.on_disconnect = on_disconnect
print("connecting to broker")
client.connect(broker_address, port=port) #connect to broker
print("Subscribing to topic","topic")
client.subscribe("topic")
client.loop_forever() 

call back functions

def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("connected OK Returned code=",rc)
        print(client)
    else:
        print("Bad connection Returned code=",rc)

def on_disconnect(client, userdata, rc):
   print("Client Got Disconnected")
   if rc != 0:
       print('Unexpected MQTT disconnection. Will auto-reconnect')

   else:
       print('rc value:' + str(rc))

   try:
       print("Trying to Reconnect")
       client.connect(broker_address, port)
       client.subscribe("topic")

       print('tried to subscribe')
   except:
       print("Error in Retrying to Connect with Broker")

def on_message(client, userdata, message):
    print("message received ")

So the problem, client gets connected to broker , receives messages for a while and gets disconnected. I have added a re-connection once the client gets disconnected. Now it gets connected but client is not receiving any messages. Output

connecting to broker
Subscribing to topic unilever
connected OK Returned code= 0
<paho.mqtt.client.Client object at 0x7f454660dcf8>
message received
.
.
.
.

recieves messaged for a while and gets disconnected. Output

Client Got Disconnected
Unexpected MQTT disconnection. Will auto-reconnect
Trying to Reconnect
tried to subscribe
connected OK Returned code= 0
<paho.mqtt.client.Client object at 0x7f454660dcf8>

Can someone help me with why this is happening?

Thanks

Romero answered 7/8, 2019 at 13:19 Comment(0)
A
7

This is because you get a fresh session by default when you reconnect (because you have clean_session=True), so you have no active subscriptions.

Move the call to client.subscribe('topic') to inside the on_connect callback then it will resubscribe when it reconnects.

Adverse answered 7/8, 2019 at 14:16 Comment(4)
Hi, after moving client.subscribe('topic') inside on_connect , it is resubscribing when it reconnects and everything is working perfectly fine. But still there is a lot of disconnection happening. It disconnects around 2-3 times every hour. What could be the reason for such frequent disconnections. Any ideas?Romero
No idea, it will most likely be to do with the network.Adverse
Check if your connection is being kept alive? Implement on_log and see how often pingreq and pingresp is seen.Underestimate
@Romero one reason could be of other mqtt client using the same client id is connecting, this will kick out other clients.Krugersdorp

© 2022 - 2024 — McMap. All rights reserved.