-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_gui.py
129 lines (103 loc) · 4.06 KB
/
sample_gui.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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'sample_gui.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
import pyqtgraph as pg
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import paho.mqtt.client as mqtt
import sys
class MQTT_Client(QObject):
# Define your signals
messageSignal = pyqtSignal(str)
connected = pyqtSignal()
disconnected = pyqtSignal()
def __init__(self):
QObject.__init__(self)
self.host = "localhost"
self.port = 1883
self.keepAlive = 60
self.cleanSession = True
self.subscribe_topic = "measurement"
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 = msg.payload.decode("ascii")
# print("on_message", mstr, obj, mqttc)
self.messageSignal.emit(mstr)
def on_connect1(self, *args):
# print("on_connect", args)
self.connected.emit()
def on_disconnect1(self, *args):
# print("on_disconnect", args)
self.disconnected.emit()
@pyqtSlot()
def connectToHost(self):
self.client.connect(self.host,
port=self.port,
keepalive=self.keepAlive)
self.client.subscribe(self.subscribe_topic)
self.client.loop_start()
@pyqtSlot()
def disconnectFromHost(self):
self.client.disconnect()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.plot = pg.PlotWidget(self.centralwidget)
self.plot.setObjectName("widget")
self.horizontalLayout.addWidget(self.plot)
self.gridLayout.addLayout(self.horizontalLayout, 0, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setGeometry(QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
self.client = MQTT_Client()
self.client.messageSignal.connect(self.onMessage)
self.pushButton.clicked.connect(self.client.connectToHost)
##starts here
self.xdata = [0]
self.ydata = [0]
self.curve1 = pg.PlotDataItem(self.xdata, self.ydata)
self.plot.addItem(self.curve1)
def onMessage(self,msg):
data=[float(value) for value in msg.split()]
self.ydata+=data
self.xdata+=[self.xdata[-1]+i for i in range(1,len(data)+1)]
N=2000
if len(self.xdata)>=N:
self.xdata=self.xdata[-N:]
self.ydata = self.ydata[-N:]
self.curve1.setData(self.xdata,self.ydata)
def retranslateUi(self, MainWindow):
_translate = QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())