Issues with MQTT not receiving messages after ~25 seconds #10795
-
As the title suggests, MQTT subscription works until about 25 seconds, where it then doesn't receive any requests. I used the hivemq websocket to try to get it to work, when I ran into this issue. Any help would be appreciated! Here is the Code: import network
import time
from machine import Pin
from umqtt.simple import MQTTClient
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("*****",security=0)
time.sleep(5)
print(wlan.isconnected())
LED = Pin("LED", Pin.OUT)
mqtt_server = 'broker.hivemq.com'
client_id = 'client'
topic_sub = b'makesurethisworks'
def sub_cb(topic, msg):
print("New message on topic {}".format(topic.decode('utf-8')))
msg = msg.decode('utf-8')
print(msg)
if msg == "on":
LED.on()
elif msg == "off":
LED.off()
def mqtt_connect():
client = MQTTClient(client_id, mqtt_server, keepalive=60)
client.set_callback(sub_cb)
client.connect()
print('Connected to %s MQTT Broker'%(mqtt_server))
return client
def reconnect():
print('Failed to connect to MQTT Broker. Reconnecting...')
time.sleep(5)
machine.reset()
try:
client = mqtt_connect()
except OSError as e:
reconnect()
client.subscribe(topic_sub, 0)
while True:
client.wait_msg() |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Are you using their free service? |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
Under your while true loop change client.wait_msg() to:
|
Beta Was this translation helpful? Give feedback.
-
I eventually figured it out that my college campuses wifi did not allow Raspberry Pis and so that was the reason that I got disconnected. |
Beta Was this translation helpful? Give feedback.
I eventually figured it out that my college campuses wifi did not allow Raspberry Pis and so that was the reason that I got disconnected.