1- # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
1+ # SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
22#
33# SPDX-License-Identifier: MIT
44
1616import adafruit_displayio_ssd1306
1717import adafruit_imageload
1818from digitalio import DigitalInOut , Direction
19- from adafruit_httpserver .server import HTTPServer
20- from adafruit_httpserver .request import HTTPRequest
21- from adafruit_httpserver .response import HTTPResponse
22- from adafruit_httpserver .methods import HTTPMethod
23- from adafruit_httpserver .mime_type import MIMEType
19+ from adafruit_httpserver import Server , Request , Response , POST
2420from adafruit_onewire .bus import OneWireBus
2521from adafruit_ds18x20 import DS18X20
2622
@@ -82,14 +78,14 @@ def c_to_f(temp):
8278
8379print ("Connected to WiFi" )
8480pool = socketpool .SocketPool (wifi .radio )
85- server = HTTPServer (pool , "/static" )
81+ server = Server (pool , "/static" , debug = True )
8682
8783# variables for HTML
8884# comment/uncomment desired temp unit
8985
9086# temp_test = str(ds18.temperature)
9187# unit = "C"
92- temp_test = str ( c_to_f (ds18 .temperature ) )
88+ temp_test = c_to_f (ds18 .temperature )
9389unit = "F"
9490# font for HTML
9591font_family = "monospace"
@@ -126,7 +122,7 @@ def webpage():
126122 <p class="dotted">This is a Pico W running an HTTP server with CircuitPython.</p>
127123 <br>
128124 <p class="dotted">The current ambient temperature near the Pico W is
129- <span style="color: deeppink;">{ temp_test } °{ unit } </span></p><br>
125+ <span style="color: deeppink;">{ temp_test :.2f } °{ unit } </span></p><br>
130126 <h1>Control the LED on the Pico W with these buttons:</h1><br>
131127 <form accept-charset="utf-8" method="POST">
132128 <button class="button" name="LED ON" value="ON" type="submit">LED ON</button></a></p></form>
@@ -141,15 +137,14 @@ def webpage():
141137
142138# route default static IP
143139@server .route ("/" )
144- def base (request : HTTPRequest ): # pylint: disable=unused-argument
140+ def base (request : Request ): # pylint: disable=unused-argument
145141 # serve the HTML f string
146142 # with content type text/html
147- with HTTPResponse (request , content_type = MIMEType .TYPE_HTML ) as response :
148- response .send (f"{ webpage ()} " )
143+ return Response (request , f"{ webpage ()} " , content_type = 'text/html' )
149144
150145# if a button is pressed on the site
151- @server .route ("/" , method = HTTPMethod . POST )
152- def buttonpress (request : HTTPRequest ):
146+ @server .route ("/" , POST )
147+ def buttonpress (request : Request ):
153148 # get the raw text
154149 raw_text = request .raw_request .decode ("utf8" )
155150 print (raw_text )
@@ -166,8 +161,7 @@ def buttonpress(request: HTTPRequest):
166161 # toggle the parrot_pin value
167162 parrot_pin .value = not parrot_pin .value
168163 # reload site
169- with HTTPResponse (request , content_type = MIMEType .TYPE_HTML ) as response :
170- response .send (f"{ webpage ()} " )
164+ return Response (request , f"{ webpage ()} " , content_type = 'text/html' )
171165
172166print ("starting server.." )
173167# startup the server
@@ -184,19 +178,19 @@ def buttonpress(request: HTTPRequest):
184178# text objects for screen
185179# connected to SSID text
186180connect_text_area .text = "Connected to:"
187- ssid_text = "%s" % os .getenv ('WIFI_SSID' )
181+ ssid_text = f" { os .getenv ('CIRCUITPY_WIFI_SSID' ) } "
188182ssid_text_area = label .Label (
189183 terminalio .FONT , text = ssid_text , color = 0xFFFFFF , x = 0 , y = offset_y + 15
190184)
191185splash .append (ssid_text_area )
192186# display ip address
193- ip_text = "IP: %s" % wifi .radio .ipv4_address
187+ ip_text = f "IP: { wifi .radio .ipv4_address } "
194188ip_text_area = label .Label (
195189 terminalio .FONT , text = ip_text , color = 0xFFFFFF , x = 0 , y = offset_y + 30
196190)
197191splash .append (ip_text_area )
198192# display temp reading
199- temp_text = "Temperature: %.02f F" % float ( temp_test )
193+ temp_text = f "Temperature: { temp_test :.2f } F"
200194temp_text_area = label .Label (
201195 terminalio .FONT , text = temp_text , color = 0xFFFFFF , x = 0 , y = offset_y + 45
202196)
@@ -230,13 +224,13 @@ def buttonpress(request: HTTPRequest):
230224 print ("lost connection" )
231225 else :
232226 connect_text_area .text = "Connected to:"
233- ssid_text_area .text = "%s" % os .getenv ('WIFI_SSID' )
227+ ssid_text_area .text = f" { os .getenv ('CIRCUITPY_WIFI_SSID' ) } "
234228 print ("connected" )
235229 clock = time .monotonic ()
236230 # comment/uncomment for desired units
237- # temp_test = str( ds18.temperature)
238- temp_test = str ( c_to_f (ds18 .temperature ) )
239- temp_text_area .text = "Temperature: %s F" % temp_test
231+ # temp_test = ds18.temperature
232+ temp_test = c_to_f (ds18 .temperature )
233+ temp_text_area .text = f "Temperature: { temp_test :.2f } F"
240234
241235 #if parrot is True:
242236 if parrot_pin .value is True :
0 commit comments