Is there a way to check if the client is still connected to the MQTT broker?
Something like
if client.isConnected(): # for example
# if True then do stuff
Edit: There was instance where my Raspberry Pi stopped receiving from the client although it was still (from the look of it, the code was still showing updated results) running.
Here is the code since I may be doing something wrong:
client = mqtt.Client()
client.connect(address, 1883, 60)
while True:
data = getdata()
client.publish("$ahmed/",data,0)
time.sleep(0.2)
The thing is that I was away, so I am not even sure why it stopped! Only if I restart my broker then it will start receiving again.
I can't see one in the doc but there are the
on_disconnect
on_connect
callbacks that can be used to set your own state variableEDIT:
You need to call one of the
loop
functions to give the client cycles to handle the network operations:Not sure if someone still wants an answer to this, I know I did, hence looked around and found that mqtt paho has such functionality to check if the client is still connected to the broker or not.
It goes something like this:
Cheers!
You can activate a flag in on_connect and deactivate it in on_disconnect. In this way you can know if the client is connected or not.