Python Forum
If/ Or if/ temp controlled fan.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/ Or if/ temp controlled fan.
#1
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
Reply


Messages In This Thread
If/ Or if/ temp controlled fan. - by clueless - Dec-24-2017, 03:58 PM
RE: If/ Or if/ temp controlled fan. - by squenson - Dec-24-2017, 04:30 PM
RE: If/ Or if/ temp controlled fan. - by clueless - Dec-24-2017, 05:20 PM
RE: If/ Or if/ temp controlled fan. - by bogo10 - Dec-24-2017, 06:36 PM
RE: If/ Or if/ temp controlled fan. - by clueless - Dec-24-2017, 07:03 PM
RE: If/ Or if/ temp controlled fan. - by DeaD_EyE - Dec-25-2017, 03:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Deleting Windows temp folder Raysz 7 606 Apr-02-2024, 12:36 PM
Last Post: Raysz
  Print controlled with global-var fmr300 3 1,518 Feb-05-2022, 03:09 PM
Last Post: fmr300
  pyspark creating temp files in /tmp folder aliyesami 1 5,190 Oct-16-2021, 05:15 PM
Last Post: aliyesami
  Help with storing temp data for each day then recording min/max in app. trthskr4 3 2,484 Sep-10-2021, 10:51 PM
Last Post: trthskr4
  How to save Matplot chart to temp file? Morkus 2 4,631 Jun-12-2021, 10:52 AM
Last Post: Morkus
  Get system info from PI (cpu load and temp) korenron 2 2,146 Aug-04-2019, 08:45 AM
Last Post: korenron
  temp analysis Simba 13 6,029 Apr-25-2019, 09:13 PM
Last Post: Simba
  Temp folder creation ste1605 7 5,424 Oct-03-2018, 10:39 AM
Last Post: buran
  Problem with remove Temp Files karlo_ds 1 3,195 Oct-26-2017, 11:42 PM
Last Post: wavic
  How to create a value system with a temp/humidity sensor? Tacoon 2 3,495 Feb-13-2017, 03:11 PM
Last Post: Tacoon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020