-
Notifications
You must be signed in to change notification settings - Fork 0
/
air_pequi_obstacles.py
95 lines (85 loc) · 2.26 KB
/
air_pequi_obstacles.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import RPi.GPIO as GPIO
import time
import pyrebase
import http.client
GPIO.setmode(GPIO.BCM)
TRIGGER = 18
ECHO = 17
PINO_VIBRACALL = 21
DISTANCIA_OBSTACULO = 150
DISTANCIA_LIMITE = 20
PINO_TOCAR = 4
GPIO.setup(TRIGGER, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(PINO_VIBRACALL, GPIO.OUT)
GPIO.setup(PINO_TOCAR, GPIO.OUT)
GPIO.output(TRIGGER, False)
GPIO.output(PINO_VIBRACALL, False)
GPIO.output(PINO_TOCAR, True)
config = {
"apiKey": "AIzaSyABcDJyUL74&^",
"authDomain": "airp.firebaseapp.com",
"databaseURL": "https://airp.firebaseio.com/",
"storageBucket": "gs://airp.appspot.com/"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
def isConnected():
conn = http.client.HTTPConnection("www.google.com")
try:
conn.request("HEAD", "/")
return True
except:
return False
def vibrar():
try:
while True:
GPIO.output(PINO_VIBRACALL, True)
time.sleep(2)
GPIO.output(PINO_VIBRACALL, False)
print("vibracall...")
return
except KeyboardInterrupt:
print("quit")
GPIO.cleanup()
def tocar():
try:
while True:
GPIO.output(PINO_TOCAR, False)
time.sleep(2)
GPIO.output(PINO_TOCAR, True)
print("som...")
return
except KeyboardInterrupt:
print("quit")
GPIO.cleanup()
def salvar(distance):
try:
data = {"data": time.strftime("%d-%m-%y %H:%M:%S") , "distancia":distance}
db.child("obstaculos").push(data)
return
except KeyboardInterrupt:
print("quit")
GPIO.cleanup()
try:
while True:
GPIO.output(TRIGGER, True)
time.sleep(0.00001)
GPIO.output(TRIGGER, False)
start = time.time()
while GPIO.input(ECHO)==0:
start = time.time()
while GPIO.input(ECHO)==1:
stop = time.time()
elapsed = stop-start
distance = ( elapsed * 34300)/2
print(distance)
if (distance < DISTANCIA_OBSTACULO):
vibrar()
tocar()
if(distance < DISTANCIA_LIMITE and isConnected()):
salvar(distance)
time.sleep(1)
except KeyboardInterrupt:
print("quit")
GPIO.cleanup()