Skip to content

Instantly share code, notes, and snippets.

@AlanDecode
Last active January 24, 2021 13:18
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 AlanDecode/b3081f849cc9f603cb462a8ee768a1b1 to your computer and use it in GitHub Desktop.
Save AlanDecode/b3081f849cc9f603cb462a8ee768a1b1 to your computer and use it in GitHub Desktop.
树莓派风扇控制代码
#!/usr/bin/python3
import time
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO!")
def cpu_temp():
with open("/sys/class/thermal/thermal_zone0/temp", 'r') as f:
return float(f.read())/1000
def main():
channel = 14
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# close air fan first
GPIO.setup(channel, GPIO.OUT, initial=GPIO.LOW)
is_close = True
while True:
temp = cpu_temp()
output = ' '.join([str(time.ctime()), str(temp)])
if is_close:
if temp >= 65:
GPIO.output(channel, GPIO.HIGH)
is_close = False
else:
if temp < 55:
GPIO.output(channel, GPIO.LOW)
is_close = True
if is_close:
output += ' fan off'
else:
output += ' fan on'
with open('/var/log/autofan.log', 'a+') as f:
print(output, file=f)
time.sleep(2.0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment