Fixed temphum sensor
This commit is contained in:
@@ -15,17 +15,28 @@ white = 1 # The White colored sensor.
|
|||||||
|
|
||||||
def get_temperature_and_humidity():
|
def get_temperature_and_humidity():
|
||||||
with grove_lock:
|
with grove_lock:
|
||||||
[temp,humidity] = grovepi.dht(sensor,blue)
|
try:
|
||||||
if math.isnan(temp) == False and math.isnan(humidity) == False:
|
[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
|
return temp, humidity
|
||||||
else:
|
except Exception:
|
||||||
print("Error reading from DHT sensor")
|
pass
|
||||||
return None, None
|
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()
|
temp, humidity = get_temperature_and_humidity()
|
||||||
if temp is not None and humidity is not None:
|
if temp is not None and humidity is not None:
|
||||||
return temp, humidity
|
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
|
return None, None
|
||||||
Reference in New Issue
Block a user