Automated Greenhouse Power Control with a Raspberry Pi

This can really control anything that requires turning on and off power. I am using it to turn on a heater and a fan based on the room temperature for better temperature and air circulation control. It would be excellent for opening a greenhouse vent based on the temperature in the room. I plan to hook it up to a humidifier or dehumidifier for vegetable storage later in the year. There are numerous applications this could be used for, I am sure I haven’t thought of them all. It is also a fun project to put together and convenient to have.

My room temperature is more stable now that I can monitor it by my temperature and humidity logging setup. Because of the results, I turn my radiant heater to a temperature that keeps the room in a better range. I still wanted more control. If I had a heater with a thermostat, I could use that. The benefit of my solution is that I could control the heat and fan from anywhere in the world. I didn’t set it up to do that for now. Right now, I am happy with just having more control because I don’t have a heater with a thermostat.

My current set up turns on the heater and the fan if the temperature gets to less than 67 degrees F and off when it gets to 75 degrees F. The fan is mostly to give a little breeze in the room to keep the seedlings stronger and help the air circulation. I made the range a little wide, I don’t want the fan and heater to be turning on and off constantly.

I started with my current set up which included setting up the raspberry pi, setting up temperature and humidity logging, and setting up automated emails. I highly recommend reviewing those three blog posts first before jumping to the code in this post or it may not make a lot of sense.

Setting up the IoT Relay

IoT Relay used for power control from the Raspberry Pi

I chose this device because it was reasonably priced, it contains more than one outlet, it has excellent reviews, and it is fully enclosed. I don’t want my kids near any high-powered devices that aren’t enclosed. Two of the outlets are default ON and two of the outlets are default OFF. The IoT relay can be controlled by the 3.3V control signal that the Raspberry Pi provides. I found that it works reliably and was very easy to get working.

I am including all of the steps that I went through, providing the code below, and the code for download.

Prepare the Current Code

The first thing I did was turn down my logging. Right now I have it logging every 5 minutes. I have found that the temperature just doesn’t change fast enough to need to know every 5 minutes. I turned down the logging to 15 minutes and then the amount of logging that I send to myself in my automated emails.

In the file I changed to log every 15 minutes by changing the FREQUENCY_SECONDS:


In the I changed it to email me 100 of the last lines from the spreadsheet instead of 300 which will be a little over a day of logging.


Connect the Hardware

Wiring everything up was very easy. I just connect the IoT Relay (spec sheet) to the Raspberry Pi, ground to the ground and the control signal to GPIO (shown in the pinout below). I used pin 17 as my GPIO.

IoT Relay to Raspberry Pi pinout

It took me a little while to discover that the green plastic plug can be pulled out, allowing access to the screw in order to connect the wires in and then screw them in to tighten.

Pull out this green plastic for access in order to secure wire

There really isn’t much to the code. Most of the code I inserted into the file that came with the DHT22 temperature sensor and I previously provided modification instructions for. I also am including some sample code that controls IoT relay without any integration into the temperature sensor code for testing.

Sample Code

The first file is,. It sends an OFF signal, waits 10 seconds, sends an ON signal, waits 10 seconds, and then turns off again

import RPi.GPIO as GPIO
import time


#Set up the board to Broadcom

#Set up to send out signal

print('starting power off')

print('turning power on')

print('turning power off')


Control Power using Temperature Sensor

Next is the code to add to to integrate turning the heater on when the temperature is within a certain range measured by the DHT22 temperature/humidity sensor. This has modifications to the previously added Fahrenheit as instructed in a previous post.

First I insert the imports at the beginning of the file with the other imports

import RPi.GPIO as GPIO

I added this next set of lines after all of the imports at the beginning of the file.

#power outlet control pin
#Change the IOT_RELAY_PIN to your GPIO pin controlling your IOT Relay device
#initialize the power variable

After the temperature has been received or calculated, I added this next section of code. I used the fahrenheit variable. For Celsius, use the temp variable instead. The following code turns the outlet switch on when fahrenheit is less than 67 degrees and turns off when more than 75 degrees. The variables can be adjusted as desired.

if fahrenheit < 67:

if fahrenheit > 75:

I added a column to my spreadsheet that contains if the heater is on or off. To add this I changed this line:

worksheet.append_row((, temp, humidity, fahrenheit))

To this line:

worksheet.append_row((, temp, humidity, fahrenheit, power))

Updated spreadsheet including a column to show if the power is turned on or off. (This is for normally on)

Download the code here.

I hope this is useful. Please share how it works for you and what you are using it for if you try it out.

• $22.95 – Iot Relay – Enclosed High-power Power Relay for Arduino or Raspberry Pi
• 30 – 60 minutes – This is an estimate, since I am providing the code.



[Image Credit: ©Garden4Dinner]

One comment

  1. […] I was worried I wouldn’t finish this on time because life was pretty busy. I was able to automate my temperature, humidity, and fan power control. […]

Leave a Reply