From 0469b3d406c3009a903ae353926ab7b1e79ab92b Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Sun, 16 Aug 2026 14:23:28 +0200 Subject: [PATCH] Fixed temphum sensor --- orchestrateur/sensors/temp_hum.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/orchestrateur/sensors/temp_hum.py b/orchestrateur/sensors/temp_hum.py index 3991228..316c5c5 100644 --- a/orchestrateur/sensors/temp_hum.py +++ b/orchestrateur/sensors/temp_hum.py @@ -15,17 +15,28 @@ white = 1 # The White colored sensor. def get_temperature_and_humidity(): with grove_lock: - [temp,humidity] = grovepi.dht(sensor,blue) - if math.isnan(temp) == False and math.isnan(humidity) == False: - return temp, humidity - else: - print("Error reading from DHT sensor") - return None, None + 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=3): - for _ in range(max_retries): # Try up to max_retries times + +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 - time.sleep(1) # Wait a bit before retrying + + # 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 \ No newline at end of file