Aquaponics: Online Temperature and Humidity


This tutorial is based on the Environment DAQ and while it is designed with aquaponics in mind, it does not require an aquaponics system, making it useful for other projects such as hydroponics or home automation. The included application, therefore, is bare-bones, making it easier to integrate into any other application.

How it works
Here we focus on capturing temperature and relative humidity and sending it to App Engine to be visualized using Google Chart Tools. Every sixty seconds, the Arduino will test its connection to App Engine. The return should be “Ok”, which is parsed by the Arduino. If the connection is ok, the bicolor LED is set to green, otherwise it is set to red. If the connection is good, the Arduino will take a reading from digital pin six (the DHT pin) and create a GET request to App Engine. App Engine will query the datastore (its database) for the Environment entity, update the temperature and relative humidity values and put the entity back in the datastore.

On startup, the web browser (client) will create a temperature and humidity gauge with values at zero. It will then make an AJAX request to the datastore which will return a JSON array. The client parses the array and updates the gauges. Finally, it sets an interval to repeat the process, giving you a dynamically updated display.

Final Result.

This project doesn't go into detail on how the webapp is put together or how and where to customize the webapp code, as that takes many, many more pages.  So we wrote a book that delves deeper into this project and others to create a single data acquisition and control system using the Arduino and App Engine.

Software Versions
1. Arduino IDE: Arduino-1.0.3
2. App Engine SDK: Python, Linux, 1.7.4
3. Python: Python2.7
4. Ubuntu 12.04
5. Adafruit's DHT Arduino library

Parts List
1 x Arduino Uno R3
1 x Arduino Ethernet Shield R3
1 x Arduino Power Cord (optional, but gets better power than just the USB)
1 x Bicolor LED (Red/Green)
1 x 1/4W 470 ohm resistor
1 x 1/4W 4.7k ohm resistor
1 x DHT22
Breadboard jumper wires
Small breadboard

Step 1 -  Install Adafruit's DHT Library
Fig 1.  Adafruit Library
  • You can find the Arduino library for the DHT here, see Fig 1.
  • Download and extract.  Rename the extracted folder "DHT".
  • Copy the folder into: ~/Arduino/arduino-1.0.3/libraries 



Step 2 - Google App Engine
Fig 2.  New GAE Project
  • If you don't already have a free Google account, sign up for one.
  • Use your Google account to sign up for Google App Engine
  • Download the App Engine Python SDK.  The version used here is 1.7.4, on Linux and extract it to your home directory.  In this case, I renamed the folder AppEngine.  If you depart, simply make note of where you extracted the folder.
  • In the App Engine Admin Console, create a new project, Figure 2. 
  • Take note of your application identifier.  You will need that for future steps.
  • Keep the default security settings; we will restrict access to Admin (you) later.

Step 3 - GAE Project Code
  • Download the project source code here.
  • Extract the tar file in your home directory, it should be called IAquaponics_DHT.  Inside you will find the GAE directory labelled myapsystem and the Arduino folder.  The name of the folder containing the GAE code is irrelevant, so there isn't a need to rename it.  The rest of the instructions will keep the original name.  If you choose to rename the folder, amend the directions accordingly.
  • Inside the GAE directory, open app.yaml.  The first line is the application name, so change it to the application identifier you made in Step 2.


Fig 3.  app.yaml.  Replace the highlighted name with your project name.
  • Take note of the bottom.  The main page of our webapp is listed last because it will catch all url requests not listed above it.  In fact, this webapp only has two pages: adacs and main.  The former is used by the Arduino and the latter is the visible interface you will see with the web browser (client).
  • Finally, under main.app, you will see login: admin.  This app uses Google Accounts as you saw in Step 2, but we are restricting application to the webapp to just you.

Step 4 - Launch the AppEngine SDK with Your Project
  • Open a terminal and launch the AppEngine SDK and your project.
python2.7 AppEngine/dev_appserver.py IAquaponics_DHT/myapsystem
  • If all went well, the terminal will tell you the project is located at
http://localhost:8080/
  • So fire up a web browser and point it to that url.  Because we have restricted access to admin, you will need to login as admin.  The login name is irrelevant, but beneath the email input box you need to check the box that says "Sign in as Administrator".  Then login.
