By the end of this tutorial, we'll have our Raspberry Pi Pico W connected to a Wi-Fi network and performing basic network operations like querying a web page. To follow along, all you'll need is a Raspberry Pi Pico W and a USB Micro B cable to connect it to your computer. I've already soldered pin headers to my Pico, though if yours doesn't have any, you won't need them for this tutorial. And of course, a Wi-Fi network connected to the internet.
Let's get started. We're going to start by loading MicroPython onto our Raspberry Pi Pico. If you know that your Pico is already running MicroPython, you won't have to follow this step. On your Pico, locate the boot button and hold that down, then connect your USB cable. I can see that my Pico has appeared as removable storage on my computer here, it's labelled RPI-RP2. Download MicroPython from the article, just find that download link, click that, wait for the download to complete. That's gone to my downloads directory, and I'll just drag and drop that into my Pico's directory. MicroPython is now loaded onto the Pico. The final step is to just unplug it and then plug it back in.
We're going to be working in Phony now. If you haven't already downloaded and installed Phony, find the link in the article to do that. Open up Phony and connect to your Pico. Go to the Run menu and select Interpreter. Make sure that you have MicroPython Raspberry Pi Pico selected and press OK, then click the red button for Stop/Restart Backend. And we can see in the File Pane we have the Raspberry Pi Pico connected. If you don't see this File Pane, you can enable it with View Files and we can see that the Pico is running MicroPython, so we can execute commands in the REPL. If you wish, like print("Hello World").
All right, now to connect to the internet, find the example code in the article, copy and paste that into the Phony editor and press the Run button.
For connecting to Wi-Fi, highlight all of that code and copy it into funny. Taking a look at the example code, there's a section for entering your network credentials so I'll enter the name for my network under SSID which is my Wi-Fi and my password. We can see this example is broken up into two sections. The first section we're going to query Google.com so we'll actually read the raw HTML from that website and the second example we're going to query a date time server to get the live current date and time.
I'm going to save the script to my Raspberry Pi Pico and I'll call it example.py. I don't want it to run automatically so I won't call it main.py. Now the moment of truth, click the green run button and let's see what happens. There we go, let's have a look at what happened. We start off with a message saying querying Google.com and then what follows is this big block of HTML code that is actually the result returned by the query.
When we look at section one where we have that print for querying Google.com, we call urequests.get and then we're just getting the address of Google.com. The result of the request is loaded into ah and then we can print r.content which is the HTML content that was returned. Now there's a lot of data here, we could click this for details and see the entirety of what was returned.
Okay, so we can query web pages but what can we do with that information? Let's move on to the second part of the example. Having a look at that second example, we can see that we query the current date and time. And if we look at the code for that, we're using urequests.get again this time with a different website and this is a date time server. This time instead of printing r.content we're calling r.json which is unpacking the data that was returned because it was returned in the JSON format.
If I navigate to that URL, we can see that it doesn't load like a normal web page. My browser is actually presenting it like a data file. We can see that we have today's date, something called milliseconds, and also the time. So, Micropython is able to interpret this data and here we see it presented as a Python dictionary. That means that if I call `r.json` and then we just want to extract the time component, we should get just that one little section of that JSON data because we're just extracting the time entry out of what is a Python dictionary.
And so, of course, we could do the same thing with the date by accessing that entry in the dictionary and here we have the date presented as month day year, nice. And there you have it, a basic example of getting connected to Wi-Fi and communicating with the world outside. We were able to query a web page for its HTML data and we were also able to access the live date and time from some time server. Then we did a little bit of post processing on the data that was returned to just extract, for example, the date or the time.
If you have any questions or uncertainty then leave us a comment at the bottom of the article. We're full-time makers and happy to help. And if you're wondering where to go to from here, there's also some additional resources at the bottom of the article including other tutorials and neat projects that you can do with a Pico W. Catch you next time!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.