Home CodeMicropython Create a Basic Web Server using the Raspberry Pi Pico W

Create a Basic Web Server using the Raspberry Pi Pico W

by rp2040guy71

In this example we will create the most basic Web Server using our Raspberry Pi Pico W.

This Web Server will display a simple Web Page when we navigate to the IP address assigned to our Pico W. The HTML is embedded in the code

Code

Enter your wifi name and password in here

ssid = ‘my wifi name’
password = ‘my wifi password’

The HTML that is displayed is

html = """<!DOCTYPE html>
<html>
<head> <title>Pico W Web server</title> </head>
<body> <h1>Pico W Web server</h1>
<p>Hello World example</p>
</body>
</html>
"""

You can modify this and test the results, add extra lines, headings and any valid HTML

import network
import socket
import time
  
ssid = 'my wifi name'
password = 'my wifi password'
 
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

# The HTML that will be displayed
html = """<!DOCTYPE html>
    <html>
        <head> <title>Pico W Web server</title> </head>
        <body> <h1>Pico W Web server</h1>
            <p>Hello World example</p>
        </body>
    </html>
"""
 
# Wait for connection or failure
max_retries = 10
# 10 attempts of 1 second
while max_retries > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_retries -= 1
    print('waiting for a connection...')
    time.sleep(1)

# Handle a connection error
if wlan.status() != 3:
    raise RuntimeError('network connection has failed')
else:
    print('connected')
    status = wlan.ifconfig()
    print( 'Your ip address = ' + status[0] )
 
# Open a socket
addrinfo = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
 
mysocket = socket.socket()
mysocket.bind(addrinfo)
mysocket.listen(1)
 
print('listening on', addrinfo)
 
# Listen for connection
while True:
    try:
        myclient, addrinfo = mysocket.accept()
        print('client connected from', addrinfo)

        request = myclient.recv(1024)
        print(request)
        # display the page
        request = str(request)
        response = html
        
        myclient.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
        myclient.send(response)
        myclient.close()
 
    except OSError as e:
        myclient.close()
        print('connection closed')

If you look at the shell window in Thonny you will see something like this

>> %Run -c $EDITOR_CONTENT
waiting for a connection...
waiting for a connection...
waiting for a connection...
waiting for a connection...
waiting for a connection...
waiting for a connection...
connected
Your ip address = 192.168.1.188
listening on ('0.0.0.0', 80)

In your favourite web browser type in the IP address and you will see something like this

When the page is displayed you will see the following in the shell

client connected from ('192.168.1.149', 49249)
b'GET / HTTP/1.1\r\nHost: 192.168.1.188\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Language: en-GB,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\n\r\n'
client connected from ('192.168.1.149', 49250)
b'GET /favicon.ico HTTP/1.1\r\nHost: 192.168.1.188\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0\r\nAccept: image/webp,*/*\r\nAccept-Language: en-GB,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n\r\n'
client connected from ('192.168.1.149', 56678)
b'GET / HTTP/1.1\r\nHost: 192.168.1.188\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Language: en-GB,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nDNT: 1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nPragma: no-cache\r\nCache-Control: no-cache\r\n\r\n'
client connected from ('192.168.1.149', 56679)
b'GET /favicon.ico HTTP/1.1\r\nHost: 192.168.1.188\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0\r\nAccept: image/webp,*/*\r\nAccept-Language: en-GB,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nDNT: 1\r\nConnection: keep-alive\r\nPragma: no-cache\r\nCache-Control: no-cache\r\n\r\n'

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More