Skip to content

Instantly share code, notes, and snippets.

@walkure
Created September 6, 2017 12:09
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 walkure/1b86ad4d68994294be8db07653d2f561 to your computer and use it in GitHub Desktop.
Save walkure/1b86ad4d68994294be8db07653d2f561 to your computer and use it in GitHub Desktop.
Raspberry Piのファン制御スクリプト
#!/usr/bin/python3
import RPi.GPIO as GPIO
def main() -> None:
tmp = get_cpu_temp()
print("CPU temp:{0}".format(tmp))
if(tmp > 60):
fan_control(True)
elif(tmp < 50):
fan_control(False)
def fan_control(active: bool) -> None:
GPIO.setmode(GPIO.BCM)
# ignore warning 'This channel is already in use.'
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, active)
# if cleanup, output always turns off.
# GPIO.cleanup()
def get_cpu_temp() -> None:
with open('/sys/class/thermal/thermal_zone0/temp') as f:
temp = f.readline()
return int(temp) / 1000
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment