import Adafruit_DHT
from gpiozero import LED
import time
# Define the sensor type and pin number
DHT_SENSOR = Adafruit_DHT.DHT22 # Change to DHT11 if using that sensor
DHT_PIN = 4 # GPIO pin for DHT sensor
# Define the LED pin
led = LED(17) # GPIO pin for LED
# Define temperature thresholds (in Celsius)
LOW_THRESHOLD = 15.0 # Minimum acceptable temperature
HIGH_THRESHOLD = 30.0 # Maximum acceptable temperature
while True:
# Read the humidity and temperature from the DHT sensor
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
if temperature is not None:
print(f'Temperature={temperature:.1f}°C Humidity={humidity:.1f}%')
# Check if temperature is outside defined thresholds
if temperature < LOW_THRESHOLD or temperature > HIGH_THRESHOLD:
led.on() # Turn on LED if outside thresholds
print("Alert! Temperature out of range!")
else:
led.off() # Turn off LED if within thresholds
else:
print("Failed to retrieve data from humidity sensor")
time.sleep(10) # Wait for 10 seconds before next reading