from machine import Pin, PWM, SoftI2C
import time
import dht
import wifi
import ifttt
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import urequests as requests
# setup
ledRed = Pin(13, Pin.OUT)
dht22_sensor = dht.DHT22(Pin(32, Pin.IN))
pir_sensor = Pin(25, Pin.IN)
servo = PWM(Pin(27), freq=50, duty=0)
buzzer1 = PWM(Pin(12))
buzzer2 = PWM(Pin(14))
buzzer1.freq(1024)
buzzer1.freq(512)
buzzer1.duty(0)
buzzer2.duty(0)
# Setup LCD
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(18), sda=Pin(19), freq=100000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
lcd.move_to(0, 0);
lcd.putstr("Cyber Coffee");
lcd.move_to(0, 1);
lcd.putstr("Fire Alarm")
#Connect to WiFi
wifi.connect_ap()
def automatic_door():
motion_val = pir_sensor.value()
if (motion_val == 1):
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(f"Somebody is comming...")
print("Motion Detection")
servo.duty_ns(1500000)
ledRed.on()
time.sleep(4)
else:
ledRed.off()
servo.duty_ns(500000)
def fire_alarm(mail_send):
dht22_sensor.measure()
t = dht22_sensor.temperature()
time.sleep(4)
print("Temperature in the room: ",str(t)+"°C")
#Print to LCD
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Temperature:")
lcd.move_to(0, 1);
lcd.putstr(f"{t} C")
#Check if temperature in the room > 57°C then turn on fire alarm system
if t > 57:
print("**FIRE ALERT**")
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(f"**FIRE DETECTION IN THE ROOM**")
ledRed.on()
time.sleep(0.4)
ledRed.off()
buzzer1.duty(50)
time.sleep(0.4)
buzzer1.duty(0)
buzzer2.duty(50)
time.sleep(0.5)
buzzer2.duty(0)
#send mail alert
if mail_send == 0:
ifttt.fire_alert(t)
# mail_send += 1
else:
ledRed.off()
buzzer1.duty(0)
buzzer2.duty(0)
mail_send = 0
if __name__ == '__main__':
while True:
fire_alarm(0)
automatic_door()