Fig 4.  Login as Administrator.


  • At this point you should see two gauges that read zero, see Fig 5.

Fig 5.  Initial Gauges.

  • Next, we are going to replicate the request the Arduino will make to the live webapp.  In the browser, type in:
http://localhost:8080/adacs/dht?Temp=69.1&RH=24.8
  • You will be presented with a page that says "Ok".  Go Back to the main screen and you should be presented with the two updated gauges, like Fig 6.
Fig 6.  Update gauge display.
  • That's how this works.  The Arduino will make requests to the webapp, sending data.  The webapp will save that data in the datastore (its database).  The JavaScript will make AJAX requests (asynchronous JavaScript requests) to the datastore, get the values, and assign them to the gauges giving you a constantly updated display.

Step 5 - It's Alive: Upload the Webapp to App Engine
  • If everything has worked for you so far, then it's time to upload the webapp to a live server.  Open a terminal and type
cd AppEngine/
./appcfg.py update ~/IAquaponics_DHT/myapsystem
  • You will likely be prompted to input your login email and password.
  • When the update is done, go to your webapp.
http://myapsystem.appspot.com
  • If you are not logged into your Google account, you'll be prompted to.  Note the login is consistent for all of Google's products.  At the home page, you should see two zero gauges, just like the SDK.
  • To test the live webapp, repeat the replicated call made by the Arduino and return back to the home screen.  You should be presented with two updated gauges.
Step 6 - Arduino
  • Wire up your breadboard following the Fritzing diagram in Fig 7.  The Ethernet shield is not shown,  but it resides on top of the Arduino and the jumper wires plug into the Ethernet shield.  The 470 ohm resistor is for the bicolor LED and the 4.7k ohm resistor is for the DHT.
Fig 7.  Fritzing layout of breadboard.
  • With that done, load the Arduino file DHT.ino from the project folder.
  • You will need to amend the Arduino code to send requests to your webapp.  There are three locations where the webapp link is listed.  Change ONLY the highlighted project name.
Fig 8.  First location.
Fig 9.  Second location.

Fig 10.  Third location.

  • Save and upload to your Arduino.
  • Open a Serial monitor
  • When the Arduino connects, the bicolor LED will turn green.  If there is a connection issue, the Serial output will tell you and the LED will turn red (See Note 1).
  • If it's not still open, launch a web browser and point it to your live webapp.  You should see the gauges change.  After that, depending on how much your temperature fluctuates, you may not see a noticeable change.
  • And that's it.

