MQTT – Is there a way to check if the client is still connected
Question:
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.
Answers:
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 variable
EDIT:
You need to call one of the loop
functions to give the client cycles to handle the network operations:
client = mqtt.Client()
client.connect(address, 1883, 60)
while True:
data = getdata()
client.publish("$ahmed/",data,0)
client.loop(timeout=1.0, max_packets=1)
time.sleep(0.2)
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.
import paho.mqtt.client as mqtt
flag_connected = 0
def on_connect(client, userdata, flags, rc):
global flag_connected
flag_connected = 1
def on_disconnect(client, userdata, rc):
global flag_connected
flag_connected = 0
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect(server,port)
client.loop_forever()
if flag_connected == 1:
# Publish message
else:
# Wait to reconnect
You can use will
message to do this.
client=mqtt.Client()
client.will_set('will_message_topic',payload=time.time(),qos=2,retain=True)
client.connect(address,1883,60)
client.publish('will_message_topic',payload='I am alive',qos=2,retain=True)
client.loop_start()#this line is important
while 1:#faster than while True
you loop
By leaving a will
message, you can use another client to make sure if the client is online or not.
Here is the API available.
You just use client.is_connected() returns True or False.
You can also view this article: How to use MQTT in Python (Paho)
A block of code from the article answering your question:
from paho.mqtt import client as mqtt_client
broker = 'broker.io'
port = 8888
client_id = 'client_id '
username = 'username'
password = 'password'
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %dn", rc)
# Set Connecting Client ID
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
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 variable
EDIT:
You need to call one of the loop
functions to give the client cycles to handle the network operations:
client = mqtt.Client()
client.connect(address, 1883, 60)
while True:
data = getdata()
client.publish("$ahmed/",data,0)
client.loop(timeout=1.0, max_packets=1)
time.sleep(0.2)
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.
import paho.mqtt.client as mqtt
flag_connected = 0
def on_connect(client, userdata, flags, rc):
global flag_connected
flag_connected = 1
def on_disconnect(client, userdata, rc):
global flag_connected
flag_connected = 0
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect(server,port)
client.loop_forever()
if flag_connected == 1:
# Publish message
else:
# Wait to reconnect
You can use will
message to do this.
client=mqtt.Client()
client.will_set('will_message_topic',payload=time.time(),qos=2,retain=True)
client.connect(address,1883,60)
client.publish('will_message_topic',payload='I am alive',qos=2,retain=True)
client.loop_start()#this line is important
while 1:#faster than while True
you loop
By leaving a will
message, you can use another client to make sure if the client is online or not.
Here is the API available.
You just use client.is_connected() returns True or False.
You can also view this article: How to use MQTT in Python (Paho)
A block of code from the article answering your question:
from paho.mqtt import client as mqtt_client
broker = 'broker.io'
port = 8888
client_id = 'client_id '
username = 'username'
password = 'password'
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %dn", rc)
# Set Connecting Client ID
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client