Pi based temp controlled relay help

Heya,

I’m currently attempting to build a auto greenhouse and so far have:

Raspberry Pi 3b
Seeed Studio Hat
Grove Temp & Humidity Sensor
Grove - 4-Channel SPDT Relay

So far I have managed to get the temp sensor working but know I need to figure out how to code it so the relay will turn on and off at certain temps, I’m no coder so any help is appreciated In finding out how to write the code for this project?

Thanks!

Node.

Hi @nodejunky, Can you share your existing code and share the details what you need to achieve?

If you only need to control a relay using temperature, you can use a simple conditional statement
example:

if(temp < 50)
{
// turn on relay 
}
else {
// turn off relay
}

50 is the threshold temperature when the temperature is great than 50, code inside the if clause will run once and check the condition again(that’s depends on reset of the code) if temperature is lessthan 50 the relay remain off.

1 Like

Hey @salman I dont really have any code as of yet I just have the temp reading out like in the example code for the DHT11 sensor, I’m a bit confused as to what I should be using to program this as the sensor is in Python & I cannot find out how to load the relay as theres only code for Arduino not the Pi for the relays.

Sorry hope that makes sence…

You can find the code examples in the https://wiki.seeedstudio.com/Grove-Relay/ GitHub repo,

Step 1 . Follow Setting Software to configure the development environment.
Step 2 . Download the source file by cloning the grove.py library.

cd ~
git clone https://github.com/Seeed-Studio/grove.py

Step 3 . Execute below commands to run the code.

cd grove.py/grove

python grove_relay.py 12

Following is the grove_relay.py code.

from grove.gpio import GPIO
 
 
class GroveRelay(GPIO):
    def __init__(self, pin):
        super(GroveRelay, self).__init__(pin, GPIO.OUT)
 
    def on(self):
        self.write(1)
 
    def off(self):
        self.write(0)
 
 
Grove = GroveRelay
 
 
def main():
    import sys
    import time
 
    if len(sys.argv) < 2:
        print('Usage: {} pin'.format(sys.argv[0]))
        sys.exit(1)
 
    relay = GroveRelay(int(sys.argv[1]))
 
    while True:
        try:
            relay.on()
            time.sleep(1)
            relay.off()
            time.sleep(1)
        except KeyboardInterrupt:
            relay.off()
            print("exit")
            exit(1)            
 
if __name__ == '__main__':
    main()

Source: https://wiki.seeedstudio.com/Grove-Relay/

1 Like

@salman thank you sir you are a legend that will get me going! thanks for your help!

Node.

Glad to know that :blush: :+1:. Happy Building

1 Like