随笔

记录,分享,然后享受生活。

0%

自动降温控制脚本

采用一个S9014型号的NPN型三极管,基极接在GPIO14口上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python2
#coding:utf8
import sys
import time
import os
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
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)
GPIO.setup(channel, GPIO.OUT, initial=GPIO.LOW)
is_close = True
while True:
temp = cpu_temp()
if is_close:
if temp >= 58.0:
url = "http://tts.youdao.com/fanyivoice?word=开启风扇&le=zh&keyfrom=speaker-target"
os.system('mplayer -loop 1 -softvol -softvol-max 100 "%s"' % url)
GPIO.output(channel, 1)
is_close = False
else:
if temp <= 40.0:
url = "http://tts.youdao.com/fanyivoice?word=关闭风扇&le=zh&keyfrom=speaker-target"
os.system('mplayer -loop 1 -softvol -softvol-max 100 "%s"' % url)
GPIO.output(channel, 0)
is_close = True
time.sleep(10)
if __name__ == '__main__':
main()

三极管的用法

树莓派GPIO