Notes
  1. Since working with AppEngine and Arduino Ethernet we have encountered one consistent error.  The Arduino will fail to make the third request.  The first two are fine.  Every request after the third connects and works, but for some reason the third one will fail, every time.
  2. For troubleshooting and development mode, Serial output is on.  Specifically, there are two counters provided, which need to be commented out (using double slashes at the beginning of the line: //).  If you don't, the counter can grow very, very large.
  3. There is a flaw in this code.  If the Arduino fails to update AppEngine, your display will not be changed and you won't know it.  Because this is a bare-bones application, our solution wasn't included.  The solution we developed was to save each timestamp of the Arduino request in AppEngine and create a deferred task set to delay for five minutes.  At the end of the delay, the current timestamp was compared to the last timestamp of the Arduino.  If the delay exceeded a certain time interval (such as three minutes), it meant the Arduino hadn't communicated in five minutes (obviously).  At that point, an email or text alert was sent to the owner notifying you of a connection issue and the gauge values were reset to zero.  
You are free to modify the code as you see fit, but I have one suggestion.  Google App Engine allows for daily free quotas, if you exceed the free quotas and don't pay a monthly fee ($9), your app will be shut down.  Two free quotas you should be aware of are instance hours and read/write/small operations on the datastore.  You get 28 free instance hours, reset daily.  The Arduino should not be set to send requests to AppEngine too often, or new instances of your app may spawn.  Similarly, the browser is set to refresh every 30 seconds.  If you combine the Arduino requests with the client requests, and make them too frequently,  you may again spawn new instances.

Read/write operations are set to 50K each and AppEngine is a little funny about how they count these, but needless to say, making a request every ten seconds, all day from both the browser and Arduino will run up a lot of operations.  Try not to go below twenty seconds on both the client and Arduino and enjoy the free quota.

If you have trouble, hit up the comments below.  And if you include this in one of your own projects, send us a pic, we'd love to see it.

More projects like this can be found on the Automating Aquaponics with Arduino page.

Related:   
EnvDAQ with Water Temperature Sensor 
Real-Time Graphing Online
Online Relay Control

Testing Proto Shields for Automating Aquaponics with Arduino

For Maker's who already have a proto shield such as the official shield, Adafruit's, or Make's, we thought we'd test these boards with the Arduino Data Acquisition and Control System design.


The tiny breadboard seen here is from Adafruit and was used to layout the parts specifically for Adafruit's shield, which is more of a printed breadboard design - if it could fit on the breadboard, it could fit on the shield.  The empty side of the breadboard is where the other sensors and indicators belong. 

With Adafruit's shield we ran into a clearance issue between the proto shield and the Arduino ethernet shield.  The ethernet jack is 0.5" tall and Adafruit's shield kit does not come with stackable header pins - the pins it does comes with are not long enough to make the connection when stacked on the ethernet shield.  Fortunately, we roughed the proto shield parts into place with the Uno and ethernet shield before soldering the kit together, saving us from desoldering the proto shield to add stackable pins. 

The official Arduino proto shield does not have this clearance issue and we'll test the Make shield later (since it comes with stackable header pins, we can't immediately see it having any issues).  All three shields are capable of being placed between the Uno and the Ethernet shield making the clearance seemingly irrelevant.  However, if you chose to solder the DHT sensor directly into the proto shield (flat, unlike the picture), and then sandwich the shield between the Uno and Ethernet, your temperature and humidity readings could become skewed by the ambient heat generated from the other shields.  This means you would have to use extension wires to move the sensor to the enclosure and if you don't hard solder the extension cables in, instead choosing to use removable connectors, you might be presented with clearance issues all over again.  In the end, simply adding stackable header pins was the easier option.

The Adafruit's shield is very simple to put together and barring the (easy to resolve) header issue, worked well.  Because the Adafruit's proto shield is similar to a breadboard it is more intuitive to setup a circuit design from the breadboard vs. the Make or Arduino proto shields, which we'll discuss in more detail later.

------------
Using Arduino with Aquaponics is covered in our upcoming book Automating Aquaponics with Arduino and companion kit.

Water Quality Tracking Spreadsheet

Attached is a Google Docs Spreadsheet for keeping track of water quality tests.  The data columns have conditional formatting for parameter ranges.  The graphs are on the far right.

If you try exporting to convert to OpenOffice, we have found the format does not seem to include the graphs if you import it into LibreOffice.  And we haven't tried exporting to the Excel format as we don't have Windows to test it on.

Water Quality Tracker

To make a copy for yourself:
File > Make a Copy

To add a system:
Right Click 'System 1' > Duplicate > Rename

Automating Aquaponics with Arduino - Coming Soon

Coming soon, we are publishing a project book on automating aquaponics using the Arduino microcontroller.  Want to build your own automated data acquisition and control system for your aquaponics setup?  It's easier than you may think.  The first in a series, this do-it-yourself guide shows you how to use the Arduino to monitor and control electronic devices and...more

Grow Lights and Automation

Fig 1.  Chlorophyll.

The Vertical Farm (TVF) is an implementation of Controlled Environment Agriculture (CEA), employing technology that enables the manipulation of the environment to meet a crop's optimum growing conditions.  Greenhouses, aquaculture, hydroponics and aquaponics are examples of CEA, where temperature, lighting, heating, pH, humidity and nutrient analysis are all tightly controlled.  Every plant has a natural tolerance range for each of these variables, but it is light that plants depend on most, keeping all other variables within their extrema.  It is also one the more intense areas of research in CEA.  For commercial operations and home growers alike, lighting is the key to a consistent yield - the determining factor to retaining customers and remaining in business. 

