Controlling a LED via the web
When you’re writing software for hardware, turning an LED on, off, and then on again, is typically the first program that gets run in a new programming environment. Learning how to blink an LED gets you half way to anywhere. We’re going to do exactly that, via a web browser.
Connecting you Raspberry Pi Pico W to a LED.
Connecting your Raspberry Pi Pico W to a LED.
We are in fact going to build a RESTful(ish) web server to control our LED.
I’ve chosen to attach an external LED to GP15 of our Raspberry Pi Pico W, but you could just as easily use the on-board LED for testing things out. Open up Thonny, and upload the following Python script to your Pico W. If you haven’t used MicroPython and Thonny before, full instructions on how to do that can be found in the Raspberry Pi Pico Python SDK book.
from machine import Pin
led = Pin(15, Pin.OUT)
ssid = ‘YOUR NETWORK NAME’
password = ‘YOUR NETWORK PASSWORD’
wlan = network.WLAN(network.STA_IF)
html = “””
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
max_wait -= 1
print(‘waiting for connection…’)
if wlan.status() != 3:
raise RuntimeError(‘network connection failed’)
status = wlan.ifconfig()
print( ‘ip = ‘ + status )
addr = socket.getaddrinfo(‘0.0.0.0’, 80)[-1]
s = socket.socket()
print(‘listening on’, addr)
Listen for connections
cl, addr = s.accept()
print(‘client connected from’, addr)
request = cl.recv(1024)
request = str(request) led_on = request.find('/light/on') led_off = request.find('/light/off') print( 'led on = ' + str(led_on)) print( 'led off = ' + str(led_off)) if led_on == 6: print("led on") led.value(1) stateis = "LED is ON" if led_off == 6: print("led off") led.value(0) stateis = "LED is OFF" response = html % stateis 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')
Make sure to replace the ssid and password with the name and password for your own wireless network at home, and then hit the button to run the script on your Pico W.
Running our Python script on Pico W from the Thonny editor
After your Pico W connects to your wireless network, you should see the the IP address for your board appear on the REPL inside the Thonny shell window.
Turning our LED on from a web browser
You should subsitute your IP address, which for most home networks will probably be in the 192.168.1.X range, for the one shown here.
This example lets you remotely turn an LED on, and then off again. However, we can also extend this example to add buttons to the web page we’re serving to allow you to control the LED directly from a web interface rather than by using a RESTful server — which after all is more suited to programmatic use rather than working from a web browser. Alternatively we can go further and reimplement our server, so that rather than blocking, it will operate asynchronously.
More information about connecting your Pico W to the web can be found in the online documentation and the “Connecting to the internet with Raspberry
Pi Pico W” book.