8
8
9
9
import time
10
10
import os
11
- import ssl
12
11
import microcontroller
13
12
import supervisor
13
+ import ssl
14
14
import socketpool
15
15
import wifi
16
16
import board
17
+ import digitalio
17
18
import alarm
18
19
import neopixel
19
20
import adafruit_hcsr04
22
23
import adafruit_requests
23
24
import adafruit_max1704x
24
25
26
+
27
+
28
+ # Initialize the power pin for the sensor
29
+ sensor_power = digitalio .DigitalInOut (board .A2 )
30
+ sensor_power .direction = digitalio .Direction .OUTPUT
31
+ sensor_power .value = False # Start with sensor powered off
32
+
33
+ def power_sensor_on ():
34
+ """Turn on power to the ultrasonic sensor and wait for it to stabilize."""
35
+ sensor_power .value = True
36
+ time .sleep (0.55 ) # Give sensor time to power up and stabilize
37
+
38
+ def power_sensor_off ():
39
+ """Turn off power to the ultrasonic sensor."""
40
+ sensor_power .value = False
41
+
25
42
# Initialize the sonar sensor
26
43
sonar = adafruit_hcsr04 .HCSR04 (trigger_pin = board .A0 , echo_pin = board .A1 )
27
44
46
63
47
64
# Operating hours (24-hour format with minutes, e.g., "6:35" and "16:00")
48
65
OPENING_TIME = "6:00"
49
- CLOSING_TIME = "22 :30"
66
+ CLOSING_TIME = "15 :30"
50
67
# Normal operation check interval
51
- NORMAL_CHECK_MINUTES = 5
68
+ NORMAL_CHECK_MINUTES = 10
52
69
# Sleep duration in seconds during operating hours
53
70
SLEEP_DURATION = 60 * NORMAL_CHECK_MINUTES
54
71
# Display duration in seconds
58
75
59
76
def parse_time (time_str ):
60
77
"""Convert time string (HH:MM format) to hours and minutes."""
61
- # pylint: disable=redefined-outer-name
62
78
parts = time_str .split (':' )
63
79
return int (parts [0 ]), int (parts [1 ])
64
80
65
81
def get_average_distance ():
66
82
"""Take multiple distance readings and return the average."""
83
+ power_sensor_on () # Power on the sensor before taking measurements
67
84
distances = []
68
85
for _ in range (NUM_SAMPLES ):
69
86
try :
@@ -73,6 +90,7 @@ def get_average_distance():
73
90
except RuntimeError :
74
91
print ("Error reading distance" )
75
92
continue
93
+ power_sensor_off () # Power off the sensor after measurements
76
94
77
95
# Only average valid readings
78
96
if distances :
@@ -103,9 +121,7 @@ def set_pixel_color(distance):
103
121
avg_distance = get_average_distance ()
104
122
105
123
if avg_distance is not None :
106
-
107
124
if avg_distance >= 22 :
108
- # pylint: disable=invalid-name
109
125
avg_distance = 22
110
126
print (f"Average distance: { avg_distance :.1f} cm" )
111
127
# Set color based on average distance
@@ -118,7 +134,6 @@ def set_pixel_color(distance):
118
134
119
135
# Try connecting to WiFi
120
136
try :
121
-
122
137
print ("Connecting to %s" % os .getenv ("CIRCUITPY_WIFI_SSID" ))
123
138
# Show pink while attempting to connect
124
139
pixel .fill (PINK )
@@ -127,7 +142,6 @@ def set_pixel_color(distance):
127
142
# Show cyan on successful connection
128
143
pixel .fill (CYAN )
129
144
time .sleep (1 ) # Brief pause to show the connection success
130
- # pylint: disable=broad-except
131
145
except Exception as e :
132
146
print ("Failed to connect to WiFi. Error:" , e , "\n Board will hard reset in 30 seconds." )
133
147
pixel .fill (OFF )
@@ -160,7 +174,6 @@ def set_pixel_color(distance):
160
174
aio_username = os .getenv ("ADAFRUIT_AIO_USERNAME" )
161
175
aio_key = os .getenv ("ADAFRUIT_AIO_KEY" )
162
176
timezone = os .getenv ("TIMEZONE" )
163
- # pylint: disable=line-too-long
164
177
TIME_URL = f"https://io.adafruit.com/api/v2/{ aio_username } /integrations/time/strftime?x-aio-key={ aio_key } &tz={ timezone } "
165
178
TIME_URL += "&fmt=%25Y-%25m-%25d+%25H%3A%25M%3A%25S.%25L+%25j+%25u+%25z+%25Z"
166
179
@@ -209,7 +222,6 @@ def set_pixel_color(distance):
209
222
time .sleep (DISPLAY_DURATION )
210
223
211
224
# Use normal check interval during operating hours
212
- # # pylint: disable=invalid-name
213
225
sleep_seconds = SLEEP_DURATION
214
226
print (f"Next check in { NORMAL_CHECK_MINUTES } minutes" )
215
227
else :
@@ -233,7 +245,6 @@ def set_pixel_color(distance):
233
245
234
246
response .close ()
235
247
236
- # pylint: disable=broad-except
237
248
except Exception as e :
238
249
print ("Failed to get or send data, or connect. Error:" , e ,
239
250
"\n Board will hard reset in 30 seconds." )
@@ -244,14 +255,13 @@ def set_pixel_color(distance):
244
255
else :
245
256
print ("Failed to get valid distance readings" )
246
257
pixel .fill (OFF )
247
- # pylint: disable=invalid-name
248
258
sleep_seconds = SLEEP_DURATION # Use normal interval if we couldn't get readings
249
259
250
260
# Prepare for deep sleep
251
261
pixel .brightness = 0 # Turn off NeoPixel
262
+ power_sensor_off () # Make sure sensor is powered off before sleep
252
263
253
264
# Flush the serial output before sleep
254
- # pylint: disable=pointless-statement
255
265
supervisor .runtime .serial_bytes_available
256
266
time .sleep (0.05 )
257
267
0 commit comments