Skip to content

Commit 36ada53

Browse files
authored
Update PicoW_CircuitPython_HTTP_Server for new adafruit_httpserver library version 2.0.0
Tested the edited routes on `Adafruit CircuitPython 8.0.0-beta.6 on 2022-12-21; Raspberry Pi Pico W with rp2040`, but without the static ipconfig, outputs, and displayio.
1 parent d67dd66 commit 36ada53

File tree

1 file changed

+8
-4
lines changed
  • PicoW_CircuitPython_HTTP_Server

1 file changed

+8
-4
lines changed

PicoW_CircuitPython_HTTP_Server/code.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from digitalio import DigitalInOut, Direction
1919
from adafruit_httpserver.server import HTTPServer
2020
from adafruit_httpserver.response import HTTPResponse
21+
from adafruit_httpserver.methods import HTTPMethod
22+
from adafruit_httpserver.mime_type import MIMEType
2123
from adafruit_onewire.bus import OneWireBus
2224
from adafruit_ds18x20 import DS18X20
2325

@@ -138,13 +140,14 @@ def webpage():
138140

139141
# route default static IP
140142
@server.route("/")
141-
def base(request): # pylint: disable=unused-argument
143+
def base(request: HTTPRequest): # pylint: disable=unused-argument
142144
# serve the HTML f string
143145
# with content type text/html
144-
return HTTPResponse(content_type="text/html", body=webpage())
146+
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
147+
response.send(f"{webpage()}")
145148

146149
# if a button is pressed on the site
147-
@server.route("/", "POST")
150+
@server.route("/", method=HTTPMethod.POST)
148151
def buttonpress(request):
149152
# get the raw text
150153
raw_text = request.raw_request.decode("utf8")
@@ -162,7 +165,8 @@ def buttonpress(request):
162165
# toggle the parrot_pin value
163166
parrot_pin.value = not parrot_pin.value
164167
# reload site
165-
return HTTPResponse(content_type="text/html", body=webpage())
168+
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
169+
response.send(f"{webpage()}")
166170

167171
print("starting server..")
168172
# startup the server

0 commit comments

Comments
 (0)