Dec-24-2017, 03:58 PM
Good afternoon.
I am trying to turn on a fan automatically when it reaches a certain temperature and have borrowed this code to the letter. I really don't know what I'm doing. To me, it looks like in this code the fan will turn on and off repeatedly as the temperature is lowered below the threshold every 5 seconds. It would be much more tolerable if it were to turn on at say 75°C and stay on until it drops to 60°.
Now, instead of using else: fanoff, could I use
or
if CPU_tmp<minTMP
fanoff()
Would this get the desired result if I add minTMP = 60 to the beginning
Also, the section at the bottom, is that intended as is? Is it all supposed to follow try: and is it saying that pressing ctrl+c will turn off the fan until the next scheduled CPU_tmp check?
Thank you all for your time.
pin = 18 # The pin ID, edit here to change it
maxTMP = 75 # The maximum temperature in Celsius after which we trigger the fan
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.setwarnings(False)
return()
def getCPUtemperature():
res = os.popen(‘vcgencmd measure_temp’).readline()
temp =(res.replace(“temp=”,””).replace(“’C\n”,””))
#print(“temp is {0}”.format(temp)) #Uncomment here for testing
return temp
def fanON():
setPin(True)
return()
def fanOFF():
setPin(False)
return()
def getTEMP():
CPU_temp = float(getCPUtemperature())
if CPU_temp>maxTMP:
fanON()
else:
fanOFF()
return()
def setPin(mode): # A little redundant function but useful if you want to add logging
GPIO.output(pin, mode)
return()
try:
setup()
while True:
getTEMP()
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program
I am trying to turn on a fan automatically when it reaches a certain temperature and have borrowed this code to the letter. I really don't know what I'm doing. To me, it looks like in this code the fan will turn on and off repeatedly as the temperature is lowered below the threshold every 5 seconds. It would be much more tolerable if it were to turn on at say 75°C and stay on until it drops to 60°.
Now, instead of using else: fanoff, could I use
or
if CPU_tmp<minTMP
fanoff()
Would this get the desired result if I add minTMP = 60 to the beginning
Also, the section at the bottom, is that intended as is? Is it all supposed to follow try: and is it saying that pressing ctrl+c will turn off the fan until the next scheduled CPU_tmp check?
Thank you all for your time.
pin = 18 # The pin ID, edit here to change it
maxTMP = 75 # The maximum temperature in Celsius after which we trigger the fan
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.setwarnings(False)
return()
def getCPUtemperature():
res = os.popen(‘vcgencmd measure_temp’).readline()
temp =(res.replace(“temp=”,””).replace(“’C\n”,””))
#print(“temp is {0}”.format(temp)) #Uncomment here for testing
return temp
def fanON():
setPin(True)
return()
def fanOFF():
setPin(False)
return()
def getTEMP():
CPU_temp = float(getCPUtemperature())
if CPU_temp>maxTMP:
fanON()
else:
fanOFF()
return()
def setPin(mode): # A little redundant function but useful if you want to add logging
GPIO.output(pin, mode)
return()
try:
setup()
while True:
getTEMP()
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program