If you ran your Raspberry Pi Pico W for a very long time, the chances are that you would eventually notice that your program isn't running anymore.

One reason why programs fail over a long period of time involves Wi-Fi reliability. If your Wi-Fi becomes unreliable, the Pico W's first instinct is to crash.

Let's see how we can make the Pico W more resilient in MicroPython.

What Causes Wi-Fi Reliability on the Pico W?

Your Wi-Fi is disconnecting many times a day. It's hard to perceive this on your phone or PC because operating systems and software have accounted for this guaranteed shortcoming of Wi-Fi networks.

With the Pico W, a simple script that connects and fetches data would crash if your Pico W lost its connection. The only solution is to hard reset it by pulling the power.

Raspberry Pi Pico W logging Wi-Fi outage

If your project requires logging data for hours at a time, you don't want to constantly have to monitor outages.

Error handling is the key to ensuring that your program can survive through poor connections.

Will Improving Wi-Fi Signal Strength Help?

You might be considering whether you should use a mesh network to increase signal strength. Yes, it will help, but not to a degree where you can avoid scenarios where there's a drop in the connection.

In one experiment, we placed a Pico W in a location on the second floor of a house, and another Pico W next to the Wi-Fi router. The difference in outcome is that the device on the second floor disconnected earlier than the Pico W situated closer to the router. The latter eventually had a dropped connection too, as reflected by gaps in data gathering.

Unfortunately, while improving signal strength reduces the issue, it doesn't solve it. So let's look at some methods to write more resilient code.

Connect and Disconnect Software Method

If your project only requires one-way communication, this method would be perfect for it. Some examples would be temperature loggers which need to be run over a long period of time but only logs data once in a while.

This resolves the issue of crashes happening when your Pico W thinks it's connected, but when it tries to send data to an endpoint, it realizes it's offline, then proceeds to crash.

The key to this method involves a procedure where your program runs in an infinite loop. Within this loop is a function that will run after a specific amount of time has elapsed. This function will connect the Pico W to Wi-Fi, do its job (e.g. send data to a server), then disconnect.

Software Hard Reset

Using MicroPython's hard reset code can help you ensure that your Pico W will be running even if you don't intervene. See our intro to MicroPython if you need help, then run this command:

        import machine
machine.reset()

Let's say you lose connection at midnight while you're asleep. If you didn't have a software hard reset, you would have to manually reset the program in the morning. This might cause eight hours of data-logging loss.

If you have a software hard reset, which you have put in your error handling code block, the Pico W will reset itself.

The downside to this is that upon hard reset, your program's state will be lost and if your program crashes, the software hard reset won't trigger.

Use the Pico W's Built-In Watchdog Timer

A hardware watchdog timer is basically a device that would reset your Pico W if it malfunctions. Watchdog timers are countdown timers. If they get a signal before the countdown hits zero, then the countdown is reset. However, if the countdown hits zero, then it will reset the Pico W.

In MicroPython, the basic code is as such:

        from machine import WDT
wdt = WDT(timeout=1000) #timeout is in ms
wdt.feed() #resets countdown

You can visit MicroPython's WDT documentation to learn more and also Pico's official SDK docs for C++ guidance.

A Raspberry Pi Pico W with headers

Make Your Pico W's Connection Bombproof

To give your Pico W the best chance to run indefinitely on a Wi-Fi connection, you will need to do multiple things.

Firstly, ensure that the Wi-Fi connection is reasonably strong. While code can mitigate some challenges with Wi-Fi, depending on the method used, it can sometimes result in a lost program state or lost data points.

Secondly, the program needs to have some method to recover from dropped Wi-Fi connections so that you can get continued service.

With these steps in place, you can be confident of more reliable connectivity from your Pico W.