-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic_plant_HC-SR04.py
53 lines (39 loc) · 1.21 KB
/
ultrasonic_plant_HC-SR04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# AquaFusion - BSIT 4A - CPSTONE
import RPi.GPIO as GPIO
import time
import pyrebase
from config import config
# Define GPIO pins for HC-SR04
TRIG = 27
ECHO = 22
firebase = pyrebase.initialize_app(config)
db = firebase.database()
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
while True:
GPIO.output(TRIG, False)
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print("Distance:", distance, "cm")
# Create a dictionary with the data you want to send to Firebase
data = {
"distance": distance
}
# Send the data to Firebase Realtime Database
db.child("ultrasonic_data_plant").set(data)
except KeyboardInterrupt:
print("Measurement stopped by the user.")
finally:
GPIO.cleanup()
time.sleep(2) # Adjust the sleep interval for Ultrasonic sensor readings