RP2040 MicroPython: Making a Wireless Connection

4 minute read

Where I illustrate how to establish a wireless connection using the Pi Pico W.

Introduction

When the Pico W was introduced, there was finally a great product from Raspberry Pi to compete with the ESP32. The ESP32-based products are good, however, I prefer the Pi Pico-based products due to superior support and documentation due to its ARM Cortex-based architecture. The Pico W added wireless capability to the Pico, which brought the ability to network the Pico board. It also added bluetooth, which will be a different tutorial.

Wireless Connection

In order to serve pages, you need to connect to a network. This made my first program a simple program to connect to a wireless network. I found it helpful to have both an LED status of my wireless connection as well as a detailed serial status as well. The former, provides a quick check if I’m connected and the latter provides such details as my IP address and MAC address. This program worked so well, I now use it as both a standalone wireless test as well as my standard method of connecting to a network.

Use wlan as main.py to test wireless connection

As a standalone test, I run it as main.py. This allows me to have it connect to the wireless network and provide information which is helpful as to understanding the connection tothe network.

Use wlan.py as a function to make a wireless connection

I also use it as a function in my web server program using this test to differentiate between MicroPython running on the Pico and Python running on my desktop. (It won’t differentiate between the Pico and Pico W, with the former lacking a wireless interface). This allows me to run the same program using Python on my PC for my test bed. Once I have a semi-successful program running, I begin testing on my Pico with the same program!

# Required for WLAN on Pico W, 'machine' indicates Pico-based micropython
# Will not differeniate between Pico and Pico W!
if hasattr(sys.implementation, '_machine'):
    from wlan import connect
    connect()

This test provides an indication if I’m running MicroPython or Python (CPython) on my desktop. If it is MicroPython, the program will connect to the wireless network and advise of the IP address, otherwise it will spin-up a local webserver running on the desktop. I’ve annotated the program with comments to cover the above points.

secrets.py

This file is a standard practice and contains the SSID and password of your desired wireless LAN connection:

ssid = 'Demo'
password = 'mpc1234!'

A simple text file called secrets.py with the above format and the correct SSID and password is required. It sits at the root folder along with the other files such as wlan.py. As this file is not tracked by git, you will need to add it.

See wlan.py for annotated program or review it in ST4.

Resolving Connection Errors with Pico W

Sometimes it is difficult to connect to the Pico W, here is some background and hints how to resolve issues.

When the Pico W resets, it will attempt to connect to the SSID using the information in secrets.py. The built-in LED will blink slowly when this is in progress. If a wireless connection is made, the LED will turn off and the program will print the following information:

Name: Pico W B
IP Address: 10.0.1.12
MAC Address: 28:cd:c1:08:a9:7d
Channel: 1
SSID: Demo
TX Power: 31
Starting sync server on 0.0.0.0:5001...

It is also critical the device (phone or PC) which are using to connect with the Pico is on the same network as well. Be sure you are connected to the same SSID.

The address you enter in your browser is a combination of the addresses supplied above. Use the IP Address combined with the port number following the 0.0.0.0 as in :5001. Using the above data, you would need to go to this address:

http://10.0.1.12:5001

If the wireless connection can not be made, the LED will blink at a faster rate and a error will be printed via the serial port as in:

Connection failed: LINK_BADAUTH

In this case, the password was in-correct, resulting in a BADAUTH or bad authorization error. The error codes will provide a modicum of information as to how to fix the error.

Link Status Value Meaning
CYW43_LINK_DOWN 0 Wifi down
CYW43_LINK_JOIN 1 Connected to wifi
CYW43_LINK_NOIP 2 Connected to wifi, but no IP address
CYW43_LINK_UP 3 Connect to wifi with an IP address
CYW43_LINK_FAIL -1 Connection failed
CYW43_LINK_NONET -2 No matching SSID found (could be out of range, or down)
CYW43_LINK_BADAUTH -3 Authenticatation failure

For more information, go to the Raspberry Pi Networking Libraries Documentation:

Comments powered by Talkyard.