From 93c450259a79748b8dec67d32b25e10af1b48a78 Mon Sep 17 00:00:00 2001 From: jedgarpark Date: Wed, 12 Feb 2025 12:34:24 -0800 Subject: [PATCH 1/2] updated espresso code to power down sensor --- Espresso_Water_Meter/code.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/Espresso_Water_Meter/code.py b/Espresso_Water_Meter/code.py index 7991e7b73..7dea92019 100644 --- a/Espresso_Water_Meter/code.py +++ b/Espresso_Water_Meter/code.py @@ -8,12 +8,13 @@ import time import os -import ssl import microcontroller import supervisor +import ssl import socketpool import wifi import board +import digitalio import alarm import neopixel import adafruit_hcsr04 @@ -22,6 +23,22 @@ import adafruit_requests import adafruit_max1704x + + +# Initialize the power pin for the sensor +sensor_power = digitalio.DigitalInOut(board.A2) +sensor_power.direction = digitalio.Direction.OUTPUT +sensor_power.value = False # Start with sensor powered off + +def power_sensor_on(): + """Turn on power to the ultrasonic sensor and wait for it to stabilize.""" + sensor_power.value = True + time.sleep(0.55) # Give sensor time to power up and stabilize + +def power_sensor_off(): + """Turn off power to the ultrasonic sensor.""" + sensor_power.value = False + # Initialize the sonar sensor sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.A0, echo_pin=board.A1) @@ -46,9 +63,9 @@ # Operating hours (24-hour format with minutes, e.g., "6:35" and "16:00") OPENING_TIME = "6:00" -CLOSING_TIME = "22:30" +CLOSING_TIME = "15:30" # Normal operation check interval -NORMAL_CHECK_MINUTES = 5 +NORMAL_CHECK_MINUTES = 10 # Sleep duration in seconds during operating hours SLEEP_DURATION = 60 * NORMAL_CHECK_MINUTES # Display duration in seconds @@ -58,12 +75,12 @@ def parse_time(time_str): """Convert time string (HH:MM format) to hours and minutes.""" - # pylint: disable=redefined-outer-name parts = time_str.split(':') return int(parts[0]), int(parts[1]) def get_average_distance(): """Take multiple distance readings and return the average.""" + power_sensor_on() # Power on the sensor before taking measurements distances = [] for _ in range(NUM_SAMPLES): try: @@ -73,6 +90,7 @@ def get_average_distance(): except RuntimeError: print("Error reading distance") continue + power_sensor_off() # Power off the sensor after measurements # Only average valid readings if distances: @@ -103,9 +121,7 @@ def set_pixel_color(distance): avg_distance = get_average_distance() if avg_distance is not None: - if avg_distance >= 22: - # pylint: disable=invalid-name avg_distance = 22 print(f"Average distance: {avg_distance:.1f} cm") # Set color based on average distance @@ -118,7 +134,6 @@ def set_pixel_color(distance): # Try connecting to WiFi try: - print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) # Show pink while attempting to connect pixel.fill(PINK) @@ -127,7 +142,6 @@ def set_pixel_color(distance): # Show cyan on successful connection pixel.fill(CYAN) time.sleep(1) # Brief pause to show the connection success - # pylint: disable=broad-except except Exception as e: print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds.") pixel.fill(OFF) @@ -160,7 +174,6 @@ def set_pixel_color(distance): aio_username = os.getenv("ADAFRUIT_AIO_USERNAME") aio_key = os.getenv("ADAFRUIT_AIO_KEY") timezone = os.getenv("TIMEZONE") - # pylint: disable=line-too-long TIME_URL = f"https://io.adafruit.com/api/v2/{aio_username}/integrations/time/strftime?x-aio-key={aio_key}&tz={timezone}" TIME_URL += "&fmt=%25Y-%25m-%25d+%25H%3A%25M%3A%25S.%25L+%25j+%25u+%25z+%25Z" @@ -209,7 +222,6 @@ def set_pixel_color(distance): time.sleep(DISPLAY_DURATION) # Use normal check interval during operating hours - # # pylint: disable=invalid-name sleep_seconds = SLEEP_DURATION print(f"Next check in {NORMAL_CHECK_MINUTES} minutes") else: @@ -233,7 +245,6 @@ def set_pixel_color(distance): response.close() - # pylint: disable=broad-except except Exception as e: print("Failed to get or send data, or connect. Error:", e, "\nBoard will hard reset in 30 seconds.") @@ -244,14 +255,13 @@ def set_pixel_color(distance): else: print("Failed to get valid distance readings") pixel.fill(OFF) - # pylint: disable=invalid-name sleep_seconds = SLEEP_DURATION # Use normal interval if we couldn't get readings # Prepare for deep sleep pixel.brightness = 0 # Turn off NeoPixel +power_sensor_off() # Make sure sensor is powered off before sleep # Flush the serial output before sleep -# pylint: disable=pointless-statement supervisor.runtime.serial_bytes_available time.sleep(0.05) From d64251b18ae2fde47ae96e305eb859fd064b11b3 Mon Sep 17 00:00:00 2001 From: jedgarpark Date: Wed, 12 Feb 2025 12:48:06 -0800 Subject: [PATCH 2/2] pylint fixes --- Espresso_Water_Meter/code.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Espresso_Water_Meter/code.py b/Espresso_Water_Meter/code.py index 7dea92019..fba430e16 100644 --- a/Espresso_Water_Meter/code.py +++ b/Espresso_Water_Meter/code.py @@ -8,9 +8,9 @@ import time import os +import ssl import microcontroller import supervisor -import ssl import socketpool import wifi import board @@ -23,8 +23,6 @@ import adafruit_requests import adafruit_max1704x - - # Initialize the power pin for the sensor sensor_power = digitalio.DigitalInOut(board.A2) sensor_power.direction = digitalio.Direction.OUTPUT @@ -75,6 +73,7 @@ def power_sensor_off(): def parse_time(time_str): """Convert time string (HH:MM format) to hours and minutes.""" + # pylint: disable=redefined-outer-name parts = time_str.split(':') return int(parts[0]), int(parts[1]) @@ -121,7 +120,9 @@ def set_pixel_color(distance): avg_distance = get_average_distance() if avg_distance is not None: + if avg_distance >= 22: + # pylint: disable=invalid-name avg_distance = 22 print(f"Average distance: {avg_distance:.1f} cm") # Set color based on average distance @@ -134,6 +135,7 @@ def set_pixel_color(distance): # Try connecting to WiFi try: + print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID")) # Show pink while attempting to connect pixel.fill(PINK) @@ -142,6 +144,7 @@ def set_pixel_color(distance): # Show cyan on successful connection pixel.fill(CYAN) time.sleep(1) # Brief pause to show the connection success + # pylint: disable=broad-except except Exception as e: print("Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds.") pixel.fill(OFF) @@ -174,6 +177,7 @@ def set_pixel_color(distance): aio_username = os.getenv("ADAFRUIT_AIO_USERNAME") aio_key = os.getenv("ADAFRUIT_AIO_KEY") timezone = os.getenv("TIMEZONE") + # pylint: disable=line-too-long TIME_URL = f"https://io.adafruit.com/api/v2/{aio_username}/integrations/time/strftime?x-aio-key={aio_key}&tz={timezone}" TIME_URL += "&fmt=%25Y-%25m-%25d+%25H%3A%25M%3A%25S.%25L+%25j+%25u+%25z+%25Z" @@ -222,6 +226,7 @@ def set_pixel_color(distance): time.sleep(DISPLAY_DURATION) # Use normal check interval during operating hours + # # pylint: disable=invalid-name sleep_seconds = SLEEP_DURATION print(f"Next check in {NORMAL_CHECK_MINUTES} minutes") else: @@ -245,6 +250,7 @@ def set_pixel_color(distance): response.close() + # pylint: disable=broad-except except Exception as e: print("Failed to get or send data, or connect. Error:", e, "\nBoard will hard reset in 30 seconds.") @@ -255,13 +261,15 @@ def set_pixel_color(distance): else: print("Failed to get valid distance readings") pixel.fill(OFF) + # pylint: disable=invalid-name sleep_seconds = SLEEP_DURATION # Use normal interval if we couldn't get readings # Prepare for deep sleep -pixel.brightness = 0 # Turn off NeoPixel power_sensor_off() # Make sure sensor is powered off before sleep +pixel.brightness = 0 # Turn off NeoPixel # Flush the serial output before sleep +# pylint: disable=pointless-statement supervisor.runtime.serial_bytes_available time.sleep(0.05)