Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5433

MicroPython • Multipage Webserver on Pico W

$
0
0
Hi

I've created a 2 page webserver on the pico w based on an example of a light on/off webpage I found. I wanted to have an entry page that could use a simple password that would only allow entry to the LED control page once a correct password is entered. Firstly though, I thought I'd try out using a link to the second page and see if that worked. After a bit of effort I got it working. The code is pasted below:

Code:

import networkimport socketimport timeimport machine  from machine import Pin intled = machine.Pin("LED", machine.Pin.OUT) ssid = 'xxxxxxxxxxx'password = 'xxxxxxxxxxx' wlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(ssid, password)html_intro = """<!DOCTYPE html>    <html>        <head> <title>Pico W</title> </head>        <body> <h1>Pico W</h1>            <p>intro</p>            <p>            <a href='/next/page'>Next page</a>            </p>            <br>        </body>    </html>"""html = """<!DOCTYPE html>    <html>        <head> <title>Pico W</title> </head>        <body> <h1>Pico W</h1>            <p>Hello World</p>            <p>            <a href='/light/on'>Turn Light On</a>            </p>            <p>            <a href='/light/off'>Turn Light Off</a>            </p>            <p>            <a href='/last/page'>Go to last page</a>            </p>            <br>        </body>    </html>""" # Wait for connect or failmax_wait = 10while max_wait > 0:    if wlan.status() < 0 or wlan.status() >= 3:        break    max_wait -= 1    print('waiting for connection...')    time.sleep(1)# Handle connection errorif wlan.status() != 3:    raise RuntimeError('network connection failed')else:    print('connected')    status = wlan.ifconfig()    print( 'ip = ' + status[0] ) # Open socketaddr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket()s.bind(addr)s.listen(1)print('listening on', addr)stateis = ""link_clicked = 0# Listen for connectionswhile True:    try:        if link_clicked != 6:            cl, addr = s.accept()             print('client connected from', addr)            request = cl.recv(1024)            print(request)            request = str(request)            link_clicked = request.find('/next/page')            print( 'link clicked = ' + str(link_clicked))            linkstate = "link state " + str(link_clicked) + "\n"            if link_clicked == 6:                response = html                cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')                cl.send(response)                cl.close()            else:                    response = html_intro + linkstate                cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')                cl.send(response)                cl.close()            continue        print("link clicked before check = ", link_clicked)        if link_clicked == 6:            print("waiting for client")             cl, addr = s.accept()             print('client connected from', addr)            request = cl.recv(1024)            print(request)            request = str(request)                        led_on = request.find('/light/on')            led_off = request.find('/light/off')            go_back = request.find('/last/page')            if go_back == 6:                link_clicked = -1            print( 'led on = ' + str(led_on))            print( 'led off = ' + str(led_off))            if led_on == 6:                print("led on")                intled.value(1)                stateis = "LED is ON \n"            if led_off == 6:                print("led off")                intled.value(0)                stateis = "LED is OFF \n"                         linkstate = "link state " + str(link_clicked) + " \n"            if go_back == 6:                response = html_intro + linkstate                cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')                cl.send(response)                cl.close()            else:                    response = html + stateis + linkstate                            cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')                cl.send(response)                print(response)                cl.close()            continue     except OSError as e:        cl.close()        print('connection closed')
When the code starts the pico connects to the wifi and listens for a connection. When the connection is made a page is presented in the browser with a link to the next page. Clicking this link takes you to the LED control page. Here, you can switch the onboard LED on or off, or click an exit link that takes you back to the intro page.

Hopefully someone finds this interesting or helpful for a project they want to build. I haven't seen any examples of doing this so thought it would be worth posting.

It's not particularly elegant but it works. The next stage is to add a text entry box and check it's input for a particular string and only allow entry to the LED control page if that string matches the password.

Statistics: Posted by KennyRonin — Tue Feb 27, 2024 11:41 am



Viewing all articles
Browse latest Browse all 5433

Trending Articles