Last active
May 27, 2018 17:10
-
-
Save stepanmas/b7119a3268f6af90af808e1d10eca636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Author: Edoardo Paolo Scalafiotti <edoardo849@gmail.com> | |
import os | |
from time import sleep | |
import signal | |
import sys | |
import RPi.GPIO as GPIO | |
pin = 12 # The pin ID, edit here to change it | |
maxTMP = 55 # The maximum temperature in Celsius after which we trigger the fan | |
fan_off_on = 45 | |
def setup(): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.setwarnings(False) | |
return () | |
def getCPUtemperature(): | |
res = os.popen('/opt/vc/bin/vcgencmd measure_temp').readline() | |
temp = (res.replace("temp=", "").replace("'C\n", "")) | |
#print(f"Temperature: {temp}") | |
return temp | |
def fanON(): | |
#print('ON') | |
setPin(True) | |
return () | |
def fanOFF(): | |
#print('OFF') | |
setPin(False) | |
return () | |
def getTEMP(): | |
CPU_temp = float(getCPUtemperature()) | |
if CPU_temp > maxTMP: | |
fanON() | |
elif CPU_temp < fan_off_on: | |
fanOFF() | |
return () | |
def setPin(mode): # A little redundant function but useful if you want to add logging | |
GPIO.output(pin, mode) | |
return () | |
try: | |
setup() | |
while True: | |
getTEMP() | |
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want | |
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt | |
GPIO.cleanup() # resets all GPIO ports used by this program | |
# And after, you need add to the crontab a next string | |
# @reboot python /usr/local/bin/fan_control |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment