In this article we will fetch a web page using a Raspberry Pi Pico W using micropython, the source code of the web page will be displayed in the shell window
The first task to do, if you do not have micropython on your Raspberry Pi Pico W is to download and install it
You need to download the latest micropython firmware, you can get this from the following link
https://micropython.org/download/rp2-pico-w/
After you have downloaded the first step is to flash the MicroPython Firmware onto the Raspberry Pi Pico W.
To do this you will need to put the Pico W in Bootloader mode by holding the “BOOTSEL” button down while connecting the Pico W to your machine.
You will see a drive appear on your PC called RPI-PI2, drag the uf2 firmware that you downloaded from the micropython link above.
It will take a short bit of time t hen you will have Micropython ready to go on the device
I use Thonny for development – now for the code
Code
This example connects to your network using Wi-Fi with your network id and password. You need to supply the credentials here
ssid = 'your network id here' password = 'your password here'
We then get the IP Address of the Google homepage and connects to the IP address using a Socket connection
Performs a GET request and prints the result in the Shell window
import time import network import socket ssid = 'your network id here' password = 'your password here' wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) # Wait for connection or fail connection_attempts = 10 # 10 attempts to connect at 1 second intervals while connection_attempts > 0: if wlan.status() < 0 or wlan.status() >= 3: break connection_attempts -= 1 print('waiting for a connection.') time.sleep(1) # Handle connection error if wlan.status() != 3: raise RuntimeError('network connection has failed') else: print('connected') status = wlan.ifconfig() print( 'ip = ' + status[0] ) # Get IP address for google addressinfo = socket.getaddrinfo("google.com", 80) address = addressinfo[0][-1] # Create a socket and make a HTTP request mysocket = socket.socket() mysocket.connect(address) mysocket.send(b"GET / HTTP/1.0\r\n\r\n") # Print the response print(mysocket.recv(1024))