import grovepi import math import time from sensors.lock import grove_lock # Connect the Grove Temperature & Humidity Sensor Pro to digital port D3 # This example uses the blue colored sensor. # SIG,NC,VCC,GND sensor = 3 # The Sensor goes on digital port 3. # temp_humidity_sensor_type # Grove Base Kit comes with the blue sensor. blue = 0 # The Blue colored sensor. white = 1 # The White colored sensor. def get_temperature_and_humidity(): with grove_lock: try: [temp, humidity] = grovepi.dht(sensor, blue) if not math.isnan(temp) and not math.isnan(humidity) and temp > -40 and humidity >= 0: return temp, humidity except Exception: pass return None, None def get_temperature_and_humidity_with_retry(max_retries=4, retry_delay=1.5): """ Reads temperature and humidity with retries. Uses a 1.5s delay between attempts to respect DHT sampling limits. """ for attempt in range(max_retries): temp, humidity = get_temperature_and_humidity() if temp is not None and humidity is not None: return temp, humidity # Wait before retrying (DHT sensors require >= 1s between reads) if attempt < max_retries - 1: time.sleep(retry_delay) print("Warning: All DHT sensor read attempts failed.") return None, None