-
Notifications
You must be signed in to change notification settings - Fork 0
/
total.py.save
149 lines (119 loc) · 4.8 KB
/
total.py.save
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import RPi.GPIO as GPIO
import time
import serial
import firebase_admin
from firebase_admin import auth
from firebase_admin import credentials, db
from bluetooth import *
# FCM
from pyfcm import FCMNotification
APIKEY = "AAAAp7fGgNA:APA91bG3w58cfBLHhG4cRykjIS9O3rib5wIh-A67k8VeXWhVp_4vCWzNDnwtjot0vo18Zz2BcHk4vC9k8ArAqIEpWUg8v1j_PzsCKCT_PXHcV3biJqbDCbZx9piipb1uJc7D5w_gW2vt"
# 자영
#TOKEN = "e2kvzJhvTl2fwt2znfYdb7:APA91bF_IuhorxGqmbaPJaa__lMovMF6zUyyBlAvcq6KsyWMLf0pSjVZ2clz2SdqZakQ1CL_-HXjuzH04lF-GgNepYiRLIsvJh3Q0TRkEySublKO6si_w3Er5WaetR-evdSXtG_ARjV9"
#수현
TOKEN = "ewf8khSsQR2lK9bw37ylJ_:APA91bF4npdqkLFVM3sqk85paNFINFPSTAz-RP2UZ2XbzrpvMU7Tl7uPev1q_HfkwNwPyDUZAewhi94Abquo10pwdAHfKL-uz7MtCeJmk2M_Q5Bx7Djmv3TsK0jhd6bSBGca9DKYtkeq"
push_service = FCMNotification(APIKEY)
def sendMessage(title, body):
data_message = {
"body" : body,
"title": title
}
result = push_service.single_device_data_message(registration_id=TOKEN, data_message=data_message)
print(result)
cred = credentials.Certificate("/home/pi/probono-da97e-firebase-adminsdk-23ap0-dce234980c.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://probono-da97e-default-rtdb.firebaseio.com/'
})
ref = db.reference('alarm/alarm_02')
# bluetooth
socket = BluetoothSocket(RFCOMM)
socket.connect(("98:D3:71:FD:BF:91", 1))
def measure(GPIO_TRIGGER, GPIO_ECHO):
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
start = time.time()
while GPIO.input(GPIO_ECHO)==1:
stop = time.time()
elapsed = stop-start
distance = (elapsed * 34300)/2
return distance
def measure_average(GPIO_TRIGGER, GPIO_ECHO):
distance1=measure(GPIO_TRIGGER, GPIO_ECHO)
time.sleep(0.1)
distance2=measure(GPIO_TRIGGER, GPIO_ECHO)
time.sleep(0.1)
distance3=measure(GPIO_TRIGGER, GPIO_ECHO)
distance = distance1 + distance2 + distance3
distance = distance / 3
return distance
def total():
GPIO.setmode(GPIO.BCM)
# 초음파1
GPIO_TRIGGER1 = 23
GPIO_ECHO1 = 24
# 초음파2
GPIO_TRIGGER2 = 17
GPIO_ECHO2 = 27
print("Ultrasonic Measurement")
GPIO.setup(GPIO_TRIGGER1, GPIO.OUT)
GPIO.setup(GPIO_ECHO1, GPIO.IN)
GPIO.setup(GPIO_TRIGGER2, GPIO.OUT)
GPIO.setup(GPIO_ECHO2, GPIO.IN)
GPIO.output(GPIO_TRIGGER1, False)
GPIO.output(GPIO_TRIGGER2, False)
Flag = False
print("거리감지시작")
port = '/dev/ttyACM0'
brate = 9600 #boudrate
cmd = 'temp'
seri = serial.Serial(port, baudrate = brate, timeout = None)
print("소리감지시작", seri.name)
seri.write(cmd.encode())
while True:
distance1 = measure_average(GPIO_TRIGGER1, GPIO_ECHO1)
distance2 = measure_average(GPIO_TRIGGER2, GPIO_ECHO2)
time.sleep(1)
if distance1 < 45 or distance2 < 45: # 아기 머리가 가까움
if Flag == False :
print("Distance Detection : %.1f" % distance1)
print("Distance Detection : %.1f" % distance2)
now = time.localtime()
ref.update({'title' : '낙상 위험 감지'})
ref.update({'comment' : '아기가 잘 누워 있나요?'})
ref.update({'time' : "%04d/%02d/%02d %02d:%02d"%(now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour,now.tm_min)})
# Bluetooth
socket.send('2')
# FCM
sendMessage("낙상 위험 감지", "아기가 떨어질 것 같아요!!")
Flag = True
elif distance1 >= 45 or distance2 >= 45: # 아기 머리가 멀어짐
if Flag == True :
print("Safe : %.1f" % distance1)
print("Safe : %.1f" % distance2)
ref.delete()
Flag = False
if seri.in_waiting != 0 :
content = seri.readline()
print("소리감지", content.decode(), end='')
if(float(content.decode()) == -1):
ref.delete()
else:
now = time.localtime()
ref.update({'title' : '울음 소리 감지'})
ref.update({'comment': '아기가 울고 있어요!'})
ref.update({'time' : "%04d/%02d/%02d %02d:%02d"%(now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour,now.tm_min)})
sendMessage("울음 소리 감지", "아기 상태를 확인해주세요!!")
# Bluetooth
socket.send('3')
try:
total()
except KeyboardInterrupt:
GPIO.cleanup()
socket.close()
print("강제종료")