summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtermostato.py164
-rw-r--r--termostato.ui150
2 files changed, 314 insertions, 0 deletions
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>535</width>
+ <height>333</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>MainWindow</string>
+ </property>
+ <widget class="QWidget" name="centralwidget">
+ <layout class="QVBoxLayout" name="verticalLayout_3">
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <widget class="QLabel" name="label">
+ <property name="text">
+ <string>Temperatura attuale:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLabel" name="tempAttuale">
+ <property name="font">
+ <font>
+ <pointsize>36</pointsize>
+ <weight>75</weight>
+ <italic>true</italic>
+ <bold>true</bold>
+ </font>
+ </property>
+ <property name="text">
+ <string>0 °C</string>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignCenter</set>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <spacer name="verticalSpacer">
+ <property name="orientation">
+ <enum>Qt::Vertical</enum>
+ </property>
+ <property name="sizeType">
+ <enum>QSizePolicy::Preferred</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>20</width>
+ <height>40</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <widget class="Line" name="line">
+ <property name="orientation">
+ <enum>Qt::Vertical</enum>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <layout class="QVBoxLayout" name="verticalLayout_2">
+ <item>
+ <widget class="QLabel" name="label_2">
+ <property name="text">
+ <string>Temperatura da mantenere:</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLabel" name="tempImpostataLabel">
+ <property name="font">
+ <font>
+ <pointsize>36</pointsize>
+ <weight>75</weight>
+ <italic>true</italic>
+ <bold>true</bold>
+ </font>
+ </property>
+ <property name="text">
+ <string>0 °C</string>
+ </property>
+ <property name="alignment">
+ <set>Qt::AlignCenter</set>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QDial" name="tempImpostata">
+ <property name="minimum">
+ <number>-20</number>
+ </property>
+ <property name="maximum">
+ <number>50</number>
+ </property>
+ <property name="value">
+ <number>25</number>
+ </property>
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="invertedAppearance">
+ <bool>false</bool>
+ </property>
+ <property name="invertedControls">
+ <bool>false</bool>
+ </property>
+ <property name="wrapping">
+ <bool>false</bool>
+ </property>
+ <property name="notchesVisible">
+ <bool>false</bool>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <widget class="QPushButton" name="SwitchOn">
+ <property name="text">
+ <string>Spegni</string>
+ </property>
+ <property name="checkable">
+ <bool>true</bool>
+ </property>
+ <property name="checked">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>