To understand the importance of light, we need a quick biology and physics lesson.  Inside each leaf of a plant are hundreds of thousands of microscopic organelles called chloroplast, whose function, among other things, is to conduct photosynthesis.  The green pigment found in a plant leaf is called chlorophyll and it is the job of chlorophyll to capture photons and convert them into energy-storage molecules.  During photosynthesis, the chlorophyll use photons to break down carbon dioxide \(CO_2\), which contains one carbon and two oxygen and water \(H_2O\), two hydrogen and one oxygen.  Carbon dioxide and water are broken down to produce sugar and other plant products like cellulose \((C_{6}H_{10}O_{5})n\), in the process releasing the oxygen in the carbon dioxide molecule back into the atmosphere.  Animals then eat the plants and combine the carbons in the plant sugars with two oxygen atoms we breathe, producing the carbon dioxide \(CO_{2}\) we exhale as waste and adenosine triphosphate (ATP), the chemical energy our bodies use.  The cycle repeats.

If we could keep the amount of light consistent 365 days a year, we would produce a consistent crop, in quality and quantity.  Remember, a commercial greenhouse operation expecting to sell 1500 heads of lettuce a week needs to sell 1500 heads of lettuce, not 1000 and not 2000; excess production is just as much a problem as a lack of production.  If you produce too much you probably don't have enough transplants ready and will have unused space in your grow beds.

Mother nature is rarely cooperative in providing the same intensity of light on a daily basis, so it was only natural for indoor farmers to employ artificial lighting.  If we want to increase the efficiency of lighting for plant growth we start with the observation that chlorophyll is green.  Obvious, but important, because it means chlorophyll does not absorb the entire light spectrum and artificial lighting drawing power to produce green wavelengths is wasting money.

Continuing our research, we find there are two important forms of chlorophyll, chlorophyll a and chlorophyll b, and each absorbs distinct wavelengths of the visible electromagnetic spectrum.  Fig. 2, shows the peak absorption wavelengths.

Fig. 2 - The EM absorption spectrum of chlorophyll.  Image Source: Wikipedia.

You'll notice they absorb mostly in the darker blue and red spectrum.  This is critical because it means the majority of the visible light is useless in the process of photosynthesis.  It also means those nice bright white lights are drawing electricity to output wavelengths that are simply wasted in the growing process, reducing efficiency and increasing costs. 

The parameters for determining optimal lighting are:
  1. The type of plant being grown - intensity
  2. The stage of growth - wavelength
  3. The photoperiod required by the plant - time
The vegetative phase requires the blue spectra while fruiting requires the red, or red-orange spectra and typically requires a higher intensity. Seedlings require less intensity and are planted much closer together (an average of 2 inches apart).  This allows one grow light to be used for a large number of seedlings and is therefore more economically viable.  As the plants grow, the single grow light can no longer provide the necessary intensity for the same number of plants, forcing growers to either buy more lights or rely on natural light alone, thus losing control of the schedule.

Not all lights are created equally.  Incandescent bulbs are inexpensive, but have very poor intensity and waste the most electricity in the form of heat.  Fluorescents also lack enough intensity for larger plants or plants in the fruiting stage, but they can be used for plants not requiring too much light, such as herbs.  Newer fluorescents are making big strides in heat loss and increased intensity, but obviously cost more.  High Intensity Discharge (HIDs) lamps are on the high end, both in cost and quality.  They are very bright and are more efficient, but they still output a lot of thermal radiation.  Metal halide bulbs have a blue tint to them and based on your new knowledge of wavelengths you know these are good for the growth stage; they also output a lot of lumens.  While metal halides are good for growth, high pressure sodium lights hit the spectrum for fruiting/flowering with a red-orange tint.  To get the most out of artificial lighting it has become a common practice to put the grow lights very close to the plants to achieve a higher intensity, however, this is dangerous with lights that output too much heat. 

New forms of lighting are being engineered for the express purposes of emitting just the right wavelengths plants need and increasing efficiency by drawing only the energy needed to produce them and preventing the loss of energy to thermal radiation.  LEDs (light emitting diodes) have been around for years, but are relatively new in application to agriculture while OLEDs (organic light emitting diodes) hold the potential to accurately target specific wavelengths.  Another exciting aspect of OLEDs is that they can be "printed" on thin pieces of plastic, allowing for uniquely shaped lights.  Imagine a hollow cylinder that produces light only on the inside, and that can enclose an entire plant.

