-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_2.py
401 lines (333 loc) · 16.5 KB
/
interface_2.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
import paho.mqtt.client as mqtt
import sys
import numpy as np
class MQTT_Client(QtCore.QObject):
# Define the signals. Qt terminology.
messageSignal = QtCore.pyqtSignal(str)
connected = QtCore.pyqtSignal()
disconnected = QtCore.pyqtSignal()
def __init__(self):
QtCore.QObject.__init__(self)
# define your default client settings
self.host = "localhost"
self.port = 1883
self.keepAlive = 60
self.cleanSession = True
self.subscribe_topic = "measurement"
self.state = "disconnected" # created to keep track of the state of the client
# Create the paho client and set your callbacks
self.client = mqtt.Client(clean_session=self.cleanSession)
self.client.on_connect = self.on_connect1
self.client.on_message = self.on_message1
self.client.on_disconnect = self.on_disconnect1
def on_message1(self, mqttc, obj, msg):
mstr = str(msg.payload)
self.messageSignal.emit(mstr) # Emit the signal.Qt terminology
def on_connect1(self, *args):
self.connected.emit() # Emit the signal.Qt terminology
def on_disconnect1(self, *args):
self.disconnected.emit() # Emit the signal.Qt terminology
@QtCore.pyqtSlot()
def connectToHost(self):
# The function to connect the client to the broker.
self.client.connect(self.host,
port=self.port,
keepalive=self.keepAlive)
self.state = "connected"
self.client.subscribe(self.subscribe_topic)
self.client.loop_start()
print("connected")
@QtCore.pyqtSlot()
def disconnectFromHost(self):
# Call this function if you want to disconnect the broker
self.client.loop_stop()
self.state = "disconnected"
self.client.disconnect()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
# Here starts the main constructor. It mainly constitutes all the
# "architecture" of the app: Instances the objects, places them in the respective layouts and other things.
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.mqtt_label = QtWidgets.QLabel(self.centralwidget)
self.mqtt_label.setObjectName("mqtt_label")
self.verticalLayout_2.addWidget(
self.mqtt_label, 0, QtCore.Qt.AlignHCenter)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.host_label = QtWidgets.QLabel(self.centralwidget)
self.host_label.setObjectName("host_label")
self.horizontalLayout_3.addWidget(self.host_label)
spacerItem = QtWidgets.QSpacerItem(
40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem)
self.line_host = QtWidgets.QLineEdit(self.centralwidget)
self.line_host.setObjectName("line_host")
self.horizontalLayout_3.addWidget(self.line_host)
self.verticalLayout_2.addLayout(self.horizontalLayout_3)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.port_label = QtWidgets.QLabel(self.centralwidget)
self.port_label.setObjectName("port_label")
self.horizontalLayout_4.addWidget(self.port_label)
spacerItem1 = QtWidgets.QSpacerItem(
40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem1)
self.line_port = QtWidgets.QLineEdit(self.centralwidget)
self.line_port.setObjectName("line_port")
self.horizontalLayout_4.addWidget(self.line_port)
self.verticalLayout_2.addLayout(self.horizontalLayout_4)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.topic_label = QtWidgets.QLabel(self.centralwidget)
self.topic_label.setObjectName("topic_label")
self.horizontalLayout_5.addWidget(self.topic_label)
spacerItem2 = QtWidgets.QSpacerItem(
40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem2)
self.line_topic = QtWidgets.QLineEdit(self.centralwidget)
self.line_topic.setObjectName("line_topic")
self.horizontalLayout_5.addWidget(self.line_topic)
self.verticalLayout_2.addLayout(self.horizontalLayout_5)
self.button_conn = QtWidgets.QPushButton(self.centralwidget)
self.button_conn.setObjectName("button_conn")
self.verticalLayout_2.addWidget(self.button_conn)
self.button_disconn = QtWidgets.QPushButton(self.centralwidget)
self.button_disconn.setObjectName("button_disconn")
self.verticalLayout_2.addWidget(self.button_disconn)
self.button_erase_data = QtWidgets.QPushButton(self.centralwidget)
self.button_erase_data.setObjectName("button_erase_data")
self.verticalLayout_2.addWidget(self.button_erase_data)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.n_points = QtWidgets.QLabel(self.centralwidget)
self.n_points.setObjectName("n_points")
self.verticalLayout_3.addWidget(self.n_points)
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setTickPosition(QtWidgets.QSlider.TicksBelow)
self.horizontalSlider.setTickInterval(4)
self.horizontalSlider.setObjectName("horizontalSlider")
self.verticalLayout_3.addWidget(self.horizontalSlider)
self.verticalLayout_2.addLayout(self.verticalLayout_3)
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setObjectName("listWidget")
self.verticalLayout_2.addWidget(self.listWidget)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.plot_long = pg.PlotWidget(self.centralwidget)
self.plot_long.setObjectName("plot_long")
self.verticalLayout.addWidget(self.plot_long)
self.plot = pg.PlotWidget(self.centralwidget)
self.plot.setObjectName("plot")
self.verticalLayout.addWidget(self.plot)
self.horizontalLayout.addLayout(self.verticalLayout)
self.horizontalLayout.setStretch(0, 25)
self.horizontalLayout.setStretch(1, 75)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#-------------------The functional part of the app starts here-------------------------#
# initialize the live data plot
self.N = 3000 # pre set the amount of points the live data plot
self.xdata = [0] # initialize some dummy data
self.ydata = [0]
# instance the curve object
self.curve1 = pg.PlotDataItem(self.xdata, self.ydata)
self.plot.addItem(self.curve1)
self.plot.setLabels(title="Data from potentiometer",
bottom="Time in seconds from start of measurement", left="Voltage (V)")
# initialize the long range data plot
self.xdata_long = [0]
self.ydata_long = [0]
self.curve2 = pg.PlotDataItem(self.xdata_long, self.ydata_long)
self.plot_long.addItem(self.curve2)
self.plot_long.setLabels(title="Data from potentiometer from the beggining",
bottom="Time in seconds from start of measurement",
left="Voltage (V)")
# Instance the custom client
self.client = MQTT_Client()
# Connect the signals to its respective slots
self.client.messageSignal.connect(self.onMessage)
self.button_conn.clicked.connect(self.connect)
self.button_disconn.clicked.connect(self.disconnect)
self.button_erase_data.clicked.connect(self.erase_data)
self.horizontalSlider.valueChanged.connect(self.set_n)
self.line_host.editingFinished.connect(self.host_edit)
self.line_port.editingFinished.connect(self.port_edit)
self.line_topic.editingFinished.connect(self.topic_edit)
# adding a timer to get the long ranged data
self.qtimer = QtCore.QTimer()
self.qtimer.setInterval(1000) #set timeout interval. Miliseconds
self.qtimer.timeout.connect(self.get_long_data)# Connect the timeout event to a slot
# Second timer to get the frequency
self.qtimer1 = QtCore.QTimer()
self.qtimer1.setInterval(10000)#set timeout interval. Miliseconds
self.qtimer1.timeout.connect(self.get_freq)# Connect the timeout event to a slot
def get_freq(self):
# Prints data on the listwidget
try:
x = np.asarray(self.xdata)
xdiff = x[1:]-x[:len(x)-1]
dt = np.mean(xdiff)
self.listWidget.addItem(
"--> Avg sampling frequency from \n the last {} points:\n{} Hz \n Packet size= {} \n Measurement duration= {} \n Time between messages= {} \n Delay = {}".format(len(x), str(1/dt)[:7], self.packet_size, self.meas_duration, self.time_between_msgs/1e6, self.delay))
except RuntimeWarning:
pass
def host_edit(self):
# reassigns the new hostname based on the line edit
text = self.line_host.text()
if text == "": # catches the null editing of a line
pass
else:
self.client.host = text
self.listWidget.addItem("--> Hostname changed to:{}".format(text))
def port_edit(self):
# reassings a new port based on the line edit
text = self.line_port.text()
if text == "":
pass
else:
try:
self.client.port = int(text)
self.listWidget.addItem("--> Port changed to:{}".format(text))
except ValueError:
self.listWidget.addItem("--> Insert an integer, not a string.")
def topic_edit(self):
# Reassigns a new topic to subscribe.
text = self.line_topic.text()
if text == "":
pass
else:
self.client.subscribe_topic = text
self.listWidget.addItem(
"--> Topic to subscribe changed to:{}".format(text))
def connect(self):
# handles the connection after clicking the button
if self.client.state == "connected":
self.listWidget.addItem("-->Already connected to a host")
else:
try:
self.client.connectToHost()
self.listWidget.addItem("--> GUI connected: \n hostname: \"{}\" \n Port: {} \n Clean session {} \n Subscribed to \"{}\"".format(
self.client.host, self.client.port, self.client.cleanSession, self.client.subscribe_topic))
self.first_message = True # set the first message criteria on connection
# start the timers
self.qtimer.start()
self.qtimer1.start()
except:
self.listWidget.addItem("--> Something went wrong. Try again")
def get_long_data(self):
if len(self.xdata) > 1:
try:
self.xdata_long.append(self.xdata[-1])
self.ydata_long.append(np.mean(self.ydata[-10:]))
self.curve2.setData(self.xdata_long[1:], self.ydata_long[1:])
except IndexError:
pass
def set_n(self, value):
# handles the change on the slider for the number of points.
n_points = [50*i for i in range(1000)]
self.N = n_points[value]
def onMessage(self, msg):
# Handles the reception of a new message from the broker
data = msg.split(", ")
data[0] = data[0][3:]
data[-1] = data[-1][:-2]
# [meas_start] [meas stop] [delay] [new_msg]
# |------------measurement-------------|-------------| |--------------measurement-----------|---------|
if self.first_message:
self.previous_start = int(data[0]) #save the start of the first message
self.dt = int(data[-1])/1e6/len(data[1:len(data)-1]) # in seconds. Time interval between samples
voltage = [int(i)/4095*3.3 for i in data[1:len(data)-1]]
time = [self.xdata[-1]+i*self.dt for i in range(len(voltage))]
self.first_message = False
else:
self.time_between_msgs = int(data[0])-self.previous_start
self.delay = self.time_between_msgs-int(data[-1])
self.delay = self.delay/1e6 # in seconds
self.dt = int(data[-1])/1e6/len(data[1:len(data)-1]) # in seconds
voltage = [int(i)/4095*3.3 for i in data[1:len(data)-1]]
time = [self.xdata[-1]+i*self.dt +
self.delay for i in range(len(voltage))]
# Just to get the current packet size
self.packet_size = len(voltage)
self.meas_duration=int(data[-1])/1e6
self.previous_start = int(data[0])
self.ydata += voltage
self.xdata += time
N = self.N
if len(self.xdata) >= N:
self.xdata = self.xdata[-N:]
self.ydata = self.ydata[-N:]
try:
self.curve1.setData(self.xdata[1:], self.ydata[1:])
except IndexError:
pass
def erase_data(self):
#Erase all data from the plots
self.xdata = [0]
self.ydata = [0]
self.curve1.setData(self.xdata, self.ydata)
self.xdata_long = [0]
self.ydata_long = [0]
self.curve2.setData(self.xdata_long, self.ydata_long)
self.first_message = True
def disconnect(self):
#Disconnect from the host
if self.client.state == "disconnected":
self.listWidget.addItem("--> Already disconnected from host")
else:
try:
self.client.disconnectFromHost()
self.listWidget.addItem("--> GUI disconnected from host.")
# stop the timers
self.qtimer.stop()
self.qtimer1.stop()
except:
self.listWidget.addItem("--> Something went wrong. Try again")
def retranslateUi(self, MainWindow):
# Just another function to handle the backbones of the app. Just renames stuff
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Data interface"))
self.mqtt_label.setText(_translate(
"MainWindow", "MQTT client options"))
self.host_label.setText(_translate("MainWindow", "Hostname"))
self.port_label.setText(_translate("MainWindow", "Port"))
self.topic_label.setText(_translate(
"MainWindow", "Topic to subscribe"))
self.button_conn.setText(_translate("MainWindow", "Connect to Host"))
self.button_disconn.setText(_translate(
"MainWindow", "Disconnect from Host"))
self.button_erase_data.setText(_translate("MainWindow", "Erase data"))
self.n_points.setText(_translate(
"MainWindow", "Number of points in plot:"))
self.plot_long.setStatusTip(_translate(
"MainWindow", "Displays the data for long periods of time"))
if __name__ == "__main__": # Launch the GUI
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())