Raspberry Pi Pico W | Connecting to the Internet

Updated 05 August 2024

Introduction

In this guide, we'll get a Raspberry Pi Pico W connected to the internet using WiFi. To demonstrate the connection we can query a webpage, or get the current time from a time service. To follow along, all you'll need is:

Install MicroPython onto Pico W

Follow these steps if it's your first time using a Pico W, or the Pico W does not have MicroPython currently installed.

  • Download MicroPython for Pico W - comes (with urequests and upip preinstalled)
  • Put your Pico W into Device Firmware Update mode as follows:
    • Hold the BOOTSEL button on the Pico W
    • Connect the Pico W to your computer via the USB cable
    • Release the BOOTSEL button

 

The Pico W will appear as a removable drive called RPI-RP2

 

  • Drag + Drop the MicroPython .uf2 file into RPI-RP2
  • Unplug your Pico W from USB, and plug it back in.

After the reboot, your Pico W is now running MicroPython

Example - Connect to WiFi

  • Open Thonny
  • Select the correct interpreter: (Run > Configure Interpreter...) > MicroPython (Raspberry Pi Pico): 
  • Connect to your Pico W (Run > Stop/Restart Backend)
  • Create a new script (File > New)
  • Copy the following code into the blank script
# A simple example that:
# - Connects to a WiFi Network defined by "ssid" and "password"
# - Performs a GET request (loads a webpage)
# - Queries the current time from a server

import network   # handles connecting to WiFi
import urequests # handles making and servicing network requests

# Connect to network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)

# Fill in your network name (ssid) and password here:
ssid = ''
password = ''
wlan.connect(ssid, password)


# Example 1. Make a GET request for google.com and print HTML
# Print the html content from google.com
print("1. Querying google.com:")
r = urequests.get("http://www.google.com")
print(r.content)
r.close()

# Example 2. urequests can also handle basic json support! Let's get the current time from a server
print("\n\n2. Querying the current GMT+0 time:")
r = urequests.get("http://date.jsontest.com") # Server that returns the current GMT+0 time.
print(r.json())

 

Fill in your WiFi credentials in the code (ssid, password). This example is in two parts:

Part 1 shows how to make a simple GET request. In this example we query www.google.com. Part 2 queries a time-server for the current date and time.

Save and run this script. All going well you ought to see something similar to the following:

 

This is the output from our example code - and it looks pretty complicated! Let's break it down:

Part 1: GET Request

The large grey block of text is the result from querying google.com. This is actually the raw HTML code that makes up the webpage - neat! Note; If you query a webpage with lots of embedded content and rich media your Pico W may run out of memory and crash! This would happen if we query www.core-electronics.com.au, which contains lots of images.

Code Remix: See what happens when you query www.core-electronics.com.au (you may have to restart your Pico W afterwards!)

 

Part 2: Timeserver and json support

This is the data returned by a time-server (http://date.jsontest.com). The date/time returned is from timezone GMT+0. If you follow the link to that server you will see the data is presented on a webpage in .json format. We could query this website in the same way as Example 1, but it's far more useful to make use of the limited json support offered by the urequests package. Printing r.json() interprets the requested data as json format and loads it into a useful dictionary. For example, we can extract just the time component by reading r.json()['time']

Conclusion

We've connected a Pico W to a WiFi network and executed some simple queries to prove we can connect to the internet - this is just the start! With this assumed knowledge you're ready to tackle more advanced examples and project. Check the Additional Resources section below to see what's available.

Finally, if you have any questions don't hesitate to leave a comment below. We're full time makers and happy to help!

Additional Pico W Resources

Guides, projects and technical resources to help you get started with the Raspberry Pi Pico W

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Tags:

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.