haizemailea/fan_control

53 lines
1.7 KiB
Plaintext
Raw Normal View History

2022-02-03 09:45:55 +01:00
#!/usr/bin/python3
import RPi.GPIO as GPIO
import subprocess
import time
class FanStatus(object):
ON_THRESHOLD = 50 # 50 (gradu zentigradu) Haizemailea pizten da.
OFF_THRESHOLD = 43 # 43 (gradu zentrigadu) Haizemailea itzaltzen da.
SLEEP_INTERVAL = 20 # (segundoak) Tenperatura neurketa maiztasuna.
def __init__(self):
super(FanStatus, self).__init__()
self.FanPin = 27 # pin13
def setup(self):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.FanPin, GPIO.OUT)
def get_temp(self):
output = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True)
temp_str = output.stdout.decode()
try:
return float(temp_str.split('=')[1].split('\'')[0])
except (IndexError, ValueError):
raise RuntimeError('Akatsa tenperatura eskuratzerakoan')
def main(self):
# Piztu/Itzaldu balioak egokiak direla ziurtatu
if self.OFF_THRESHOLD >= self.ON_THRESHOLD:
raise RuntimeError('Konfiguraketa akatsa -> ITZALTZE tenperatura PIZTE tenperatura baina txikiagoa izan behar da!')
while True:
temp = f_status.get_temp()
if temp > self.ON_THRESHOLD and not GPIO.input(self.FanPin):
GPIO.output(self.FanPin, True)
elif GPIO.input(self.FanPin) and temp < self.OFF_THRESHOLD:
GPIO.output(self.FanPin, False)
time.sleep(self.SLEEP_INTERVAL)
def destroy(self):
GPIO.cleanup()
f_status = FanStatus()
if __name__ == '__main__':
f_status.setup()
try:
f_status.main()
except KeyboardInterrupt:
f_status.destroy()