Skip to content

Instantly share code, notes, and snippets.

@dlashua
Created November 29, 2020 11:06
Show Gist options
  • Save dlashua/ffb17e3abcce125ea42da5edc6d4dcc2 to your computer and use it in GitHub Desktop.
Save dlashua/ffb17e3abcce125ea42da5edc6d4dcc2 to your computer and use it in GitHub Desktop.
Paho Reconnect Test
1606647848: New connection from 10.10.2.53 on port 1883.
1606647849: Socket error on client <unknown>, disconnecting.
1606647849: New connection from 10.10.2.53 on port 1883.
1606647849: New client connected from 10.10.2.53 as python-test (p2, c0, k60, u'hass').
1606647850: Client python-test disconnected.
1606647850: New connection from 10.10.2.53 on port 1883.
1606647850: Socket error on client <unknown>, disconnecting.
1606647851: New connection from 10.10.2.53 on port 1883.
1606647851: New client connected from 10.10.2.53 as python-test (p2, c0, k60, u'hass').
1606647854: Client python-test disconnected.
1606647854: New connection from 10.10.2.53 on port 1883.
1606647854: Socket error on client <unknown>, disconnecting.
❯ python3 ./test.py
Waiting For Connect
Waiting For Connect
connected OK
Connected in Main Loop
Disconnecting
publish while disconnected
waiting 2
Disconnected Code= 0
connected OK
python-test: b'while disconnected'
publish while connected
python-test: b'while connected'
Disconnected Code= 0
#!python3
import paho.mqtt.client as mqtt #import the client1
import time
MQTT_CLIENT = None
MQTT_USER = 'user'
MQTT_PASS = 'password'
MQTT_HOST = '1.1.1.1'
MQTT_CLIENT_ID = 'python-test'
MQTT_TOPIC = 'python-test'
def on_connect(client, userdata, flags, rc):
if rc==0:
print("connected OK")
client.subscribe(
(MQTT_TOPIC, 2)
)
else:
print("Bad connection Returned code=",rc)
def on_disconnect(client, userdata, rc):
print('Disconnected Code=',rc)
client.reconnect()
def on_message(client, userdata, msg):
print(f"{msg.topic}: {str(msg.payload)}")
MQTT_CLIENT = mqtt.Client(
client_id=MQTT_CLIENT_ID,
clean_session=False
)
MQTT_CLIENT.username_pw_set(
username=MQTT_USER,
password=MQTT_PASS,
)
MQTT_CLIENT.reconnect_delay_set(min_delay=1, max_delay=120)
MQTT_CLIENT.on_connect = on_connect
MQTT_CLIENT.on_disconnect = on_disconnect
MQTT_CLIENT.on_message = on_message
MQTT_CLIENT.loop_start()
def mqtt_connect():
MQTT_CLIENT.connect(
MQTT_HOST,
port=1883,
keepalive=60
)
mqtt_connect()
while not MQTT_CLIENT.is_connected(): #wait in loop
print("Waiting For Connect")
time.sleep(1)
print("Connected in Main Loop")
# MQTT_CLIENT.loop_stop() #Stop loop
print("Disconnecting")
MQTT_CLIENT.disconnect() # disconnect
while MQTT_CLIENT.is_connected(): #wait in loop
print("Waiting For Disconnect")
time.sleep(1)
print('publish while disconnected')
MQTT_CLIENT.publish(
MQTT_TOPIC,
payload='while disconnected',
qos=2,
retain=False
)
print('waiting 2')
time.sleep(2)
cnt = 0
while not MQTT_CLIENT.is_connected() and cnt < 10:
print("Waiting For Connect")
time.sleep(1)
cnt = cnt + 1
print('publish while connected')
MQTT_CLIENT.publish(
MQTT_TOPIC,
payload='while connected',
qos=2,
retain=False
)
time.sleep(2)
MQTT_CLIENT.loop_stop()
MQTT_CLIENT.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment