-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCH340-fan-control.py
More file actions
executable file
·77 lines (53 loc) · 1.97 KB
/
CH340-fan-control.py
File metadata and controls
executable file
·77 lines (53 loc) · 1.97 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# QinHeng Electronics CH-340 / HL-340 USB relay fan controller script
# By Dan MacDonald, 2021
# You must install the python USB modules to run this:
# sudo apt install python-usb
# or
# pacman -S python-pyusb
# To run with pyusb debugging:
#
# PYUSB_DEBUG=debug python relay.py
import subprocess, time, usb.core, usb.util
dev = usb.core.find(idVendor=0x1a86, idProduct=0x7523)
if dev is None:
raise ValueError("Device not found")
if dev.is_kernel_driver_active(0):
dev.detach_kernel_driver(0)
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
close_relay_cmd = [0xA0, 0x01, 0x01, 0xA2]
open_relay_cmd = [0xA0, 0x01, 0x00, 0xA1]
ON_THRESHOLD = 80 # (degrees Celsius) Fan kicks on at this temperature
OFF_THRESHOLD = 70 # (degress Celsius) Fan shuts off at this temperature
SLEEP_INTERVAL = 5 # (seconds) How often we check the core temperature
# function to get the CPU temp
def getCPUTemp():
f = open("/sys/class/thermal/thermal_zone0/temp")
CPUTemp = f.read()
f.close()
return int(CPUTemp.replace("\n",""))/1000 # remove return from result, cast to int and divide by 1000
if __name__ == '__main__':
# Validate the on and off thresholds
if OFF_THRESHOLD >= ON_THRESHOLD:
raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD')
while True:
temp = getCPUTemp()
# print ("CPU temperature: %d" % (temp))
# Start the fan if the temperature has reached the limit
if temp > ON_THRESHOLD:
ep.write(close_relay_cmd)
# print ("Starting fan")
# Stop the fan if the off threshold is reached
elif temp < OFF_THRESHOLD:
ep.write(open_relay_cmd)
# print ("Stopping fan")
time.sleep(SLEEP_INTERVAL)