Hans-Petter Halvorsen
In this Tutorial we will use Raspberry Pi Pico W (Raspberry Pi Pico with WiFi).
Raspberry Pi Pico W (YouTube) + PowerPoint (PDF)
Raspberry Pi Pico is a “downscaled” version of the original Raspberry Pi and is more comparable with Arduino compared to the original Raspberry Pi. You also need to use a downscaled version of Python, called MicroPython.
Haven't used Raspberry Pi Pico before? Getting Started with Raspberry Pi Pico
Raspberry Pi Pico W has built-in WiFi while ordinary Raspberry Pi has no WiFi. If we are going to send or retrieve data over Internet we need Raspberry Pi Pico W and WiFi.
As mentioned, Raspberry Pi Pico uses a downscaled version of Python, called MicroPython, but most of the original Python will work.
Here you will find more Python Resources.
In order to connect to network and WiFi and use network communication on Raspberry Pi Pico you need to use the MicroPython network Module.
Basic Code Example for connection Raspberry Pi Pico to WiFi:
import network
from time import sleep
ssid = 'xxxxxx'
password = 'xxxxxx'
def ConnectWiFi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
print(wlan.ifconfig())
ConnectWiFi()
Now we know how to connect to a WiFi. We make separate Python Module for Network part We also put Usernames, Passwords, etc. into separate Configuration File. Basically, most of the Code (that are general and can be reused) are put into a separate Python library to improve the code structure and code quality and make it easier to reuse the code for different applications.
import network
import wificonfig
from time import sleep
class WiFi:
def __init__(self):
self.ssid = wificonfig.ssid
self.password = wificonfig.password
def ConnectWiFi(self):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(self.ssid, self.password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Pico Connected on IP {ip}')
return ip
Your WiFi network credentials should be put into a seperate Python file for security reasons. WiFi network credentials include a WiFi network name (SSID) and a password.
WiFi network credentials (Filename: wificonfig.py):
ssid = "xxxxxx"
password = "xxxxxx"
from WiFiNetwork import WiFi
from time import sleep
#Network Initialization
network = WiFi()
ip = network.ConnectWiFi()
#Main Program
while True:
#Do Something..
sleep(20)
For more Raspberry Pi Pico Resources, Tutorials, Examples and Videos see the main Raspberry Pi Pico page.
Internet of Things
Raspberry Pi resouces