Raspberry Pi Pico W (WiFi)

Hans-Petter Halvorsen

1 Introduction

In this Tutorial we will use Raspberry Pi Pico W (Raspberry Pi Pico with WiFi).

Raspberry Pi Pico W (YouTube) + PowerPoint (PDF)

 

1.1 Raspberry Pi Pico

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.

 

1.2 Python

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.

 

2 Connect Raspberry Pico W to WiFi

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()

 

WiFi Python Module

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.

 

WiFi Module (Filename: WiFiNetwork.py):


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"

 

Main Program:


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.

 

Additional Resources

Below you will find more interesting and relevant resources


IoT

Internet of Things

Raspberry Pi

Raspberry Pi resouces

Arduino

Arduino resouces

Python

Here you find lots of interesting IoT Projects, etc.