-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple-fan.py
114 lines (98 loc) · 3.29 KB
/
simple-fan.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/susr/bin/python
# >>> THIS SCRIPT ONLY WORKS with a 5v 1.2Amp Fan <<<
# connect red lead to a 3.3v pin (e.g., physical pin 1 or 17)
# connect black lead to GPIOfan (e.g., physical pin 7, GPIO 4)
# on NEMS run
# $ sudo apt-get install python-rpi.gpio python3-rpi.gpio -y
#########################
#
# simple-fan turns a fan on and off when temperature exceeds temperature
# thresholds.
#
# The fan comes with a MiuZei case, and can run on 3.3V (low speed) or
# 5V (high speed). The fan can be plugged into either 3.3V or 5.5v GPIO
# pin and to a ground GPIO pin, but that means the fan will always run
# and that isn't very much fun. Any fan can be used as long it can operate
# at 3.3V
#
# run-fan was tested on a raspberry pi 3b+ running nems linux and kodi on OSMC
#
#########################
#########################
#
# simple-fan.py is based on:
# https://stackoverflow.com/questions/41819683/how-can-i-control-a-fan-with-gpio-on-a-raspberry-pi-3-using-python
#
#########################
#########################
#
# simple-fan starts automatically using systemd
#
# Create a systemd service file using:
# $ sudo nano /lib/systemd/system/simple-fan.service
#
# with the contents as shown below
# remove # and leading spaces:
# [Unit]
# Description=run fan when hot
# After=meadiacenter.service
#
# [Service]
# # If User and Group are not specified as root, then it won't work
# User=root
# Group=root
# Type=simple
# ExecStart=/usr/bin/python /home/pi/simple-fan.py
# >>> Replace the following line: on OSMC or Raspberry Pi OS with Restart=on-failure instead of Restart=Always
# Restart=Always
#
# [Install]
# WantedBy=multi-user.target
#
# end of the simple-fan.service
# ctrl-o, ENTER, ctrl-x to save and exit the nano editor
#
# After any changes to /lib/systemd/system/simple-fan.service:
# sudo systemctl daemon-reload
# sudo systemctl enable simple-fan.service
# sudo reboot
#
# Ensure the run-fan.service in systemd is enabled and running:
# systemctl list-unit-files | grep enabled | grep simple
# systemctl | grep running | grep fan
# systemctl status simple-fan.service -l
#
# If there are any issues with starting the script using systemd,
# then examine the journal using:
# sudo journalctl -u simple-fan.service
#
#########################
import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO
GPIOfan = 26
TurnOnTemp = 65.0
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIOfan, GPIO.OUT)
while True: # Loop forever
# Read the current temperature
# osmc requires path
res = os.popen('/opt/vc/bin/vcgencmd measure_temp').readline()
# other OS's do not require path or may use different path
# use $ which vcgencmd to find path
# res = os.popen('vcgencmd measure_temp').readline()
temp = float((res.replace("temp=","").replace("'C\n","")))
# print 'Temperature from vcgencmd: {}'.format(temp)
# Control the fan
if temp > TurnOnTemp:
# print 'Turning on fan = ' + str(GPIOfan)
# The red fan pin is connected to 3.3V and the GPIOfan pin is the black pin on the fan
# this doesn't seem to make any sense, but it works
GPIO.output(GPIOfan, False)
else:
# print 'Turning off fan = ' + str(GPIOfan)
GPIO.output(GPIOfan, True)
# Wait before the next iteration
sleep(10)