-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBluetooth-firebase.py
163 lines (118 loc) · 4.68 KB
/
Bluetooth-firebase.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pyrebase
import platform
import os
import glob
import time
#import RPi.GPIO as GPIO
from bluetooth import *
config={ #fill according to your firebase database
"apiKey": "apiKey",
"authDomain": "domain name",
"databaseURL": "https://databasename.com/",
"storageBucket": "databasename.com"
}
firebase =pyrebase.initialize_app(config)
db=firebase.database()
def uploadMess(tuSun,yeSun):
db.child(tuSun).update(yeSun)
def getMAC(interface):
# Return the MAC address of interface
try:
str = open('/sys/class/net/' + interface + '/address').read()
except:
str = "00:00:00:00:00:00"
return str[0:17]
def stream_handler(message):
print(message["event"]) # put
print(message["path"]) # /-K7yGTTEp7O549EzTYtI
print(message["data"]) # {'title': 'Pyrebase', "body": "etc..."}
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "AquaPiServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
#ting = bytes(open('/sys/class/net/eth0/address').read())
ting=getMAC('eth0')
print(ting)
my_stream = db.child("database1").child(ting).child("Message").stream(stream_handler)
while True:
print ("Waiting for connection on RFCOMM channel %d" % port)
client_sock, client_info = server_sock.accept()
print ("Accepted connection from ", client_info)
data1=''
forQuit=''
data=''
while 1:
try:
Data = client_sock.recv(1024)
if len(Data) == 0: break
i=0
if(Data[i] is 'l'):
#print("inside")
Lat=""
i=i+1
while(Data[i] is not 'L'):
Lat=Lat+Data[i]
i=i+1
#print(Lat)
Long=""
i=i+1
while(Data[i] is not 'a'):
Long=Long+Data[i]
i=i+1
#print(Long)
Alt=""
i=i+1
while(Data[i] is not 's'):
Alt=Alt+Data[i]
i=i+1
#print(Alt)
Speed=""
i=i+1
while(Data[i] is not 'n'):
Speed=Speed+Data[i]
i=i+1
#print(Speed)
#print(Speed)
dataone=({'Lat':Lat,'Long':Long,'Alt':Alt,'Speed':Speed})
db.child("database1").child(ting).child("Location").update(dataone)
print("Lat:"+Lat+" Long:"+Long+" Alt:"+Alt+" Speed:"+Speed +"\n")
except IOError:
pass
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
break
data1=data
'''
myMess=db.child("database1").child(ting).child("Messages").get()
if(myMess.val()==None):
print("NO data")
else:
for alerts in myMess.each():
print(alerts.key())
print(alerts.val())
print("\n")
'''
'''
data={'kiryu':"Shining Finger"}
db.child("database1").child("kenkaneki").update(data)
myMess=db.child("database1").child("kenkaneki").get()
if(myMess.val()==None):
print("NO data")
else:
for alerts in myMess.each():
print(alerts.key())
print(alerts.val())
print("\n")
'''