31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
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:
|
|
[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
|
|
|
|
def get_temperature_and_humidity_with_retry(max_retries=3):
|
|
for _ in range(max_retries): # Try up to max_retries times
|
|
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
|
|
return None, None |