i want to make a program that count pulses then it go through some equation and display it in the gui . This my main.py
import sys
import time
import RPi.GPIO as GPIO
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from mainwindow import Ui_MainWindow
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)
class MainWindow(QMainWindow):
# access variables inside of the UI's file
def __init__(self):
super().__init__()
self.mainwindow = Ui_MainWindow()
self.mainwindow.setupUi(self)
self.i=100
self.flow = 0
self.flowliter = 0
self.totalflow=0
self.mainwindow.lcdNumber.display(self.i)
self.mainwindow.lcdNumber_2.display(self.i)
self.show()
self.mainwindow.startbttn.clicked.connect(lambda: self.pressedstartButton())
self.mainwindow.stopbttn.clicked.connect(lambda: self.pressedstopButton())
def pressedstartButton(self):
print ("Pressed On!")
self.data()
def pressedstopButton(self):
print ("Pressed Off!")
def data(self) :
global count
count = 0
def countPulse(channel):
global count
if start_counter == 1:
count = count+1
GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse)
while True:
try:
start_counter = 1
time.sleep(1)
start_counter = 4
self.flow = (10 * 60)
self.flowliter= (self.flow/60)
self.totalflow += self.flowliter
print("%d"% (count))
print ("The flow is: %.3f Liter/min" % (self.flow))
print ("The flowliter is: %.3f Liter" % (self.flowliter))
print ("The volume is: %.3f Liter" % (self.totalflow))
self.mainwindow.lcdNumber.display(self.flow)
self.mainwindow.lcdNumber_2.display(self.flowliter)
count = 0
time.sleep(1)
except KeyboardInterrupt:
print ('\ncaught keyboard interrupt!, bye')
GPIO.cleanup()
sys.exit()
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
the lcdnumber doesnt update in the gui but the self.flow update in the shell and i want display the value in the gui but i dont know which one suitable qtablewidget or qtextbroswer this code should count the pulse from the gpio 18 and show the flow in the gui.
You should not use time-consuming functions as they block the eventloop and the consequence is to freeze the GUI. In this case, you should not use an infinite loop or time.sleep but a QTimer is enough.
import sys
import RPi.GPIO as GPIO
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from mainwindow import Ui_MainWindow
FLOW_SENSOR = 18
class MainWindow(QMainWindow):
pinSignal = pyqtSignal()
def __init__(self):
super().__init__()
self.mainwindow = Ui_MainWindow()
self.mainwindow.setupUi(self)
self.i = 100
self.flow = 0
self.flowliter = 0
self.totalflow = 0
self.mainwindow.lcdNumber.display(self.i)
self.mainwindow.lcdNumber_2.display(self.i)
self.show()
self.mainwindow.startbttn.clicked.connect(self.pressedstartButton)
self.mainwindow.stopbttn.clicked.connect(self.pressedstopButton)
self.start_counter = False
self.count = 0
self.timer = QTimer(self, interval=1000, timeout=self.execute_every_second)
self.pinSignal.connect(self.handle_pin_signal)
def pressedstartButton(self):
print("Pressed On!")
GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback = lambda *args: self.pinSignal.emit())
self.execute_every_second()
self.timer.start()
def pressedstopButton(self):
print("Pressed Off!")
self.timer.stop()
GPIO.remove_event_detect(FLOW_SENSOR)
def handle_pin_signal(self):
if self.start_counter:
self.count += 1
def execute_every_second(self):
if not self.start_counter:
self.flow = 10 * 60
self.flowliter = self.flow / 60
self.totalflow += self.flowliter
print("%d" % (self.count))
print("The flow is: %.3f Liter/min" % (self.flow))
print("The flowliter is: %.3f Liter" % (self.flowliter))
print("The volume is: %.3f Liter" % (self.totalflow))
self.mainwindow.lcdNumber.display(self.flow)
self.mainwindow.lcdNumber_2.display(self.flowliter)
self.count = 0
self.start_counter = not self.start_counter
def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
app = QApplication(sys.argv)
form = MainWindow()
form.show()
ret = app.exec_()
GPIO.cleanup()
sys.exit(ret)
if __name__ == "__main__":
main()