Last active
January 24, 2021 13:18
-
-
Save AlanDecode/b3081f849cc9f603cb462a8ee768a1b1 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/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