-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleClient.py
64 lines (40 loc) · 1.07 KB
/
sampleClient.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
import bluetooth
import csv
import time
def writeToFile( data1 ):
path1 = r"C:\Users\Jonah\OneDrive - Cornell University\Fall 2017\ECE 4760\Final Project\Server\ "
with open( path1 + 'data.csv', 'w', newline='') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(data1)
# Mac address of the HC-06:
macAddress = '98:d3:31:fd:31:27'
port = 1
# Startup bluetooth connection:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((macAddress,port))
try:
while True:
time.sleep(0.2)
data = s.recv(1024)
if len(data) == 0: break
val = len(data)
if ( data[val-1] == 10 ):
decodedData = decodedData + data.decode('utf-8', "ignore")
print( p, "Received: ", int(decodedData))
writeToFile([int(decodedData)])
else:
if (data[0] == 97):
p = 'A'
elif (data[0] == 98):
p = 'B'
elif (data[0] == 99):
p = 'C'
else:
p = 'D'
decodedData = data[0:val-1].decode('utf-8', "ignore")
#print("Beginning: ", data[0])
except Exception as e:
print( "Error was thrown: " + str(e) )
pass
print("Disconnected")
s.close()