Created
October 30, 2023 16:19
-
-
Save hbisneto/a679814458321d0ada8862a47491cb8c to your computer and use it in GitHub Desktop.
Check if an MQTT client is connected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 60) | |
client.loop_forever() | |
if flag_connected == 1: | |
# Publish message | |
else: | |
# Wait to reconnect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment