Created
February 20, 2023 17:30
-
-
Save jmccardle/7a6bf2937f83da6f120056982e8f3088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pico W GPIO-over-website test | |
# thanks to https://www.petecodes.co.uk/creating-a-basic-raspberry-pi-pico-web-server-using-micropython/ for the kickstart | |
import network | |
import socket | |
import time | |
from machine import Pin | |
import secrets | |
# secrets.py defines SSID and PASSWORD variables, used on line 12 - don't expose your SSID/passphrase to github, please | |
import machine | |
wlan = network.WLAN(network.STA_IF) | |
wlan.active(True) | |
wlan.connect(secrets.SSID, secrets.PASSWORD) | |
builtin_led = Pin("LED", Pin.OUT) | |
gpio_led = Pin(1, Pin.OUT) | |
# if wlan.isconnected(): | |
# print(f"Connected: IP is {wlan.ifconfig()[0]}") | |
# else: | |
# print("Not connected") | |
data = {"builtin_led": False, "gpio_led": False, "msg": None} | |
def fmt_html(): | |
builtin_url = f'''/set/builtin/{0 if data['builtin_led'] else 1}''' | |
builtin_txt = 'Off' if data['builtin_led'] else 'On' | |
gpio_url = f'''/set/gpio/{0 if data['gpio_led'] else 1}''' | |
gpio_txt = 'Off' if data['gpio_led'] else 'On' | |
flash_txt = "" | |
if data['msg']: | |
flash_txt = '<p><b>Server Message:</b>' + str(data['msg']) + '</p>' | |
return f"""<!DOCTYPE html> | |
<html> | |
<head> <title>Pico W</title> </head> | |
<body> <h1>Pico W</h1> | |
{flash_txt} | |
<p> <b>Built-in LED state:</b> {data['builtin_led']} | |
<a href="{builtin_url}"> | |
Turn {builtin_txt} | |
</a> | |
</p> | |
<p> <b>GPIO LED state:</b> {data['gpio_led']} | |
<a href="{gpio_url}"> | |
Turn {gpio_txt} | |
</a> | |
</p> | |
</body> | |
</html> | |
""" | |
# Wait for connect or fail | |
max_wait = 10 | |
while max_wait > 0: | |
if wlan.status() < 0 or wlan.status() >= 3: | |
break | |
max_wait -= 1 | |
print('waiting for connection...') | |
time.sleep(1) | |
# Handle connection error | |
if wlan.status() != 3: | |
raise RuntimeError('network connection failed') | |
else: | |
print('connected') | |
status = wlan.ifconfig() | |
print( 'ip = ' + status[0] ) | |
# Open socket | |
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] | |
s = socket.socket() | |
s.bind(addr) | |
s.listen(1) | |
print('listening on', addr) | |
# Listen for connections | |
while True: | |
try: | |
cl, addr = s.accept() | |
#print('client connected from', addr) | |
request = cl.recv(1024) | |
#print(request) | |
data['msg'] = None | |
request = str(request) | |
request_url = "/" | |
try: | |
request_url = request.split(" ")[1] | |
except: pass | |
if request_url.startswith("/set/"): | |
data['msg'] = "Set: " | |
value = request_url.split('/')[-1].lower() in ("true", "1", "on") | |
if request_url.startswith("/set/gpio"): | |
gpio_led.value(value) | |
data['msg'] += f"GPIO: set to {value} (was {data['gpio_led']})" | |
data['gpio_led'] = value | |
elif request_url.startswith("/set/builtin"): | |
builtin_led.value(value) | |
data['msg'] += f"Built-in: set to {value} (was {data['builtin_led']})" | |
data['builtin_led'] = value | |
elif request_url.startswith("/raw"): | |
data['msg'] = str(data) | |
print(request_url) | |
print(data) | |
#response = html.format(**data) | |
response = fmt_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') | |
# except Exception as e: | |
# print(e) | |
# time.sleep(3.0) | |
# machine.reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment