# Pin definition
relay1_pin = 19
relay2_pin = 21
lpg_detector_pin = 15
fire_detector_pin = 2
trigger_pin = 5
echo_pin = 18
led_pin = 16
from machine import Pin, PWM
import utime
from machine import Pin
import network
import urequests
import ujson as json
# Create the connection to our Firebase database - don't forget to change the URL!
import ufirebase as firebase
firebase.setURL("https://cat-care-ba8e5-default-rtdb.firebaseio.com/")
# Firebase Setup
firebase_url = "https://cat-care-ba8e5-default-rtdb.firebaseio.com/cateCare/"
# Wifi Configuration
ssid = 'Wokwi-GUEST' #Your network name
password = '' #Your WiFi password
led_onboard = Pin(4, Pin.OUT)
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
led_onboard.value(0)
print(f'Connecting {ssid}...')
utime.sleep(1)
ip = wlan.ifconfig()[0]
print('Connection successful')
print(f'Connected on {ip} \n')
# led_onboard.value(1)
def fetch_data_from_firebase(path):
try:
response = urequests.get(f"{firebase_url}/{path}.json")
# print("Response:", response)
return json.loads(response.text)
except Exception as e:
print("Error getting data:", e)
return None
# # Pin definition
# relay1_pin = 14
# relay2_pin = 15
# lpg_detector_pin = 12
# fire_detector_pin = 13
# trigger_pin = 10
# echo_pin = 11
# led_pin = 16
# GPIO pins setup
relay1 = Pin(relay1_pin, Pin.OUT)
relay2 = Pin(relay2_pin, Pin.OUT)
lpg_detector = Pin(lpg_detector_pin, Pin.IN)
fire_detector = Pin(fire_detector_pin, Pin.IN)
trigger = Pin(trigger_pin, Pin.OUT)
echo = Pin(echo_pin, Pin.IN)
red_led = Pin(led_pin, Pin.OUT)
# Sound an alarm (using a PWM pin for now, connect to a buzzer)
alarm = PWM(Pin(2))
def sound_alarm(frequency):
# alarm.freq(frequency) # 1kHz frequency
alarm.duty_u16(512) # 50% duty cycle
def stop_alarm(): # Stop the alarm
alarm.deinit()
def read_distance():
# this function will read distance from distance sensor
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:# Wait until we get sound back on echo pin
pass
start = utime.ticks_us() # note down current receiving time
while echo.value() == 1: # Wait until we are getting sound signal back
pass
end = utime.ticks_us() # note down the time
distance = (end - start) / 2 / 29.1 # Calculate distance in cm
return distance
while True:
try:
# print("Starting")
button1 = fetch_data_from_firebase('btn1')
button2 = fetch_data_from_firebase('btn2')
print(button1, button2)
if button1:
print("Turn on LED1")
led_onboard.value(1)
relay1.value(1) # Turn on relay 1
else:
print("Turn off LED1")
led_onboard.value(0)
relay1.value(0) # Turn off relay 1
if button2:
print("Turn on LED2")
relay2.value(1) # Turn on relay 1
else:
print("Turn off LED2")
relay2.value(0) # Turn off relay 1
# LPG & Fire Detection if signal is high
if lpg_detector.value(): # When there's lpg leak set lpg flag 1
sound_alarm(420)
firebase.put("cateCare/lpg", True, bg=0)
print("LPG Detected")
else:
firebase.put("cateCare/lpg", False, bg=0)
stop_alarm()
print("LPG Reset")
if fire_detector.value():
sound_alarm(840)
print("Fire Detected")
firebase.put("cateCare/fire", True, bg=0)# When there's fire set lpg flag 1
else:
sound_alarm(840)
stop_alarm()
print("Fire Reset")
firebase.put("cateCare/fire", False, bg=0)
if lpg_detector.value() == 0 and fire_detector.value() == 0:
stop_alarm()
# Distance Sensor Check
print("Distance:", read_distance())
if read_distance() < 50: # if anything gets in this distance turn on red led
red_led.high()
firebase.put("motion", "1", bg=0) # In case of motion set flag
else:
red_led.low()
firebase.put("motion", "0", bg=0) # When there's no motion reset flag
utime.sleep(0.1)
except OSError as e:
#continue
print('Connection closed: ', e)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
led_onboard.value(0)
print('Connecting...')
utime.sleep(1)
ip = wlan.ifconfig()[0]
print('Connection successful')
print(f'Connected on {ip} \n')