Building a Raspberry Pi greenhouse

Scripts and Cron Jobs

Now it is time to orchestrate a working relationship among all of the component parts. The PHP scripts are triggered by cron jobs. They assume the task of orchestrating a working relationship among the parts. You should copy these scripts into /var/www/.

The scripts I have developed include sensor.php, which reads sensor data and writes it into the database every 60 seconds, and fan.php, which another cron job calls up every five minutes to control the fans on the roof.

The algorithm works according to very simple logic: When the temperature in the greenhouse reaches a maximum value defined in the script, the fans start with a minimal rotation speed. If the temperature is still too high at the next iteration, the rotation speed increases. As soon as the temperature falls below the lower limit, the fans stop completely, because the greenhouse should not cool down too much.

You should enter the PWM values in fan.php that you found earlier when testing the fans. The critical temperature level at which the ventilators should start up should also be adapted to local conditions and the plants you are growing.

To keep the plants from dying of thirst, two additional cron jobs and accompanying scripts provide for regular watering. Executing pumpon.php every evening starts the pump, and pumpoff.php turns it off after a few minutes. The index.php script offers an overview of the data relating to the current environment in the greenhouse, as well as a small history of data from the preceding day.

Every night history.php starts via a final cron job. The script writes a dataset every hour of averages from the history database table. Afterward, it erases the original data that had been gathered every 60 seconds. This process reduces the number of datasets collected per sensor per day from 1,440 to 24 so that disk space on the Rasp Pi is not overwhelmed.

It is best to bundle the cron jobs together as raspi-greenhouse-cron under /etc/cron.d. Listing  4 moves the file into the correct folder and makes it executable. You should adapt the running times for your pump to the needs of your plants. Wikipedia does a good job of explaining the syntax of a cron job  [6].

Listing 4

Bundling Cron Jobs

$ sudo mv raspi-greenhouse-cron /etc/cron.d
$ sudo chmod +x /etc/cron.d/raspi-greenhouse-cron

A more elegant solution would not use root rights for every call, because only loldht requires such rights; however, I am only controlling a home greenhouse, so this simplification is perfectly acceptable.


Once hardware and software are in harmony and your Raspberry  Pi is able to display its green thumb, you might wonder how you can further develop your Rasp Pi greenhouse. For example, it should be possible to obtain more information about the status of the plants and the greenhouse. It would certainly not be a bad idea to monitor the rotation speed of the fan motors and generate a warning if one of the fans stops working. Also, a webcam could document the day-to-day growth of the plants.

Although you won't be able to get the Rasp Pi to pull weeds and hunt down slugs, who knows? Maybe a development in the future will take care of those problems.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content