From e1f70fc868aff90177c772a132905f3c5e747162 Mon Sep 17 00:00:00 2001 From: Luca Tringali Date: Sat, 19 Jan 2019 01:31:18 +0100 Subject: First working code --- termostato.py | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ termostato.ui | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 314 insertions(+) create mode 100755 termostato.py create mode 100644 termostato.ui diff --git a/termostato.py b/termostato.py new file mode 100755 index 0000000..a91af56 --- /dev/null +++ b/termostato.py @@ -0,0 +1,164 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +import RPi.GPIO as GPIO +import w1thermsensor +import sys, os +from time import sleep + +try: + from PySide2.QtWidgets import QApplication +except: + try: + from tkinter import messagebox + messagebox.showinfo("Installazione, attendi prego", "Sto per installare le librerie grafiche e ci vorrà del tempo. Premi Ok e vai a prenderti un caffè.") + pip.main(["install", "--index-url=http://download.qt.io/snapshots/ci/pyside/5.9/latest/", "pyside2", "--trusted-host", "download.qt.io"]) + #pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.9/latest/ pyside2 --trusted-host download.qt.io + #pip install PySide2 + from PySide2.QtWidgets import QApplication + except: + try: + from pip._internal import main + main(["install", "--index-url=http://download.qt.io/snapshots/ci/pyside/5.9/latest/", "pyside2", "--trusted-host", "download.qt.io"]) + from PySide2.QtWidgets import QApplication + except: + sys.exit(1) + + +from PySide2.QtUiTools import QUiLoader +from PySide2.QtCore import QFile +from PySide2.QtCore import Qt +from PySide2.QtCore import Signal +from PySide2.QtWidgets import QLabel +from PySide2.QtWidgets import QMainWindow +from PySide2.QtCore import QThread + + +toSleep = 10 + +class TurnOn(QThread): + TempReached = Signal(bool) + + def __init__(self, w, addr = ""): + QThread.__init__(self) + self.w = w + self.setTerminationEnabled(True) + self.relaypin = 23 + # Numerazione dei PIN GPIO + GPIO.setmode(GPIO.BCM) + # Il pin del relay va in output + GPIO.setup(self.relaypin, GPIO.OUT) + #Cerco il sensore + self.sensor = w1thermsensor.W1ThermSensor() + + def __del__(self): + print("Shutting down thread") + + def run(self): + self.reachTemp() + return + + def readTemp(self): + #Leggo la temperatura + temperature_in_celsius = self.sensor.get_temperature() + print("Temperatura:"+str(temperature_in_celsius)+"° C") + return temperature_in_celsius + + def reachTemp(self): + global toSleep + try: + while self.w.SwitchOn.isChecked(): + if float(self.readTemp()) < float(self.w.tempImpostata.value()): + # Accendo il relay + GPIO.output(self.relaypin, GPIO.HIGH) + sleep(toSleep) + else: + # Spengo il relay + GPIO.output(self.relaypin, GPIO.LOW) + self.TempReached.emit(True) + except: + self.TempReached.emit(False) + + +class ShowTemp(QThread): + + def __init__(self, w, addr = ""): + QThread.__init__(self) + self.w = w + self.setTerminationEnabled(True) + #Cerco il sensore + self.sensor = w1thermsensor.W1ThermSensor() + + def __del__(self): + print("Shutting down thread") + + def run(self): + global toSleep + while True: + self.w.tempAttuale.setText(str(self.readTemp())+" °C") + sleep(toSleep) + return + + def readTemp(self): + #Leggo la temperatura + temperature_in_celsius = self.sensor.get_temperature() + return temperature_in_celsius + + + +class MainWindow(QMainWindow): + + def __init__(self, parent=None): + super(MainWindow, self).__init__(parent) + file = QFile(os.path.abspath(os.path.dirname(sys.argv[0]))+"/termostato.ui") + file.open(QFile.ReadOnly) + loader = QUiLoader(self) + self.w = loader.load(file) + self.setCentralWidget(self.w) + self.setWindowTitle("Termostato") + self.w.SwitchOn.clicked.connect(self.StopThis) + self.w.tempImpostata.valueChanged.connect(self.setTempImp) + #Devono essere abilitati i 1-Wire + os.system('sudo modprobe w1-gpio') + os.system('sudo modprobe w1-therm') + self.alreadyOn = False + self.w.tempImpostata.setValue(25) + self.setTempImp(25) + self.stoponreached = False + self.getTempThread = ShowTemp(self.w) + self.getTempThread.start() + + def itIsOff(self): + self.w.SwitchOn.setChecked(False) + self.StopThis() + + def reached(self): + print("Temperatura raggiunta") + if self.stoponreached: + self.itIsOff() + + def dostuff(self): + if not self.alreadyOn: + self.myThread = TurnOn(self.w) + self.myThread.TempReached.connect(self.reached) + self.myThread.finished.connect(self.itIsOff) + self.myThread.start() + self.alreadyOn = True + + def StopThis(self): + if self.w.SwitchOn.isChecked(): + self.w.SwitchOn.setText("Spegni") + self.dostuff() + else: + self.w.SwitchOn.setText("Accendi") + self.alreadyOn = False + + def setTempImp(self, arg1): + self.w.tempImpostataLabel.setText(str(arg1)+" °C") + self.dostuff() + +if __name__ == "__main__": + app = QApplication(sys.argv) + w = MainWindow() + w.show() + sys.exit(app.exec_()) diff --git a/termostato.ui b/termostato.ui new file mode 100644 index 0000000..a395086 --- /dev/null +++ b/termostato.ui @@ -0,0 +1,150 @@ + + + MainWindow + + + + 0 + 0 + 535 + 333 + + + + MainWindow + + + + + + + + + + + Temperatura attuale: + + + + + + + + 36 + 75 + true + true + + + + 0 °C + + + Qt::AlignCenter + + + + + + + Qt::Vertical + + + QSizePolicy::Preferred + + + + 20 + 40 + + + + + + + + + + Qt::Vertical + + + + + + + + + Temperatura da mantenere: + + + + + + + + 36 + 75 + true + true + + + + 0 °C + + + Qt::AlignCenter + + + + + + + -20 + + + 50 + + + 25 + + + Qt::Horizontal + + + false + + + false + + + false + + + false + + + + + + + + + + + Spegni + + + true + + + true + + + + + + + + + + -- cgit v1.1