-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrssiServer.py
More file actions
69 lines (56 loc) · 1.95 KB
/
rssiServer.py
File metadata and controls
69 lines (56 loc) · 1.95 KB
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
import socket
import collections
import numpy as np
import matplotlib.pyplot as plt
host = 'E8:B1:FC:F5:16:02'
#host = socket.gethostname()
port = 4
dset = 100
y = collections.deque(np.zeros(dset), maxlen=dset)
x = np.linspace(0, 1, dset+1)[0:-1] # all points from 0 to 1 excluding 1
plotAxis1 = []
backlog = 1
bufferSize = 1024
client = None
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((host, port))
s.listen(backlog)
client, address = s.accept()
plt.style.use('ggplot')
def livePlot(x_vec,y1_data,line1,identifier='',pause_time=0.1):
# First pass in loop initializes
if line1 == []:
# turn on interactive plot
plt.ion()
fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
# plot axis in the figure
line1, = ax.plot(x_vec,y1_data,'-o',alpha=0.8)
plt.ylabel('RSSI')
#plt.title('Title: {}'.format(identifier))
plt.show()
# after the figure, axis, and line are created, we only need to update the y-data
line1.set_ydata(y1_data)
# adjust limits if new data goes beyond bounds
if np.min(y1_data)<=line1.axes.get_ylim()[0] or np.max(y1_data)>=line1.axes.get_ylim()[1]:
plt.ylim([np.min(y1_data)-np.std(y1_data),np.max(y1_data)+np.std(y1_data)])
# this pauses the data so the figure/axis can catch up - the amount of pause can be altered above
plt.pause(pause_time)
# return line so we can update it again in the next iteration
return line1
while True:
cmd = client.send(bytes(input(), 'utf-8'))
res = client.recv(bufferSize)
print(res)
res = str(res, 'utf-8')
if res == 'quit':
break
elif res[0] == 'u':
val = float(res[1:])
y.append(val)
plotAxis1 = livePlot(x, y, plotAxis1)
#print(val)
#y.append(int(res[1:]))
print('Closing sockets')
client.close()
s.close()