Skip to content

Instantly share code, notes, and snippets.

@JeremySCook
Created December 12, 2022 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeremySCook/facf2a5161fdf5ceee41c1aeed8cd8fd to your computer and use it in GitHub Desktop.
Save JeremySCook/facf2a5161fdf5ceee41c1aeed8cd8fd to your computer and use it in GitHub Desktop.
#original code via: Neradoc https://github.com/Neradoc
import os
import socketpool
import wifi
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_httpserver.server import HTTPServer
from adafruit_httpserver.response import HTTPResponse
PORT = 80
LED_PIN = board.LED
BUTTON_PIN = board.GP16
led1 = DigitalInOut(board.GP14)
led2 = DigitalInOut(board.GP15)
led1.direction = Direction.OUTPUT
led2.direction = Direction.OUTPUT
# led = DigitalInOut(LED_PIN)
# led.switch_to_output(False)
button = DigitalInOut(BUTTON_PIN)
button.pull = Pull.UP
wifi.radio.connect('WIFI_SSID','WIFI_PASSWORD')
#wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD')) # uses a .env file
print(f"Listening on http://{wifi.radio.ipv4_address}:{PORT}")
pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool)
index_html = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W HTTP Server</h1>
<p>Hello, World!</p>
<p>{body}</p>
<p><a href="?led=on">LED ON</a> - <a href="?led=off">LED OFF</a></p>
</body>
</html>
"""
@server.route("/")
def base(request):
# read the query parameters
if 'led' in request.query_params:
if request.query_params['led'] == "on":
led1.value = True
elif request.query_params['led'] == "off":
led1.value = False
# prepare the report of the LED status
if led1.value:
led_state = "Server LED is ON"
else:
led_state = "Server LED is OFF"
# prepare the report of the button status
if button.value:
button_state = "Button is NOT pressed"
#led2.value = False
else:
button_state = "Button is pressed"
#led2.value = True
# put all of that in a page
page_output = index_html.format(
body=f"{led_state} and {button_state}"
)
# send
return HTTPResponse(body=page_output, content_type="html")
# startup the server
server.start(str(wifi.radio.ipv4_address), PORT)
while True:
if button.value:
led2.value = False
else:
led2.value = True
try:
# process any waiting requests
server.poll()
except OSError:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment