├── Arduino
├── README.md
└── poti.ino
├── Program
├── README.md
├── back.png
├── gui.e4p
├── icons
│ ├── Pi4IoT.png
│ ├── README.md
│ ├── green-led-on.png
│ └── led-red-on.png
├── main.py
├── newValue.py
├── program.pdf
└── ui
│ ├── Ui_mainWindow.py
│ ├── mainWindow.py
│ └── mainWindow.ui
└── README.md
/Arduino/README.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Arduino/poti.ino:
--------------------------------------------------------------------------------
1 | /* Pi4IoT 30.12.2018 */
2 |
3 | void setup() {
4 | Serial.begin(115200);
5 | }
6 |
7 | void loop() {
8 | if (Serial.available()) {
9 | if (Serial.read() == 'p'){
10 | Serial.print(analogRead(A0));
11 | Serial.print("\n");
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Program/README.md:
--------------------------------------------------------------------------------
1 | The explanation of this program is written in the PDF file.
2 |
3 | If you want to edit the program, I recommend to install the IDE Eric first.
4 |
5 | To the website ERIC -->
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Program/back.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pi4IoT/Gauge-Python-Arduino/04ab651103efdd1813156adc61c1d2d99022d70c/Program/back.png
--------------------------------------------------------------------------------
/Program/gui.e4p:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | en_US
8 | 71ea8c6bec7d1ee2484f2e0693071f190b6a2e41
9 | Python3
10 | PyQt5
11 | 0.1
12 |
13 |
14 |
15 |
16 | main.py
17 | newValue.py
18 | ui/Ui_mainWindow.py
19 | ui/__init__.py
20 | ui/mainWindow.py
21 |
22 |
23 |
24 |
25 | main.py
26 |
27 | None
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Program/icons/Pi4IoT.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pi4IoT/Gauge-Python-Arduino/04ab651103efdd1813156adc61c1d2d99022d70c/Program/icons/Pi4IoT.png
--------------------------------------------------------------------------------
/Program/icons/README.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Program/icons/green-led-on.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pi4IoT/Gauge-Python-Arduino/04ab651103efdd1813156adc61c1d2d99022d70c/Program/icons/green-led-on.png
--------------------------------------------------------------------------------
/Program/icons/led-red-on.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pi4IoT/Gauge-Python-Arduino/04ab651103efdd1813156adc61c1d2d99022d70c/Program/icons/led-red-on.png
--------------------------------------------------------------------------------
/Program/main.py:
--------------------------------------------------------------------------------
1 | from PyQt5.QtWidgets import QApplication
2 | from ui.mainWindow import MainWindow
3 | import sys
4 |
5 | if __name__ == "__main__":
6 |
7 | app = QApplication(sys.argv)
8 | ui = MainWindow()
9 | ui.show()
10 | sys.exit(app.exec_())
11 |
12 |
--------------------------------------------------------------------------------
/Program/newValue.py:
--------------------------------------------------------------------------------
1 | from PyQt5.QtCore import pyqtSignal
2 | from PyQt5.QtCore import QThread
3 | import datetime, time
4 | import serial
5 | value = 0
6 |
7 | class Controller(QThread, object):
8 |
9 | now = datetime.datetime.now()
10 | timeInterval="%0.2d:%0.2d:%0.2d" % (now.hour, now.minute, now.second)
11 |
12 | newTime = pyqtSignal(object)
13 | def __init__(self, event):
14 | QThread.__init__(self)
15 | self.stopped = event
16 |
17 | def run(self):
18 | while not self.stopped.wait(1):
19 | self.inTime1()
20 |
21 | def inTime1(self):
22 | global timeInterval
23 | now = datetime.datetime.now()
24 | timeInterval="%0.2d:%0.2d:%0.2d" % (now.hour, now.minute, now.second)
25 |
26 | self.newTime.emit(timeInterval)
27 |
28 | class ControlArduino(QThread):
29 |
30 | newValue = pyqtSignal( object , object)
31 | testRS232 = pyqtSignal(object)
32 |
33 | def __init__(self, event):
34 | QThread.__init__(self)
35 | self.stopped = event
36 | self.altValue = 0
37 |
38 |
39 | def run(self):
40 | try:
41 | self.serArduino = serial.Serial('COM6', 115200, timeout=0) #Windows PC
42 | #ui.serArduino = serial.Serial("/dev/ttyACM0",115200,timeout=1) #Linux PC - Raspberry
43 | self.noRS232_UNO = 1
44 | self.testRS232.emit( 1)
45 | except:
46 | print ("RS232 for Arduino not found")
47 | self.noRS232_UNO = 0
48 | self.testRS232.emit(0)
49 |
50 | while not self.stopped.wait(0.1): #1 max 0.3
51 | self.ArduinoLoop()
52 |
53 | def ArduinoLoop(self):
54 | global value
55 | if self.noRS232_UNO:
56 | self.serArduino.write(b'p')
57 | time.sleep(0.01)
58 | wert = self.serArduino.read(5)
59 | try:
60 | wert1 = wert.split()
61 | intwert = int(wert1[0])
62 | value = int(22 + (intwert/3.84))
63 | self.newValue.emit( intwert , value)
64 |
65 | print(intwert)
66 | except:
67 | print("error Arduino Serial")
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/Program/program.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Pi4IoT/Gauge-Python-Arduino/04ab651103efdd1813156adc61c1d2d99022d70c/Program/program.pdf
--------------------------------------------------------------------------------
/Program/ui/Ui_mainWindow.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | # Form implementation generated from reading ui file 'C:\myPY\Gauge_test1\ui\mainWindow.ui'
4 | #
5 | # Created by: PyQt5 UI code generator 5.6
6 | #
7 | # WARNING! All changes made in this file will be lost!
8 |
9 | from PyQt5 import QtCore, QtGui, QtWidgets
10 |
11 | class Ui_MainWindow(object):
12 | def setupUi(self, MainWindow):
13 | MainWindow.setObjectName("MainWindow")
14 | MainWindow.resize(422, 302)
15 | icon = QtGui.QIcon()
16 | icon.addPixmap(QtGui.QPixmap("icons/Pi4IoT.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
17 | MainWindow.setWindowIcon(icon)
18 | self.centralWidget = QtWidgets.QWidget(MainWindow)
19 | self.centralWidget.setObjectName("centralWidget")
20 | self.lbltime = QtWidgets.QLineEdit(self.centralWidget)
21 | self.lbltime.setGeometry(QtCore.QRect(10, 60, 161, 31))
22 | font = QtGui.QFont()
23 | font.setPointSize(14)
24 | self.lbltime.setFont(font)
25 | self.lbltime.setStyleSheet("background-color: rgb(200,200,200);\n"
26 | "")
27 | self.lbltime.setText("")
28 | self.lbltime.setFrame(False)
29 | self.lbltime.setAlignment(QtCore.Qt.AlignCenter)
30 | self.lbltime.setReadOnly(True)
31 | self.lbltime.setObjectName("lbltime")
32 | self.label_20 = QtWidgets.QLabel(self.centralWidget)
33 | self.label_20.setGeometry(QtCore.QRect(10, 30, 41, 21))
34 | font = QtGui.QFont()
35 | font.setPointSize(11)
36 | font.setBold(False)
37 | font.setWeight(50)
38 | self.label_20.setFont(font)
39 | self.label_20.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
40 | self.label_20.setObjectName("label_20")
41 | self.btnExit = QtWidgets.QPushButton(self.centralWidget)
42 | self.btnExit.setGeometry(QtCore.QRect(290, 240, 91, 41))
43 | font = QtGui.QFont()
44 | font.setPointSize(11)
45 | font.setBold(True)
46 | font.setWeight(75)
47 | self.btnExit.setFont(font)
48 | self.btnExit.setStyleSheet("background-color: rgb(255, 0, 0);")
49 | self.btnExit.setObjectName("btnExit")
50 | self.Grafik = QtWidgets.QGraphicsView(self.centralWidget)
51 | self.Grafik.setGeometry(QtCore.QRect(180, 20, 200, 200))
52 | self.Grafik.setAutoFillBackground(True)
53 | self.Grafik.setStyleSheet("background-color: rgb(188, 188, 188);")
54 | self.Grafik.setFrameShape(QtWidgets.QFrame.NoFrame)
55 | self.Grafik.setFrameShadow(QtWidgets.QFrame.Plain)
56 | self.Grafik.setLineWidth(0)
57 | brush = QtGui.QBrush(QtGui.QColor(240, 240, 240))
58 | brush.setStyle(QtCore.Qt.SolidPattern)
59 | self.Grafik.setBackgroundBrush(brush)
60 | self.Grafik.setObjectName("Grafik")
61 | self.lblStatusLedUNORS232 = QtWidgets.QLabel(self.centralWidget)
62 | self.lblStatusLedUNORS232.setGeometry(QtCore.QRect(150, 180, 21, 21))
63 | self.lblStatusLedUNORS232.setText("")
64 | self.lblStatusLedUNORS232.setPixmap(QtGui.QPixmap("../../../MyPyProjekte/Gauge/ui/icons/led-red-on.png"))
65 | self.lblStatusLedUNORS232.setScaledContents(True)
66 | self.lblStatusLedUNORS232.setObjectName("lblStatusLedUNORS232")
67 | self.lblRSinfo = QtWidgets.QLabel(self.centralWidget)
68 | self.lblRSinfo.setGeometry(QtCore.QRect(20, 180, 131, 16))
69 | font = QtGui.QFont()
70 | font.setPointSize(10)
71 | font.setItalic(False)
72 | self.lblRSinfo.setFont(font)
73 | self.lblRSinfo.setObjectName("lblRSinfo")
74 | self.label = QtWidgets.QLabel(self.centralWidget)
75 | self.label.setGeometry(QtCore.QRect(250, 180, 61, 20))
76 | self.label.setStyleSheet("color: rgb(244, 244, 244);")
77 | self.label.setAlignment(QtCore.Qt.AlignCenter)
78 | self.label.setObjectName("label")
79 | self.lblAnzeige = QtWidgets.QLabel(self.centralWidget)
80 | self.lblAnzeige.setGeometry(QtCore.QRect(250, 160, 51, 20))
81 | font = QtGui.QFont()
82 | font.setPointSize(14)
83 | font.setBold(True)
84 | font.setWeight(75)
85 | self.lblAnzeige.setFont(font)
86 | self.lblAnzeige.setStyleSheet("color: rgb(250, 250, 250);")
87 | self.lblAnzeige.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
88 | self.lblAnzeige.setObjectName("lblAnzeige")
89 | self.label_3 = QtWidgets.QLabel(self.centralWidget)
90 | self.label_3.setGeometry(QtCore.QRect(20, 240, 281, 31))
91 | font = QtGui.QFont()
92 | font.setPointSize(11)
93 | font.setBold(False)
94 | font.setWeight(50)
95 | self.label_3.setFont(font)
96 | self.label_3.setObjectName("label_3")
97 | MainWindow.setCentralWidget(self.centralWidget)
98 |
99 | self.retranslateUi(MainWindow)
100 | QtCore.QMetaObject.connectSlotsByName(MainWindow)
101 |
102 | def retranslateUi(self, MainWindow):
103 | _translate = QtCore.QCoreApplication.translate
104 | MainWindow.setWindowTitle(_translate("MainWindow", "Gauge & Arduino & RS232"))
105 | self.label_20.setText(_translate("MainWindow", "
Time:
"))
106 | self.btnExit.setText(_translate("MainWindow", "EXIT"))
107 | self.lblRSinfo.setText(_translate("MainWindow", "Arduino RS232"))
108 | self.label.setText(_translate("MainWindow", "Poti on A0"))
109 | self.lblAnzeige.setText(_translate("MainWindow", "1000"))
110 | self.label_3.setText(_translate("MainWindow", "https://www.youtube.com/pi4iot"))
111 |
112 |
113 | if __name__ == "__main__":
114 | import sys
115 | app = QtWidgets.QApplication(sys.argv)
116 | MainWindow = QtWidgets.QMainWindow()
117 | ui = Ui_MainWindow()
118 | ui.setupUi(MainWindow)
119 | MainWindow.show()
120 | sys.exit(app.exec_())
121 |
122 |
--------------------------------------------------------------------------------
/Program/ui/mainWindow.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | import sys
3 |
4 | from PyQt5.QtCore import pyqtSlot
5 | from PyQt5.QtWidgets import QMainWindow
6 |
7 | from .Ui_mainWindow import Ui_MainWindow
8 |
9 | from PyQt5 import QtWidgets
10 | from PyQt5 import QtCore, QtGui
11 | from PyQt5.QtGui import QPen, QPixmap
12 |
13 | from PyQt5.QtCore import Qt
14 | from threading import Event
15 |
16 | from newValue import Controller, ControlArduino
17 |
18 | ICON_RED_LED = "icons/led-red-on.png"
19 | ICON_GREEN_LED = "icons/green-led-on.png"
20 |
21 | class MainWindow(QMainWindow, Ui_MainWindow):
22 |
23 | def __init__(self, parent=None):
24 | super(MainWindow, self).__init__(parent)
25 | self.setupUi(self)
26 |
27 | pen = QPen(Qt.red)
28 | pen.setWidth(3)
29 | pen.setCapStyle(Qt.RoundCap)
30 | scene = QtWidgets.QGraphicsScene()
31 | pen.setCosmetic(True)
32 | scene.addPixmap(QPixmap('back.png'))
33 | self.item = scene.addLine(60, 170, 97, 97, pen)
34 | pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.gray))
35 | brush = QtGui.QBrush(pen.color().darker(100))
36 | scene.addEllipse(87, 87, 20, 20, pen, brush)
37 | self.item.setTransformOriginPoint(97, 97)
38 | self.Grafik.setScene(scene)
39 |
40 | self.stop_flag_time = Event()
41 | self.stop_flag_RS232 = Event()
42 |
43 | self.getController = Controller(self.stop_flag_time)
44 | self.getController.start()
45 | self.getController.newTime.connect(self.updateTime)
46 |
47 | self.getArduino = ControlArduino(self.stop_flag_RS232)
48 | self.getArduino.newValue.connect(self.updatePoti)
49 | self.getArduino.testRS232.connect(self.updateInfoRS232)
50 | self.getArduino.start()
51 |
52 | @pyqtSlot()
53 | def on_btnExit_clicked(self):
54 | self.stop_flag_time.set()
55 | self.stop_flag_RS232.set()
56 | sys.exit(0);
57 |
58 | def updateTime(self, timeInterval):
59 | self.lbltime.setText(timeInterval)
60 |
61 | def updatePoti(self, poti, potiRotation):
62 | self.lblAnzeige.setText(str(poti))
63 | self.item.setRotation(potiRotation)
64 |
65 | def updateInfoRS232(self, rs232):
66 | print(rs232)
67 | if rs232:
68 | self.lblStatusLedUNORS232.setPixmap(QtGui.QPixmap(ICON_GREEN_LED))
69 | self.lblRSinfo.setText("Arduino RS232 okay")
70 | else:
71 | self.lblStatusLedUNORS232.setPixmap(QtGui.QPixmap(ICON_RED_LED))
72 | self.lblRSinfo.setText("Arduino RS232 failed")
73 | self.lblAnzeige.setText("Error")
74 | self.stop_flag_RS232.set()
75 |
--------------------------------------------------------------------------------
/Program/ui/mainWindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 422
10 | 302
11 |
12 |
13 |
14 | Gauge & Arduino & RS232
15 |
16 |
17 |
18 | icons/Pi4IoT.png icons/Pi4IoT.png
19 |
20 |
21 |
22 |
23 |
24 | 10
25 | 60
26 | 161
27 | 31
28 |
29 |
30 |
31 |
32 | 14
33 |
34 |
35 |
36 | background-color: rgb(200,200,200);
37 |
38 |
39 |
40 |
41 |
42 |
43 | false
44 |
45 |
46 | Qt::AlignCenter
47 |
48 |
49 | true
50 |
51 |
52 |
53 |
54 |
55 | 10
56 | 30
57 | 41
58 | 21
59 |
60 |
61 |
62 |
63 | 11
64 | 50
65 | false
66 |
67 |
68 |
69 | <html><head/><body><p>Time:</p></body></html>
70 |
71 |
72 | Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter
73 |
74 |
75 |
76 |
77 |
78 | 290
79 | 240
80 | 91
81 | 41
82 |
83 |
84 |
85 |
86 | 11
87 | 75
88 | true
89 |
90 |
91 |
92 | background-color: rgb(255, 0, 0);
93 |
94 |
95 | EXIT
96 |
97 |
98 |
99 |
100 |
101 | 180
102 | 20
103 | 200
104 | 200
105 |
106 |
107 |
108 | true
109 |
110 |
111 | background-color: rgb(188, 188, 188);
112 |
113 |
114 | QFrame::NoFrame
115 |
116 |
117 | QFrame::Plain
118 |
119 |
120 | 0
121 |
122 |
123 |
124 |
125 | 240
126 | 240
127 | 240
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 | 150
136 | 180
137 | 21
138 | 21
139 |
140 |
141 |
142 |
143 |
144 |
145 | ../../../MyPyProjekte/Gauge/ui/icons/led-red-on.png
146 |
147 |
148 | true
149 |
150 |
151 |
152 |
153 |
154 | 20
155 | 180
156 | 131
157 | 16
158 |
159 |
160 |
161 |
162 | 10
163 | false
164 |
165 |
166 |
167 | Arduino RS232
168 |
169 |
170 |
171 |
172 |
173 | 250
174 | 180
175 | 61
176 | 20
177 |
178 |
179 |
180 | color: rgb(244, 244, 244);
181 |
182 |
183 | Poti on A0
184 |
185 |
186 | Qt::AlignCenter
187 |
188 |
189 |
190 |
191 |
192 | 250
193 | 160
194 | 51
195 | 20
196 |
197 |
198 |
199 |
200 | 14
201 | 75
202 | true
203 |
204 |
205 |
206 | color: rgb(250, 250, 250);
207 |
208 |
209 | 1000
210 |
211 |
212 | Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter
213 |
214 |
215 |
216 |
217 |
218 | 20
219 | 240
220 | 281
221 | 31
222 |
223 |
224 |
225 |
226 | 11
227 | 50
228 | false
229 |
230 |
231 |
232 | https://www.youtube.com/pi4iot
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | Advanced Python example of Graphical Applications (GUI)
3 |
4 | Gauge-Python-Arduino
5 |
6 |
7 | Video example on YouTube:
8 |
9 |
10 |
11 |
12 |
13 |
14 | Shows a gauge example to display the analog value at Arduino from a potentiometer.
15 | - Serial Port with Arduino
16 | - Start Background Threads
17 | - Graphical Applications PyQt5
18 | - and Signals and Slots example
19 | - 2D Graphical Items
20 | - by using Eric IDE
21 |
22 | Read out the value of the potentiometer from Arduino Port A0 over the RS232 to the Python Program.
23 |
24 | 
25 |
26 | Download the Arduino program and upload this example to your Arduino UNO.
27 |
28 |
29 |
30 | Then download the folder Program and unzip it on your computer.
31 | Check the com-port number from Arduino. You can start the main.py program for the first test.
32 | If you want to edit the program, I recommended to install the IDE Eric first.
33 | How to do this, see this video:
34 |
35 | Video on YouTube -->
36 |
37 |
38 | and
39 | Video on YouTube -->
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------