In my previous articles I have been using the original Raspberry Pi Pico W to learn more about IoT (Internet of Things). The only problem was that it was still missing the “I” from the IoT as I have not used any component to actually connect my Pi Pico to the Internet (or any other network).
Since my last article, a Raspberry Pi Pico with wireless capabilities was released. This upgrade makes it easy to connect to the internet and other devices. In this article, we will have an introduction to the wireless capabilities of the Raspberry Pi Pico and how they can be used with Visual Studio Code (VS Code).
In order to keep it VERY simple, we will only talk about how to prepare VS Code to interact with Pi Pico and implement a code that will connect Pi Pico to the wireless network and serve a simple HTTP page. No wiring, no soldering, no previous microcontroller experience required!
Meet the Raspberry Pi Pico W
The Raspberry Pi Pico W was released in June 2022 with built-in wireless capabilities as a 2.4GHz 802.11b/g/n Wi-Fi module. This makes it easy to connect the board to the internet or other devices wirelessly.
To use the wireless capabilities of the Raspberry Pi Pico, you will need to install the necessary libraries and software. The libraries and software required to use the Wi-Fi module are available as part of the Raspberry Pi Pico’s firmware and can be easily installed through the Raspberry Pi Pico’s software updater.
Drag-and-Drop MicroPython
The process to install MicroPython on Raspberry Pi Picos became much easier now with their “Drag-and-Drop” approach. We just need to download the respective firmware, connect the Pi Pico with the boot select button (BOOTSEL) pressed and then drag the firmware file into the storage volume mounted in your computer (usually identified as RP2). Simples!
For more detailed steps, please check the official Drag-and-Drop MicroPython documentation.
VS Code as the Pico PI development environment
Thonny is usually the first IDE used by those who adventure out into the world of microcontrollers. Although it is a great tool to begin, it doesn’t give you the same productivity as proper IDEs like Visual Studio Code and Jetbrains IntelliJ (or PyCharm as we are talking about Python).
As we already mentioned that VS will be our choice for this article, so lets get it ready. I’m assuming that you already have VS Code installed in your computer, otherwise download it from the official Visual Studio Code website.
The main extension used for this exercise is the Pico-W-Go. To install it, just go to the extensions section in your VS Code, type ‘pico-w-go’ and click on the install button.
The following VS Code extensions are also required, so please install them if you haven’t done so already:
- Python (ms-python.python)
- IntelliCode (visualstudioexptteam.vscodeintellicode)
- Pylance (ms-python.vscode-pylance)
Once you finish the extension installation, you will see the following in the VS Code terminal.
(AutoConnect enabled, ignoring 'manual com port' address setting)
Searching for boards on serial devices...
Connecting to /dev/tty.usbmodem14101...
>>>
If you see the Python prompt (>>>), it means that VS Code connected to your Pico W. The device identification (/dev/tty.usbmodem14101) may vary according to your computer operating system. The one above is from my macOs, if you are using Windows you may see something like “Connecting to COM3”.
To check all the extension functionalities, open the VS Code Command Palette (Ctrl+Shift+P) and type “pico-w-go”.
Among some extension configurations, I found the status bar buttons very handy. You can customize them in the “Statusbar Buttons” sections of the Pico-W-Go extension configuration. To access the configuration, just choose “Global settings” from the list above. This is how I have configured mine:
And this is how it looks like in my status bar:
Quick Test
If you want to make sure everything is working properly, the following set of commands can be a good quick test. Just type them in the VS Code terminal and (hopefully) the on-board LED turns on.
>>> from machine import Pin
>>> led = Pin("LED", Pin.OUT)
>>> led.toggle()
If it works, you should see the on-board LED on.
The Raspberry Pi Pico W HTTP Server
Now we will try something more interesting. The code below connects Pico W to your network and serves a simple HTTP page. While it is trying to connect, the on-board LED will blink and then stay on once the network connection is stabilised.
Create a new project in VS Code with file named “main.py” and copy the content from the code block below. Please note that you need to update lines 9 and 10 with your WiFi network settings.
import time
import network
import socket
from machine import Pin
pin = Pin("LED", Pin.OUT)
ssid = 'YourSSID'
password = 'Your-WiFi-Pass'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# wlan.config(hostname="pipicoserver")
wlan.connect(ssid, password)
# HTML Page
html = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<p>It's alive!</p>
</body>
</html>
"""
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
pin.toggle()
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
pin.on()
print( 'ip = ' + status[0] )
# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from: ', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
response = html
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close
except OSError as e:
cl.close()
print('connection closed')
To test the code, just click on the “Run” button in the status bar. If you haven’t used the suggested status bar configuration, just choose “Run current file” from the Pico-W-Go extension palette command. The logs should show something like this:
waiting for connection...
waiting for connection...
waiting for connection...
waiting for connection...
waiting for connection...
waiting for connection...
waiting for connection...
waiting for connection...
waiting for connection...
connected
ip = 192.168.50.124
listening on ('0.0.0.0', 80)
You can use the IP address from the logs to test the HTTP page, but you will not have access to these logs when the Pico W is working as standalone device. Therefore, using the hostname is the proper way to access it via the network. The default hostname for the Pico W is “pybd”, so the URL to test will be:
Then you should see a page like this:
Good day, very well done tutorials. thank you. I want to send parsed data from mediapipe to the pico( so I can effect changes in the physical world). It means going from python on VS code to MicroPython on vs code (or another editor). Can you direct me? I’m very inexperienced.