I have looked at a lot of forum posts and other articles, but I can’t find an answer to this problem. My apologies if I have overlooked something.
I am running a very basic (and standard) system using three Raspberry Pi Pico W microcomputers with BME280 sensors. A microPython script reads the sensors in an indefinite “while” loop. and publishes the values to the HiveMQ public broker and via that to node-red and InfluxDB.
Everything works extremely well for a while, but after a (seemingly random) interval the connection to the broker is lost.
I have included the recommended statements to restart the Pico, but this does not seem to do anything.
The functions I use are:
def mqtt_connect():
client = MQTTClient(client_id, mqtt_server, keepalive=3600)
user = "******",
password = "*******",
client.connect()
print("Connected to %s MQTT Broker"%(mqtt_server))
print()
return client
# Function to handle failure to connect to MQTT broker
def reconnect():
print("Failed to connect to the MQTT broker. Reconnecting ......")
print()
utime.sleep(5)
machine.reset()
The credentials are:
client_id = “PKL161livingroom” (different for each Pico)
mqtt_server = “broker.hivemq.com”
The code to make the connection to the broker is:
try:
client = mqtt_connect()
except OSError as e:
reconnect()
I have tried putting this (and/or the Wifi connection) both inside and outside my measure/publish “while” loop, but it does not seem to make any difference.
The problem is the same whether a Pico is attached to my laptop or run freestanding.
It works for a while and then stops.
When connected to the laptop, the Thonny shell displays the following:
(First the last “working” output, then the error message)
Connected to Wifi!
ip = 192.168.188.41
Connected to broker.hivemq.com MQTT Broker
HOME/upstairs/picowbme280/at 18.42
HOME/upstairs/picowbme280/rh 40.14
HOME/upstairs/picowbme280/ap 1017.11
Publishing complete
(2023, 3, 5, 12, 26, 59, 6, 64)
Connected to Wifi!
ip = 192.168.188.41
Failed to connect to the MQTT broker. Reconnecting …
PROBLEM IN THONNY’S BACK-END: Exception while handling ‘Run’ (ConnectionError: EOF).
See Thonny’s backend.log for more info.
You may need to press “Stop/Restart” or hard-reset your MicroPython device and try again.
In this case, the Wifi connection was remade inside the measure/publish loop. It appears not to be the problem.
From the last three lines I wonder whether the problem is with Thonny rather than with my set-up.
Can anyone help please.