-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
62 lines (55 loc) · 1.36 KB
/
control.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import time
import RPi.GPIO as GPIO
FAN_GPIO = 14 # 针脚 博客上边有针脚图
SLEEP_TIME = 60 # 每60秒检测温度并修改一次频率
mode = "mute" # 模式
GPIO.setmode(GPIO.BCM)
GPIO.setup(FAN_GPIO, GPIO.OUT)
p = GPIO.PWM(FAN_GPIO, 331)
p.start(50)
# temp 温度
# normal 默认 pwm频率
# mute 静音
arr = [
{
"temp": 50,
"normal": 100,
"mute": 30
},
{
"temp": 48,
"normal": 90,
"mute": 30
},
{
"temp": 45,
"normal": 70,
"mute": 30
},
{
"temp": 40,
"normal": 40,
"mute": 30
}
]
# 注:pwm 范围为0 - 100 值越大转的越快 反之则越慢
while True:
# 获取CPU温度
tmpFile = open('/sys/class/thermal/thermal_zone0/temp')
cpu_temp_raw = tmpFile.read()
tmpFile.close()
cpu_temp = round(float(cpu_temp_raw) / 1000, 1)
t = cpu_temp
# 取时间 修改模式 20:00 - 2:00 为静音模式
n = time.asctime(time.localtime(time.time())) # 当前时间
nowHour = int(time.strftime("%H", time.localtime())) # 当前小时
if nowHour >= 20 or nowHour <= 2:
mode = "mute"
else:
mode = "normal"
for item in arr:
if t >= item["temp"]:
p.ChangeDutyCycle(item[mode])
print(item[mode], t, mode, n)
time.sleep(SLEEP_TIME)
break