Create a Basic Web Server using the Raspberry Pi Pico W

PICO-W

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 = """&lt;!DOCTYPE html&gt;
    &lt;html&gt;
        &lt;head&gt; &lt;title&gt;Pico W Web server&lt;/title&gt; &lt;/head&gt;
        &lt;body&gt; &lt;h1&gt;Pico W Web server&lt;/h1&gt;
            &lt;p&gt;Hello World example&lt;/p&gt;
        &lt;/body&gt;
    &lt;/html&gt;
"""
 
# Wait for connection or failure
max_retries = 10
# 10 attempts of 1 second
while max_retries &gt; 0:
    if wlan.status() &lt; 0 or wlan.status() &gt;= 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'

Related posts

A Raspberry Pi Pico and KY-006 Passive Buzzer

A Raspberry Pi Pico and KY-012 Active Buzzer

A Raspberry Pi Pico and KY-032 Obstacle Detector

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