Fish
Created July 17, 2022 © MIT

The Pico W Webserver

A simple interface to control your Pico W via HTTP

IntermediateWork in progress1 hour70
The Pico W Webserver

Things used in this project

Hardware components

Raspberry Pi Pico W
×1
Photo resistor
Photo resistor
×1
LED (generic)
LED (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×2

Story

Read more

Schematics

Pico W Schematic

Pin Usage:
3.3v Out
GND
GP26 - Photo Resistor
GP8 - LED

Code

main.py

MicroPython
import wifi
import network
import ubinascii
from machine import Pin, ADC
import urequests as requests
import time
import secrets 
import socket

# Join WiFi
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print('mac = ' + mac)
wlan = network.WLAN(network.STA_IF)
wlan.connect(secrets.SSID, secrets.PASSWORD)
status = wlan.ifconfig()
    
# Function to load in html page    
def get_html(html_name):
    with open(html_name, 'r') as file:
        html = file.read()
    return html

# HTTP server with socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)

print('Listening on', addr)

# Pins
led = Pin('LED', Pin.OUT)
led_1 = Pin(8, Pin.OUT)
light_input = ADC(0)

# Conversions
u16convert = 100 / (65535)
light_value = light_input.read_u16()
light_in = light_value * u16convert

# Listen for connections
while True:
    try:
        cl, addr = s.accept()
        print('Client connected from', addr)
        r = cl.recv(1024)        
        r = str(r)
        
# Onboard LED        
        led_on = r.find('?led=on')
        led_off = r.find('?led=off')
        print('led_on = ', led_on)
        print('led_off = ', led_off)
        if led_on == 10:
            print('LED ON')
            led.value(1)
            
        if led_off == 10:
            print('LED OFF')
            led.value(0)
            
# LED_1            
        led_1_on = r.find('?led_1=on')
        led_1_off = r.find('?led_1=off')
        print('led_1_on = ', led_1_on)
        print('led_1_off = ', led_1_off)
        if led_1_on == 10:
            print('LED 1 ON')
            led_1.value(1)
            
        if led_1_off == 10:
            print('LED 1 OFF')
            led_1.value(0)
            
# Light Sensor
        light_in = r.find('?light_in')
        print('Getting Box Status = {}'.format(light_in))

# Do HTTP Stuff            
        response = get_html('index.html')
        cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
        cl.send(response)
        cl.close()
        
    except OSError as e:
        cl.close()
        print('Connection closed')

index.html

HTML
<!DOCTYPE html>
<html>
    <head>
        <title>Pico W</title>
    </head>
    <body>
        <h1>Pico W</h1>
        <p>Control the onboard LED</p>
        <a href=\"?led=on\"><button>ON</button></a>&nbsp;
        <a href=\"?led=off\"><button>OFF</button></a>
        <br>
        <p>Control the breadboard LED (GP8)</p>
        <a href=\"?led_1=on\"><button>ON</button></a>&nbsp;
        <a href=\"?led_1=off\"><button>OFF</button></a>
        <br>
        <p>The light sensor (ADC0) reading is: </p>
        <p style="padding: 10px; border: 2px solid red;"> <% light_in %> </p>
        <p>
    </body>
</html>

secrets.py

MicroPython
WiFi Credentials
SSID = "<SSID Name>"
PASSWORD = "<WiFi Password>"

wifi.py

MicroPython
Wifi connection script
from time import sleep
import network
import secrets
import rp2

rp2.country('GB')

max_wait = 5

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.SSID, secrets.PASSWORD)

wifistatus = wlan.status()
status = wlan.ifconfig()

while max_wait > 0:
    if wifistatus < 0 or wifistatus >= 3:
        print("Connecting..")
        sleep(2)
        break
    max_wait -= 1

if wifistatus == 3:
    print("Wi-Fi Enabled!")
    print("IP:",status[0])

Credits

Fish
4 projects • 10 followers
Trying to make sense of it all and helping others along the way..
Contact

Comments

Please log in or sign up to comment.