Indoor gardening is a great hobby, particularly during the wintertime when outdoor conditions are frigid, and we're spending plenty of time indoors. It also can be a useful way to inexpensively prepare plants for the Spring, as a package of seeds are a fraction of the cost of a tray of plants. Plants give off oxygen, and help clean the air, so there's limited downside to creating an indoor garden - but how to get started?
This project builds a 21st century garden that enables tools for scientific growing as well as sharing the greenery with the world by sharing your garden via the internet. The sensors are built from a Raspberry Pi, and the monitoring and analysis is done using Amazon Web Services (AWS). Communication is done using API's starting from your home wireless network, then out to the Internet and into AWS and Twitter.
Here's a brief overview of the different layers of technology, and the narrative below will describe how to build each layer with step-by-step instructions. Please note, I've included all of the software written to build this in a single GitHub repo.
https://github.com/terrenjpeterson/raspberry
Within the repo there are four folders, and in each section is called out which folder is being used.
To collect information about the garden, we're going to be using sensors that can collect critical data about growing conditions. For this project, I recommend a DHT22 humidity and temperature sensor running on a Raspberry Pi.
Section 1 - Selecting Raspberry Pi Equipment
The sensors will be driven by a Raspberry Pi, and either the original or series 2 models will work. If you are purchasing one, I would suggest the series 2 as having the extra USB ports is very useful. You will also need a wireless adapter that can connect to your home WiFi network.
Section 2 - Collecting Humidity and Temperature
Understanding what the temperature and humidity is for the growing environment is critical, particularly in wintertime. During the growing (light) cycle, temperatures for many plants need to resemble a nice Spring or Summer day, so you're aiming for at least 75F (23C) temperatures. The inside of your home also can be quite dry during the winter, so your plants may dry out quickly. Monitoring the humidity of the air provides good indicators when the soil itself is dry, so is very useful information.
Step 1 - Connect the Sensor to the Raspberry Pi
I purchased a DHT22 sensor which is readily available from several retailers. The circuit diagram is also on this page, and below is a photo of the completed circuit running on a 30 row breadboard.
Step 2 - Install Sensor Drivers
For the sensor to be used, you'll need to install the C libraries that translate the basic signal data into usable temperature and relative humidity readings.
https://github.com/adafruit/Adafruit_Python_DHT
Installation instructions can be found on the associated GitHub page.
Step 3 - Build the Sensor App
SI've already written an app that uses, so you can clone the repo from this location onto your Raspberry Pi. There is additional documentation in the repo that can assist you along the way.
https://github.com/terrenjpeterson/raspberry
There are two programs needed for this sensor in the repo, stream.sh and simpletest.py. Both are located in the /sensor folder of the repo.
To start collecting the data, run the script on the Raspberry Pi, executing the bash script that will continuously execute the python program. Here is the command to enter to start the script and then run in a headless mode.
$ bash stream.sh & exit
This will record the data from the sensor every 20 seconds (driven by a sleep parameter in the script). Data is saved locally on the Pi, and in the next part I'll explain how to configure this to also do HTTP calls to push the data out to the internet.
Part 2 - Configure a Camera to Capture ImagesI like to be able to see how the plants are growing, and record the physical growth of the plants. This can be done quite easily with the Pi by acquiring a camera that can be connected to one of the serial ports.
Step 1 - Attach the camera to the Pi
Here are the setup instructions to physically attach the camera to the the Pi. It's a serial cable, and I advise to attach it before putting the board in a case.
https://www.raspberrypi.org/products/camera-module/
The serial cable comes in various lengths, and there are optional parts you can purchase that house the camera itself.
Step 2 - Setup the Pi to Capture Photos
To execute the camera, you'll need to install the picamera libraries, and instructions for doing so can be found here.
http://picamera.readthedocs.org/
Once the libraries have been installed, go into the /camera folder of the GitHub repo, and you will find some scripts that use the camera. The first one is called camera_setup.py and it activates the camera, streaming the output to the HDMI port. The script sets the resolution, and be sure to test out the hflip and vflip functions that orient the camera image to be correct, and not inverted or reversed depending on where your camera is mounted. The next script camera_test.py will capture a still photo from the camera onto the SD card attached to the Pi. You can overlay text onto the image using the annotate_text function.
Part 3 - Collect data on the Cloud using AWSHaving data locally on the Pi is good, but pushing the data out to the cloud opens up some great options around how to analyze the growing conditions with analytics, and use the insight to help the plant growth.
Step 1 - Setup S3 bucket to store data
The AWS service that handles object storage is called S3 - Simple Storage Service. Instructions on provisioning a bucket can be found on the AWS website here.
http://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html
Create a bucket name that is meaningful, and you can establish a file system to store data from different sensors. Charges are consumption based, so you're not preprovisioning a block of space and it's $0.03/GB per month.
Step 2 - Create an API that stores Humidity & Temperature sensor data
For data to be saved into the bucket, we'll need to write a program that can store data that's passed in through an Application Programming Interface (API). Currently this is a nodeJS program called garden.js that runs on an virtual server instance at AWS. You will need to provision a Linux EC2 instance to run this API. This program is located in the /api folder of the repo. Make sure and take the name of your S3 bucket created in step 1 and update the API.
// this is the name of the bucket that is used
var dataBucket = 'myBucketName';
The sensor data will be stored twice in this bucket. First will be a json object called current.json, and will contain the latest information recorded for the device. Below is a sample of what it will read.
{"read_time": "21:26:48", "sensor": "AM2302", "temperature": "74.6", "read_date": "2016-01-26", "humidity": "52.2"}
The second json object will be an array, and will continuously append the same object for the entire day. The naming convention for the object is YYYYMMDD.json (for example 20160126.json).
Step 3 - Configure the temperature & moisture sensor to call the API
We'll need to go back into the python application that we setup in the first part and put the address of the EC2 instance. In the simpletest.py program, include the name of your EC2 instance to match what was provided in Step 2.
# establish the endpoint that the API will be located at
Apiurl = 'http://ec2-11-22-33-44.us-west-2.compute.amazonaws.com:8080'
If you now go into the S3 bucket, you should see data being written to it. Later on in Part 4 we will create a user interface that makes use of this data.
Step 4 - Configure the camera to upload timelapse photos
Earlier in Part 2 we covered how to setup the camera and capture images locally on the SD card. You can also upload those photos to the Cloud using the AWS Python SDK called boto. The attachment provides the instructions in how to setup.
Included in the instructions is how to provision credentials, and you'll need them to configure the script that is in the /camera folder of the repo. Include them in the aws.config file on your raspberry pi. Next, configure the upload_img.py script to write to your bucket created in step 1. Here is the line to change in the program.
# configure the correct S3 bucket and API parameters
bucket = 'myBucketName'
Once this has been completed, you should be able to execute this script, and it will capture the image of your garden. Here's an example from my camera.
What size of garden to build is dependent upon how much space you have, and how ambitious you are in growing plants. Below highlights what I built for this project, but change based on your space needs.
Step 1 - Build the basic frame
I have a corner of my home office that I wasn't using and was a great space to work with. This gave me a four foot by two foot area where I could build a wood shelf and frame for the plants to stand on. This was constructed from basic 2x4's from the local home improvement store, along with a piece of 1/8" fiber board for the flooring.
Step 2 - Establish lighting
The most convenient area I had to grow wasn't near a window, so I needed to use artificial light. We already had a series of florescent grow lights in the garage that I pulled out for this project, and I installed four of them, each with two 60W Sylvania Grow Light bulbs.
When hanging the lighting, it's important to recognize how difficult it is to simulate the intensity of the sun. You will want to hang the lights on chains or something else that can be altered during the growing process so the bulbs can be as close as possible to the plants. I currently have the lights 18" above the top of the soil level of the pots for the beans and tomatoes, and 6" above the lettuce and cilantro. As the plants grow, I will adjust the chain lengths.
I'm also using a timer for the lights, so I can control how often they go off and on. Currently they're set to go on at 7AM, and turn off at 9PM. This is for all four light bars.
Step 3 - Insulate the growing area
Given that we're trying to have higher temperatures to simulate outdoor conditions, it's important to think of how to create higher temperatures, particularly for plants like tomatoes that thrive in warm conditions. The lighting does give off some level of heat, and if you build some basic insulation around the growing area that traps the energy, you can raise the temperature. I used a roll of Reflectix from my local hardware store, and it's easy to use. The material is like bubble wrap, and has the added benefit of being reflective which is great to encapsulate the growing area. The normal temperatures for our house in the winter are 68F (20C) but the insulation got the garden temps up to 82F (27C) which the tomato plants love.
Step 4 - Connect through powerbar
It's always good to be safe, so investing in a powerbar with a fuse that will trip in case of a short is always a good idea. The lights are connected through it.
Part 5 - Grow and MonitorThere are lots of plants to choose from, but I started out with four different seeds from our local nursery. I also purchased some starter soil and plants. There are many resources on the internet to assist in determining what temperature and watering conditions are relevant for each type of plant.
To remotely monitor the growing area from the Cloud, a basic website is needed. The following steps go through the setup of the website, as well as how you can begin to leverage the analytic tools to gain insight into the sensor data.
Step 1 - Acquire an internet domain name
To setup a website requires a domain name. This requires a little creativity to find one that is memorable, yet not taken by someone else. Going into Route 53, I was able to find one (robotgardening.com) and it only cost $12 for the year.
Step 2 - Create a basic home page
Now that you have a name for your garden, it's time to setup the website. Once again, the source code that I used is in the GitHub repo, so you can start from it, and just change as you see fit. These pages are under the /chart folder of the repo.
For this step, you'll need to setup another S3 bucket on AWS, but this time configure it so that can be used for hosting an internet site. This is a really useful feature as you will only pay for the number of times the website is accessed which results in very low charges, and you don't have to setup a webserver.
The name of the bucket should match what the website you just registered, and then click "Enable Website Hosting". It should look like this.
Once you've setup the bucket, modify the index.html file to share a little about your project, and then upload to the bucket. The test out with a browser by hitting the domain name.
Step 3 - Setup a dashboard to share real-time data
In addition to sharing information about your project, you can share the real-time data coming from the garden to anyone that has internet access. First, setup an API endpoint that contains the link to where the current data is stored in your S3 bucket. By abstracting away, we won't expose the bucket name to the outside world. Go into the API Gateway on AWS, and create a simple API, and then a GET method that contains enter the path for your bucket like this.
Now modify the garden.html file that is located in the /chart folder of the repo with the API name that was just created above. Here is the exact javascript code you will be modifying.
function drawChart() {
// variable for API that abstracts where the latest sensor data is located
var Apiurl = "https://xxx123.execute-api.us-west-2.amazonaws.com/prod";
Now upload the modified garden.html file into the S3 bucket just like you did in with the index.html file. This will be the rendered from your domain URL, and will constantly refresh based on the changing readings.
Now you should be able to get the current sensor data anywhere in the world!
Step 4 - Analyze your sensor data and visualize (optional - more advanced)
Once the sensor data has been ported to AWS, you have access to unlimited computing power and powerful analytic tools. The following is just one simple example of how you can use this to gain insight in how to grow better plants.
First create a Lambda function using the API located in the /api folder called getRecentTempData. This is a node script that runs an simple algorithm that aggregates sensor data into intervals for the current day, and returns back an array of summarized data. Once again you'll want to change this to use your S3 bucket where your sensor data is stored.
// this is the bucket where the sensor data is stored.
var dataBucket = 'myBucketName';
Once the Lambda function is created, now expose it through the API gateway. This requires creating another API endpoint, and you'll need to record it as it's specific to your analytics.
Now let's create a visualization of the array using Google Charts. A sample of how to do this can be found in the /chart folder of the repo, with a file name of temp_history.html. Go into the file and update the API endpoint with the name you just created.
function drawChart() {
// variable for API that abstracts where the latest sensor data is located
var Apiurl = "https://xxx123.execute-api.us-west-2.amazonaws.com/prod";
Now upload the updated version of the html file into your S3 bucket that contains the html files for your website just like you did with the prior chart. It should provide a graph similar to the one below showing temperature in the blue line and humidity in the red. It's easy to tell from this chart when the artificial lights went on!
With the sensor data being saved away on AWS, you can continue to explore different algorithms, and see what happens if you change the garden structure. Data doesn't need to be saved on the Pi, and can be occasionally pruned to free up space.
Step 5 - Setup Notifications for Events
Given that we're trying to make the garden as autonomous as possible, it's not likely that you will always be able to have the metrics dashboard readily available. This is where setting up some notifications can be very useful, and use the power of analytics on the Cloud to detect when an event has occurred.
First create a notification topic under your AWS account, the subscribe to it with your e-mail. Remember what the ARN (Amazon Resource Name) is for the topic that was provided. Detailed instructions for how to create the topic and subscribe to it are provided on the AWS page below.
https://aws.amazon.com/documentation/sns/
Then create a Lambda function in AWS that can mine the sensor data that we're storing in the S3 bucket. Detailed instructions for how to create the new Lambda function can be found here. As part of the setup, the execution role for the function will need to have access to S3 and SNS for your account.
https://aws.amazon.com/documentation/lambda/
The source code for the Lambda function is located in the repo I've provided under the /analytics folder and is called temp_alert.py. Use this as your function and change the first parameter in the handler to match the topic you setup.
# configure parameter to specific notification topic
topic_arn = 'arn:aws:sns:us-west-2:xxx-xxx:Garden-Notification'
Lambda allows scheduling triggers to be set for how often the function is invoked. This is called a "Scheduled Event". Setting up the function to run every fifteen minutes should be adequate for this check.
Part 6 - Social Media IntegrationNow this is just starting to have fun with the garden, and isn't related to any growth of the plants. To add a little more sharing, I've added integration with Twitter.
Step 1 - Create a Twitter App
I'm assuming that you already have a twitter account (or at least know how to get one) but an additional capability will we need is to build a Twitter Apps. These allow integration of content through the twitter API's, and we'll be doing that to share out our sensor readings. First, here is where you'll need to go on twitter.
Step 2 - Create Credentials for the App
Once you create the app, you'll be able to get credentials established that will be used to setup the API's.
Go to the "Keys and Access Tokens" tab, and you will get a consumer key pair. Then request an access token which is a second key pair. (I'm not showing the tab given that these credentials are private, and should be safeguarded)
Step 3 - Configure the Twitter App on the Raspberry Pi
Now let's go back to the repo, and pull down the twitter.config file from the /camera folder so that we can configure it with the credentials above.
[My Secret Data]
access_token =
access_token_secret =
API_key =
API_secret =
Copy what twitter gave you in the consumer key pair in the first two, then the access token into the second two. This file should be installed on the Raspberry Pi, and is what will enable twitter to authorize the tweets for the account.
Next, go into the tweet.py script into the same /camera folder for the repo and change the end-point variable to match where your current sensor data is. Here are the exact lines you will need to change.
# this is the API location for the sensor data and should be changed.
var Apiurl = "https://xxx123.execute-api.us-west-2.amazonaws.com/prod"
Step 4 - Start Tweeting
Once that has been completed, you should be able to run the script and the latest sensor data will be tweeted out.
This gives a second way of sharing out the sensor data in addition to the website.
Good luck with the project and feel free to check on my progress either in the github repo, at http://www.robotgardening.com, or follow the garden on twitter using @robot_gardening.
Comments