sandysound
Posts: 100
Joined: Thu Sep 14, 2017 6:05 pm

Fan turn on at defined temp

Sat May 08, 2021 10:02 pm

Hi,
Looking to turn a fan on when temperature gets to a certain level, and then off when the temperature drops under a certain point.

I am using an envirophat for temperature readings, a 3.3v 4ch optoisolated relay board and a pc fan.
Its a very simple setup with relay control on GPIO(BCM) 10.

My code looks like:

Code: Select all

#!/usr/bin/python

#3.3v 4ch optoisolated Relay
#12v pc fan

from time import sleep
import RPi.GPIO as GPIO
from envirophat import weather

GPIO.setmode(GPIO.BCM)
GPIO.setup(10,GPIO.OUT)

tempconst = "18"
tempnow = weather.temperature()

def relay4on():
    GPIO.output(10,1)
def relay4off():
    GPIO.output(10,0)
def tempread():
    temp = '{:.2f}'.format(tempnow)
    return temp

temperature = tempread()

while True:
    if temperature > tempconst:
        relay4on()
        sleep(60)
    else:
        relay4off()
        sleep(60)
        continue

I would like to schedule this as a cron job at reboot, so it will be running constantly in the background - checking every minute for a change in temp.
Will that code do what I expect it to?

Currently I get the following error:

Code: Select all

Traceback (most recent call last):
  File "relay4.py", line 14, in <module>
    tempnow = weather.temperature()
  File "/usr/lib/python2.7/dist-packages/envirophat/bmp280.py", line 134, in temperature
    self.update()
  File "/usr/lib/python2.7/dist-packages/envirophat/bmp280.py", line 169, in update
    if self._read_byte(REGISTER_CHIPID) == 0x58: # check sensor id 0x58=BMP280
  File "/usr/lib/python2.7/dist-packages/envirophat/bmp280.py", line 116, in _read_byte
    return self.i2c_bus.read_byte_data(self.addr, register)
IOError: [Errno 5] Input/output error
I assume this means I need to check my connections between the pi and the envirophat...?

Cheers,

Sandy

pcmanbob
Posts: 11157
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Fan turn on at defined temp

Sun May 09, 2021 11:20 am

As the envirophat normally plugs directly in to the gpio header pins and you say you have also connected a relay board to gpio 10, it might help if you post pictures showing how you have all this connected.

Your code looks like it should read the temperature ok.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

sandysound
Posts: 100
Joined: Thu Sep 14, 2017 6:05 pm

Re: Fan turn on at defined temp

Sun May 09, 2021 1:52 pm

Thanks for your reply,
I replaced the Envirophat and now everything is working.
Thanks for the affirmation the code should work! Seems to be so far, the fan turned on, just haven't seen it turn off yet... I have to wait for the right conditions to be met.
Cheers,
Sandy

Return to “Python”