1
- # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
1
+ # SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2
2
#
3
3
# SPDX-License-Identifier: MIT
4
4
16
16
import adafruit_displayio_ssd1306
17
17
import adafruit_imageload
18
18
from 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
24
20
from adafruit_onewire .bus import OneWireBus
25
21
from adafruit_ds18x20 import DS18X20
26
22
@@ -82,14 +78,14 @@ def c_to_f(temp):
82
78
83
79
print ("Connected to WiFi" )
84
80
pool = socketpool .SocketPool (wifi .radio )
85
- server = HTTPServer (pool , "/static" )
81
+ server = Server (pool , "/static" , debug = True )
86
82
87
83
# variables for HTML
88
84
# comment/uncomment desired temp unit
89
85
90
86
# temp_test = str(ds18.temperature)
91
87
# unit = "C"
92
- temp_test = str ( c_to_f (ds18 .temperature ) )
88
+ temp_test = c_to_f (ds18 .temperature )
93
89
unit = "F"
94
90
# font for HTML
95
91
font_family = "monospace"
@@ -126,7 +122,7 @@ def webpage():
126
122
<p class="dotted">This is a Pico W running an HTTP server with CircuitPython.</p>
127
123
<br>
128
124
<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>
130
126
<h1>Control the LED on the Pico W with these buttons:</h1><br>
131
127
<form accept-charset="utf-8" method="POST">
132
128
<button class="button" name="LED ON" value="ON" type="submit">LED ON</button></a></p></form>
@@ -141,15 +137,14 @@ def webpage():
141
137
142
138
# route default static IP
143
139
@server .route ("/" )
144
- def base (request : HTTPRequest ): # pylint: disable=unused-argument
140
+ def base (request : Request ): # pylint: disable=unused-argument
145
141
# serve the HTML f string
146
142
# 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' )
149
144
150
145
# 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 ):
153
148
# get the raw text
154
149
raw_text = request .raw_request .decode ("utf8" )
155
150
print (raw_text )
@@ -166,8 +161,7 @@ def buttonpress(request: HTTPRequest):
166
161
# toggle the parrot_pin value
167
162
parrot_pin .value = not parrot_pin .value
168
163
# 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' )
171
165
172
166
print ("starting server.." )
173
167
# startup the server
@@ -184,19 +178,19 @@ def buttonpress(request: HTTPRequest):
184
178
# text objects for screen
185
179
# connected to SSID text
186
180
connect_text_area .text = "Connected to:"
187
- ssid_text = "%s" % os .getenv ('WIFI_SSID' )
181
+ ssid_text = f" { os .getenv ('CIRCUITPY_WIFI_SSID' ) } "
188
182
ssid_text_area = label .Label (
189
183
terminalio .FONT , text = ssid_text , color = 0xFFFFFF , x = 0 , y = offset_y + 15
190
184
)
191
185
splash .append (ssid_text_area )
192
186
# display ip address
193
- ip_text = "IP: %s" % wifi .radio .ipv4_address
187
+ ip_text = f "IP: { wifi .radio .ipv4_address } "
194
188
ip_text_area = label .Label (
195
189
terminalio .FONT , text = ip_text , color = 0xFFFFFF , x = 0 , y = offset_y + 30
196
190
)
197
191
splash .append (ip_text_area )
198
192
# display temp reading
199
- temp_text = "Temperature: %.02f F" % float ( temp_test )
193
+ temp_text = f "Temperature: { temp_test :.2f } F"
200
194
temp_text_area = label .Label (
201
195
terminalio .FONT , text = temp_text , color = 0xFFFFFF , x = 0 , y = offset_y + 45
202
196
)
@@ -230,13 +224,13 @@ def buttonpress(request: HTTPRequest):
230
224
print ("lost connection" )
231
225
else :
232
226
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' ) } "
234
228
print ("connected" )
235
229
clock = time .monotonic ()
236
230
# 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"
240
234
241
235
#if parrot is True:
242
236
if parrot_pin .value is True :
0 commit comments