The production of light is not the only area for improvement.  A second area of research is the high tech plastics and glasses used in greenhouse windows and a key component in a Vertical Farm.  Glazing on the glass can lead to absorption of the wrong spectrum and reduce the overall intensity, both highly undesirable characteristics.  There is active research into windows that allow for higher emissivity, one-way emissivity, heat retention, increased tensile strength, lighter weight, improved weathering and made from recycled plastics.

Cornell University's CEA program has done some important research in the amount of light a plant needs.   They have developed an algorithm to meet optimal lighting with what they call the Daily Light Integral.  By using a combination of artificial lighting on cloudy days, natural light and active shading on sunny days, they can hit a target DLI over the course of three days and therefore keep a consistent crop rotation schedule. For commercial operations and self sufficient families who depended on their aquaponics systems, consistency is key.  Exceeding the photoperiod for a plant is a waste of money and can cause physiological symptoms.  Plants producing fruits (or flowers) need about 16 hours a day of solid light, while non-fruiting plants are around 10 to 12 hours.  Don't forget: plants need their "sleep" too.

Fig 3.  Using a LDR input and a relay to control grow lights.

Automating aquaponic grow lights for systems utilizing a combination of daytime and artificial lighting can be done using a light dependent resistor (LDR) and a 120V @15A relay, as seen in Fig 3.  In this case, the LDR output is monitored until the resistance produced meets a threshold you define through calibration (indicating a decreasing natural light intensity), triggering the relay and turning on the grow light(s).  This setup is much more adaptive than a fixed timer, allowing for changing weather patterns and lengths of the day throughout the year.  A more complex version can replace the LDR with a photometer to more accurately analyze the natural light for the specific wavelengths you need.  An extension of this project is to use a second photoresistor to monitor the grow light and alert you if the light goes out unexpectedly and is covered in depth in the upcoming book, Automating Aquaponics with Arduino.  While this addresses the photoperiod, it says nothing of the wavelengths necessary for each stage of plant growth.  Some home aquaponics and hydroponics growers are experimenting with using matrices of off-the-shelf red and blue LEDs as artificial lighting.

To evaluate the electrical costs of artificial lighting per month:
  1. Add up the combined wattage of all grow lights
  2. Divide (1) by 1000 to get total kW (kilowatts)
  3. Multiply (2) by the kW/hour rate your your utility company charges
  4. Multiply (3) by the number of hours your lights are on in one month
As an example, lets say you have one, indoor, 4 x 4 foot grow bed.  A 400W HID lamp could cover this area with the right light intensity.  The average winter rate of my electric company is 9.66 cents per kWh and assuming I am running my grow lights for 16 hours per day for 31 days, I have 496 hours per month.

\((400W) * (\frac{1E-3kW}{W}) * (\frac{$.0966}{kWh}) * (\frac{496h}{month}) = $47.9/month\)

Compared to conventional agriculture, which is entirely dependent on oil, aquaponics runs off of electricity and therefore can be powered by alternative energies such as wind, photovoltaic and hydro-electric.  For those who run their systems "on the grid" and then lose power when the grid goes down, it is very simple to incorporate a backup diesel/gas electric generator.  There are no backups for conventional agriculture.

According to Cornell University's CEA, "...the KEY to acquiring and retaining long-term customers for a commercial production facility involves ONE simple concept:  Produce a consistent, high-quality crop at a predictable and steady rate 365 days per year....More than any other environmental variable, the total sum of light received by the plants in a 24-hour period determines the rate that the plants will grow and thus the amount of produce available for sale."

The success of the Vertical Farm will inevitably be evaluated on its commercial returns and rightly so.  For those employing aquaponics at home (kudos to you), you can take advantage of this biology and physics knowledge to enhance your own systems for consistent output and also evaluate any physiological symptoms of your plants for evidence of poor lighting conditions.

--------------------------
If you would like to learn more about automating your aquaponics system, sign-up for our new book Automating Aquaponics with Arduino.