-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNProjectClientSSL.py
66 lines (55 loc) · 2.36 KB
/
CNProjectClientSSL.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
import socket
import ssl
import matplotlib.pyplot as plt
import time
import json
def main():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
ssl_client_socket = context.wrap_socket(client_socket, server_hostname='192.168.164.15')
ssl_client_socket.connect(('192.168.164.15', 8888))
print("Connected to server.")
plt.ion()
fig, ax = plt.subplots()
ax.set_xlabel("Timestamp")
ax.set_ylabel('Values')
ax.set_title('Data Vs Time')
lines = ['b-', 'g-', 'r-'] # Different colors for each variable
labels = ['Humidity', 'Temperature', 'Distance'] # Capitalized labels
plots = [ax.plot([], [], line, label=label)[0] for line, label in zip(lines, labels)]
ax.legend(loc='upper right')
data = {'humidity': [], 'temperature': [], 'distance': []}
timestamps = []
try:
while True:
data_recv = ssl_client_socket.recv(1024)
data_recv = data_recv.decode()
print(data_recv)
try:
json_data = json.loads(data_recv)
for key, value in json_data.items():
if key in data:
data[key].append(float(value) if value is not None else 0.0)
except json.JSONDecodeError:
print("Error decoding JSON data")
continue
timestamp = time.time()
timestamps.append(timestamp)
for i, plot in enumerate(plots):
if timestamps and data[labels[i].lower()] and (labels[i] != 'Distance' or data['distance'][-1] < 200): # Check if arrays are not empty and plot distance only if less than 200 cm
plot.set_xdata(timestamps)
plot.set_ydata(data[labels[i].lower()])
# Update plot limits only if there is data
if timestamps and any(data.values()):
ax.set_xlim(max(timestamps)-100,max(timestamps))
ax.set_ylim(-10,210)
fig.canvas.draw()
fig.canvas.flush_events()
except KeyboardInterrupt:
plt.ioff()
ssl_client_socket.close()
print("Connection closed.")
if __name__ == "__main__":
main()