import machine
from machine import PWM, Pin
import dht
from time import sleep
# Define GPIO pins
flame_pin = 14
dht_pin = 14
alarm_led_pin = 15
buzzer_pin = 16
# Setup flame sensor pin
flame_sensor = machine.Pin(flame_pin, machine.Pin.IN)
# Setup DHT22 sensor
dht_sensor = dht.DHT22(machine.Pin(dht_pin))
# Setup LED pin
alarm_led = machine.Pin(alarm_led_pin, machine.Pin.OUT)
# Setup buzzer pin
buzzer = PWM(Pin(buzzer_pin), Pin.OUT)
def check_flame_sensor():
"""
Function to check the flame sensor status.
Returns True if fire is detected, False otherwise.
"""
return flame_sensor.value() == 1
def read_temperature_humidity():
"""
Function to read temperature and humidity from DHT22 sensor.
Returns a tuple (temperature, humidity).
"""
dht_sensor.measure()
while True:
try:
sleep(2)
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
temp_f = temp * (9/5) + 32.0
print('Temperature: %3.1f C' %temp)
print('Temperature: %3.1f F' %temp_f)
print('Humidity: %3.1f %%' %hum)
return temp
except OSError as e:
print('Failed to read sensor.')
return dht_sensor.temperature(), dht_sensor.humidity()
def trigger_alarm():
"""
Function to trigger the alarm LED and buzzer.
"""
alarm_led.on()
buzzer.init(freq = 1400, duty = 400)
sleep(0.5)
buzzer.init(freq = 1, duty = 0)
sleep(0.5)
print("Heat rise detected! Alarm triggered.")
def reset_alarm():
"""
Function to reset the alarm LED and buzzer.
"""
alarm_led.off()
buzzer.init(freq = 1500, duty = 0)
sleep(1)
buzzer.init(freq = 1, duty = 0)
sleep(1)
print("Alarm reset.")
def main():
# Main loop
while True:
temperature = read_temperature_humidity()
if temperature > 40:
trigger_alarm()
else:
reset_alarm()
sleep(1) # Check temperature every 1 second
if __name__ == "__main__":
main()