Skip to content

Instantly share code, notes, and snippets.

@j4velin
Last active March 7, 2020 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j4velin/63d3c30208cdb4ab32a3a0d646aa97a2 to your computer and use it in GitHub Desktop.
Save j4velin/63d3c30208cdb4ab32a3a0d646aa97a2 to your computer and use it in GitHub Desktop.
Temperature based fan control script for Raspberry Pi 4
#! /usr/bin/python
import subprocess
import time
import re
from gpiozero import LED
PIN = LED(17)
MAX_TEMP = 50
MIN_TEMP = 45
PATTERN = "^temp=([0-9.]*)'C$"
def getTemp():
result = subprocess.check_output(["sudo", "vcgencmd", "measure_temp"])
match = re.search(PATTERN, result)
return float(match.group(1))
while(True):
temperature = getTemp()
if temperature >= MAX_TEMP:
PIN.on()
elif temperature <= MIN_TEMP:
PIN.off()
#print str(temperature) + " -> " + str(PIN.is_lit)
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment