-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
99 lines (78 loc) · 2.38 KB
/
plot.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
import datetime
import subprocess
import os
import threading
import paho.mqtt.client as mqtt
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
import signal
import sys
import config
nr_messages = 0
buffered_result = ""
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
stop_threads = False
# Define a signal handler function for Ctrl+C
def signal_handler(sig, frame):
global stop_threads
print("Ctrl+C captured. Stopping threads and exiting gracefully.")
stop_threads = True
client.disconnect()
sys.exit(0)
# Register the signal handler
signal.signal(signal.SIGINT, signal_handler)
def animate(i):
global xs, ys
if stop_threads:
return
with data_lock:
ax.clear()
ax.plot(xs, ys)
# Calculate indices for tick positions
total_ticks = len(xs)
desired_ticks = 15
step = max(total_ticks // desired_ticks, 1)
tick_indices = np.arange(0, total_ticks, step)
# Extract numerical indices for tick positions
tick_positions = [tick_indices[i] for i in range(len(tick_indices))]
# Set x-axis tick positions and labels
ax.set_xticks(tick_positions)
ax.set_xticklabels([xs[i] for i in tick_indices])
# Rotate x-axis tick labels for better visibility (optional)
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('Heart signal over time')
plt.ylabel('mV')
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe(config.mqtt_topic)
data_lock = threading.Lock()
def on_message(client, userdata, msg):
global xs, ys, nr_messages
rawdata = msg.payload.decode('utf-8')
if rawdata == "!":
return
nr_messages += 1
try:
float(rawdata)
except:
return
with data_lock: # Acquire the lock before modifying xs and ys
xs.append(datetime.datetime.now().strftime('%M:%S.%f'))
ys.append(float(rawdata))
xs = xs[-900:]
ys = ys[-900:]
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(config.mqtt_host, 1883, 60)
mqtt_thread = threading.Thread(target=client.loop_forever)
mqtt_thread.start()
ani = FuncAnimation(fig, animate, interval=250)
plt.show()
mqtt_thread.join()
client.disconnect()