-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluetoothTokens2.py
executable file
·83 lines (61 loc) · 1.77 KB
/
BluetoothTokens2.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
import requests
import json
#base_dir = '/home/vimanyu/PycharmProjects/'
base_dir = '/home/pi/Documents'
#device_folder = glob.glob(base_dir + '28*')[0]
device_file = base_dir + '/Attendance/pitokens.log'
def read_tokens():
#f = open('/home/pi/Documents/Attendance/pitokens.log')
f = open(device_file, 'r')
lines = f.read()
print lines
f.close()
return lines
#print type(token)
#print token
#token = 1234
#print token
import os
import glob
import time
import RPi.GPIO as GPIO
from bluetooth import *
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
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, "PiServer",
service_id=uuid,
service_classes=[uuid, SERIAL_PORT_CLASS],
profiles=[SERIAL_PORT_PROFILE],
# protocols = [ OBEX_UUID ]
)
while True:
token = long(read_tokens())
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
if data == 'temp':
data = str(token) + '!'
else:
data = 'WTF!'
client_sock.send(data)
print "sending [%s]" % data
except IOError:
pass
print " IO Error"
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
break