Skip to content

Commit 77c8698

Browse files
committed
python script tested & working
1 parent beec11e commit 77c8698

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

fan_script.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import schedule #package required to run the fan task at a set time(s)
2+
import time #package required to gauge time itself for scheduled tasks
3+
import subprocess #required to run tempreture command
4+
import RPi.GPIO as GPIO #imports the RPi.GPIO library
5+
from gpiozero import Button
6+
7+
fanPin = 14 #defines GPIO14 as the fan pin - as per internal connection
8+
9+
GPIO.setmode(GPIO.BCM) #sets the board mode for GPIO pinout
10+
GPIO.setwarnings(False) #turns GPIO warnings off - required as you get a 'channel always in use' error without it
11+
GPIO.setup(fanPin, GPIO.OUT) #defines the fan pin as an output pin
12+
13+
def fanOn():
14+
tempRaw = subprocess.check_output("vcgencmd measure_temp", shell=True).strip()
15+
tempString = tempRaw.decode("utf-8") #decodes the output into a string
16+
#print(tempString)
17+
tempStrLeft = tempString.lstrip("btemop=")
18+
tempStrRight = tempStrLeft.rstrip("'C")
19+
tempreture = float(tempStrRight)
20+
if tempreture >= 60.0:
21+
#print("hot hot hot")
22+
GPIO.output(14,True) #turns the fan on
23+
if tempreture < 45.0:
24+
GPIO.output(14,False) #turns the fan off
25+
26+
schedule.every(10).seconds.do(fanOn) #runs the fan_on task every ten seconds
27+
28+
while True:
29+
schedule.run_pending()
30+
time.sleep(1)
31+

0 commit comments

Comments
 (0)