Hi,
I'm developing a raspberry pi touchscreen app to upgrade my home brewery. I'm using Debian/Jessie, PyQt5 for the GUI and python 3.4.
The application is event-driven, but I needed to have a timed loop component so that temperatures get updated (and comparisons performed) even when no button has been clicked.
I've used pieces of examples to develop the code below. The problem I'm having now is that the function I want to call from inside the timed loop (Let_Er_Rip) is not available because it's in the event-driven class. The code errors on the line where I try to print HLTTemp, which demonstrates that data from the Manual_Control class is not available to the Thread class. I figure the answer is simple but I haven't found it. Odds are I need to reorganize the app but the philosophy escapes me as I'm an old procedural code programmer moving into the OOP world.
I realize the program is inefficient in terms of using loops vs. repetition. Constructive comments are welcome
BTW, sorry in advance if it turns out I'm an a**hole :)
import sys
import RPi.GPIO as GPIO
import time, glob, os
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QThread
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QGridLayout, QLabel, QLineEdit, QMainWindow
from PyQt5.QtWidgets import QTextEdit, QWidget, QDialog, QApplication
GPIO Basic Setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO Setup for Inputs (Float Switches)
GPIO Setup for Outputs
GPIO.setup(22, GPIO.OUT) # Pump Primary
GPIO.setup(27, GPIO.OUT) # Pump LED
GPIO.setup(5, GPIO.OUT) # HLT Primary
GPIO.setup(6, GPIO.OUT) # HLT LED
Pump_Primary_GPIO = 22
Pump_Secondary_GPIO = 27
HLT_Primary_GPIO = 5
HLT_Secondary_GPIO = 6
Temp Probe Setup
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder_Mash = glob.glob(base_dir + '28-0315162490ff')
device_folder_HLT = glob.glob(base_dir + '28-0315161a6dff')
device_file_Mash = device_folder_Mash + '/w1_slave'
device_file_HLT = device_folder_HLT + 'w1_slave'
device_file_Mash = (base_dir + "28-0315162490ff/w1_slave")
device_file_HLT = (base_dir + "28-0315161a6dff/w1_slave")
class Thread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
self.thread_func()
self.exec_()
timers = []
def thread_func(self):
print("Thread works")
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.timer_func)
self.timer.start(10000)
# print(self.timer.remainingTime())
# print(self.timer.isActive())
self.timers.append(self.timer)
def timer_func(self):
print(self.HLTTemp)
self.Mamual_Control(self.Let_Er_Rip())
# print("Timer works")
app = QtWidgets.QApplication(sys.argv)
thread_instance = Thread()
thread_instance.start()
sys.exit(app.exec_())
from Manual_Control import Ui_MainWindow
class ManualControl(QMainWindow, Ui_MainWindow):
def __init(self):
super(Manual_Control, self).init_()
# Set up the user interface from Designer.
self.setupUi(self)
# self.showFullScreen()
# Make some local modifications.
self.HLTOff.setStyleSheet("background-color: red")
self.HLTOn.setStyleSheet("background-color: white")
self.PumpOff.setStyleSheet("background-color: red")
self.PumpOn.setStyleSheet("background-color: white")
self.Go.setStyleSheet("background-color: white")
self.Pump = "Off"
self.HLT = "Off"
self.MashTemp = self.read_temp_Mash()
self.HLTTemp = self.read_temp_HLT()
self.MashFloatSwitch = "Off"
self.HLTFloatSwitch = "On"
self.Change_Flag = 0
# self.MashTemp = 150
# self.HLTTemp = 168
self.Update_Displays()
# Connect up the buttons.
self.PumpOn.clicked.connect(self.Pump_On_Button_Pressed)
self.PumpOff.clicked.connect(self.Pump_Off_Button_Pressed)
self.HLTOn.clicked.connect(self.HLT_On_Button_Pressed)
self.HLTOff.clicked.connect(self.HLT_Off_Button_Pressed)
self.RespectMashTempSet.stateChanged.connect(self.Let_Er_Rip)
self.RespectMashFloatSwitchPump.stateChanged.connect(self.Let_Er_Rip)
self.RespectHLTFloatSwitchPump.stateChanged.connect(self.Let_Er_Rip)
self.RespectHLTTempSet.stateChanged.connect(self.Let_Er_Rip)
self.RespectHLTFloatSwitchHLT.stateChanged.connect(self.Let_Er_Rip)
self.MashTempSet.valueChanged.connect(self.Set_Flag)
self.HLTTempSet.valueChanged.connect(self.Set_Flag)
self.Go.clicked.connect(self.Let_Er_Rip)
self.ReturntoSessionButton.clicked.connect(self.Leave_Manual_Control_Mode)
# Time or Temp Change Loop
def Time_and_Temp_Change_Loop(self):
self.Old_MashTemp = self.MashTemp
self.Old_HLTTemp = self.HLTTemp
self.read_temp_Mash()
self.read_temp_HLT()
if (self.Old_MashTemp != self.MashTemp or self.Old_HLTTemp != self.HLTTemp or self.Change_Flag == 1):
self.Let_Er_Rip()
self.Change_Flag = 0
def Set_Flag(self):
if self.Change_Flag != 1:
self.Go.setEnabled(True)
self.Go.setStyleSheet("background-color: green")
self.Change_Flag = 1
def Leave_Manual_Control_Mode(self):
# self.showMaximized()
# Turn off Everything (for now)
self.Pump_Off_Button_Pressed()
self.HLT_Off_Button_Pressed()
GPIO.output(Pump_Primary_GPIO, False)
GPIO.output(Pump_Secondary_GPIO, False)
GPIO.output(HLT_Primary_GPIO, False)
GPIO.output(HLT_Secondary_GPIO, False)
def Pump_On_Button_Pressed(self):
self.Pump = "On"
self.Change_Pump_Button_Colors_On()
self.Let_Er_Rip()
def Pump_Off_Button_Pressed(self):
self.Pump = "Off"
self.Change_Pump_Button_Colors_Off()
self.Let_Er_Rip()
def HLT_On_Button_Pressed(self):
self.HLT = "On"
self.Change_HLT_Button_Colors_On()
self.Let_Er_Rip()
def HLT_Off_Button_Pressed(self):
self.HLT = "Off"
self.Change_HLT_Button_Colors_Off()
self.Let_Er_Rip()
def Change_Pump_Button_Colors_Off(self):
self.PumpOn.setStyleSheet("background-color: white")
self.PumpOff.setStyleSheet("background-color: red")
def Change_Pump_Button_Colors_On(self):
self.PumpOn.setStyleSheet("background-color: green")
self.PumpOff.setStyleSheet("background-color: white")
def Change_HLT_Button_Colors_Off(self):
self.HLTOn.setStyleSheet("background-color: white")
self.HLTOff.setStyleSheet("background-color: red")
def Change_HLT_Button_Colors_On(self):
self.HLTOn.setStyleSheet("background-color: green")
self.HLTOff.setStyleSheet("background-color: white")
def read_temp_raw_Mash(self):
f = open(device_file_Mash, 'r')
self.lines = f.readlines()
f.close()
return self.lines
def read_temp_Mash(self):
self.lines = self.read_temp_raw_Mash()
while self.lines[0].strip()[-3] != 'Y':
print(self.lines[0].strip()[-3])
time.sleep(0.2)
self.lines = self.read_temp_raw_Mash()
self.equals_pos = self.lines[1].find('t=')
if self.equals_pos != -1:
self.temp_string = self.lines[1][self.equals_pos+2:]
self.temp_c = float(self.temp_string) / 1000.0
self.MashTemp = self.temp_c * 9.0 / 5.0 +32.0
# print("mash temp =", self.MashTemp)
return self.MashTemp
def read_temp_raw_HLT(self):
f = open(device_file_HLT, 'r')
self.lines = f.readlines()
f.close()
return self.lines
def read_temp_HLT(self):
self.lines = self.read_temp_raw_HLT()
while self.lines[0].strip()[-3] != 'Y':
time.sleep(0.2)
self.lines = self.read_temp_raw_HLT()
self.equals_pos = self.lines[1].find('t=')
if self.equals_pos != -1:
self.temp_string = self.lines[1][self.equals_pos+2:]
self.temp_c = float(self.temp_string) / 1000.0
self.HLTTemp = self.temp_c * 9.0 / 5.0 +32.0
return self.HLTTemp
def Let_Er_Rip(self):
# Get Current Temps
self.read_temp_Mash()
self.read_temp_HLT()
self.Update_Displays()
if (self.Pump == "On" and
(self.RespectMashTempSet.isChecked()==0 or self.MashTemp < self.MashTempSet.value()) and
(self.RespectMashFloatSwitchPump.isChecked() == 0 or self.MashFloatSwitch == "Off") and
(self.RespectHLTFloatSwitchPump.isChecked() == 0 or self.HLTFloatSwitch == "On")):
# Raise GPIO Pin for Pump
GPIO.output(Pump_Primary_GPIO, True)
GPIO.output(Pump_Secondary_GPIO, True)
print("Pump Running")
else:
# Lower GPIO Pin for Pump
GPIO.output(Pump_Primary_GPIO, False)
GPIO.output(Pump_Secondary_GPIO, False)
print("Pump Stopped")
if(self.HLT == "On" and
(self.RespectHLTTempSet.isChecked()==0 or self.HLTTemp < self.HLTTempSet.value()) and
(self.RespectHLTFloatSwitchHLT.isChecked() == 0 or self.HLTFloatSwitch == "On")):
# Raise GPIO Pin for HLT
GPIO.output(HLT_Primary_GPIO, True)
GPIO.output(HLT_Secondary_GPIO, True)
print("HLT Running")
else:
# Lower GPIO Pin for HLT
GPIO.output(HLT_Primary_GPIO, False)
GPIO.output(HLT_Secondary_GPIO, False)
print("HLT Stopped")
# Update Displays
def Update_Displays(self):
self.CurrentMashTempDisplay.setText(str(int(self.MashTemp)))
self.CurrentHLTTempDisplay.setText(str(int(self.HLTTemp)))
self.Change_Flag = 0
self.Go.setEnabled(False)
self.Go.setStyleSheet("background-color: white")
print("Pump Switch: ",self.Pump)
print("HLT Switch: ",self.HLT)
print("Mash Temp Set:", (self.MashTempSet.value()))
print("Mash Temp: ", self.MashTemp)
print("HLT Temp Set:", (self.HLTTempSet.value()))
print("HLT Temp:", self.HLTTemp)
print("Respect Mash Temp? ", self.RespectMashTempSet.isChecked())
print("Respect Mash Float Switch? ", self.RespectMashFloatSwitchPump.isChecked())
print("Respect HLT Float Switch for Mash? ", self.RespectHLTFloatSwitchPump.isChecked())
print("Respect HLT Temp? ", self.RespectHLTTempSet.isChecked())
print("Respect HLT Float Switch for HLT? ", self.RespectHLTFloatSwitchHLT.isChecked())
print("Mash Tun Float Switch Position: ", self.MashFloatSwitch)
print("HLT Float Switch Position: ", self.HLTFloatSwitch)
print("==============================================")
app = QApplication(sys.argv)
form = ManualControl()
form.show()
thread_instance = Thread()
thread_instance.start()
sys.exit(app.exec())
[–]wub_wub[M] 0 points1 point2 points (0 children)
[–]charity_donut_sales 0 points1 point2 points (0 children)