Connection to public broker fails after working well for a while

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.

On reflection, I don’t think the problem is with Thonny. It occurs just the same when the Pico is free-standing.

I have since added the line:

CleanSession = False

before

client.connect()

and set QoS to 2, but this has not made any difference.
I still get dropouts (and no recovery) after a period of succesful reporting.
What am I doing wrong?

Hello Bob,

Normally some features like QoS 2 isn’t supported by MQTT clients for MicroPython to keep code size small, so configuring to QoS 2 will not make any difference. Could you please share the full microPython script code that you are using to read the BME280 sensors?

Best regards,
Diego from HiveMQ Team

Hello Diego,
Thanks for your reply.
Very strangely, the system suddenly started working properly and I have had four Pico/sensors working permanently in different locations for a few days now.
They all deliver good data to node-red, InfluxDB and Grafana and also to your web UI.
I still have a problem with a couple of rooms, but these are on power line extenders. Although the wifi signal is strong, the Pico’s send one value successfully and then stop.
I suspect that the problem may be with the wifi (same SSID, different channel), but it works well with other devices.

For your information I attach the microPython script I am using (the others are clones of this, with only the location information changed).

Regards,
Bob Harris

(Attachment main_copy.py is missing)

Hello Diego,
Thanks for your reply.
Very strangely, the system suddenly started working properly and I have had four Pico/sensors working permanently in different locations for a few days now.
They all deliver good data to node-red, InfluxDB and Grafana and also to your web UI.
I still have a problem with a couple of rooms, but these are on power line extenders. Although the wifi signal is strong, the Pico’s send one value successfully and then stop.
I suspect that the problem may be with the wifi (same SSID, different channel), but it works well with other devices.

For your information I attach the microPython script I am using (the others are clones of this, with only the location information changed).

Regards,
Bob Harris

(Attachment myscript.txt is missing)

Hello Bob,

Good to know that the system started worked properly! Some home appliances may generate a high amount of electrical noise and create temporary unstable voltage and wireless signal issues on these powerline extenders. Generally this type of extenders is not recommended for use with ESP32, ESP8266 and Raspberry Pi Pico W devices.

Not sure if you have the opportunity to read it but follow the link to a detailed article on our blog about Raspberry Pi Pico W, Micropython and MQTT

Best regards,
Diego from HiveMQ Team