Introduction: Aquaponics: Online Temperature and Humidity

About: It's pronounced "Iowa Aquaponics".

For other projects, see our main site: www.iowa-aquaponics.com/arduino/

**Update**
You can find a live demo of the online temperature and humidity in action here:
http://adacsprojects.appspot.com
**

While this project is designed with aquaponics in mind, it does not require an aquaponic system, making it useful for other projects such as home automation. The included application is, therefore, bare-bones, making it easier to integrate into any other application project. The web application provided here runs on Google App Engine, a beautiful cloud platform from Google. GAE provides a free quota for read/write operations, emails, and instances. For a full list, see their documentation.

In this project we focus on capturing temperature and relative humidity and sending it to GAE 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 an updated display.

Step 1: Download Code

Hardware:
1. Arduino Uno R3
2. Ethernet Shield R3
3. 1 Bicolor LED
4. 1 470 Ohm resistor
5. 1 DHT-22 Sensor
6. 1 4.7k ohm resistor
7. Breadboard jumper wires
8. Breadboard
9. Arduino power cord (Gives more consistent power to your Arduino than USB)
10. USB-AB cable for uploading Arduino code & serial output


Software Requirements:
1. Arduino IDE: 1.0.3
2. Google App Engine SDK: Python, Linux 1.7.4
3. Python: 2.7
4. Ubuntu 12.04

This application was developed on Ubuntu 12.04. I don't have a Windows machine, so I can't tell you how to install Google App Engine or any other software on it.

The application files for this project are:

A. Simon Monk's Timer library
B. Adafruit's DHT library
C. Iowa Aquaponics' DHT Arduino Code
D. Iowa Aquaponics' Google App Engine Code

1. Download Simon Monk's Timer library here. Extract the zip module and put a copy in your arduino-1.0.3/libraries directory. Make sure it is named "Timer".
2. Download Adafruit's DHT library here. Extract the zip file and rename the extracted folder DHT. Make a copy and place it in your arduino-1.0.3/libraries.
3. Download Iowa Aquaponics' zip file here. Extract it into your home directory.

Step 2: Google App Engine

This project uses Google App Engine, a scalable application platform hosted on Google's cloud infrastructure.  The beauty of GAE is the developer can focus on webapp production and leave the nitty gritty of server development and maintenance to Google.

I developed this project using the GAE SDK for Python and Linux 1.7.4, which can be found here.  Download and extract.  For convenience I extracted it in my main directory as AppEngine.

If you don't already have a Google account, time to sign up for one.  After you do, you'll need an App Engine Developer's Account, which you can sign up for here with your Google account.

When you have your new account, you can sign-in to your applications dashboard

https://appengine.google.com

The next step is to create a new application.  Notice the final url will be yourapp.appspot.com.  

For security, you'll have three options.  Select the first one: "Open to all Google Accounts users (default)".  This application has one page and it is restricted to only your email account.  We'll talk about that shortly.  After you have an application identifier, finish creating your new app.



Step 3: Testing GAE SDK

With your new application identifier, it's time to customize the code from Iowa Aquaponics.  First, change directories into

$ cd IAquaponics_DHT/myapsystem

Open "app.yaml".  The first line is the application name.  You need to change this to your new application identifier.  The folder name is irrelevant.  The first line in "app.yaml" is the only line that needs to change.  Looking at the bottom of the "app.yaml" file, you will see the "main.app" has login set to admin.  The main website will require you to login and will only accept your Google email.  If you want to allow anyone with a Google email to sign in, set login to "required".  If you don't want any login at all, to make it open to anyone to view the page, remove the line altogether.

A final note on security.  The main website is protected by Google (and is as weak as your password), but the adacs links, made by the Arduino, are not.  It is certainly possible to include password protection with the Arduino, but it is outside the scope of this project.

For the rest of the commands from the terminal I will assume your GAE directory tree looks something like this

/home/you/AppEngine/

And your directory tree from Iowa Aquaponics looks like this

/home/you/IAquaponics_DHT/

Open a terminal and type

$ python2.7 AppEngine/dev_appserver.py IAquaponics_DHT/myapsystem

If it works, you'll be notified at the bottom of the terminal that your application is running on port 8080:

http://localhost:8080

So fire up a web browser and go to that url.

You should be presented with a login screen.  Ignore the email address, check the "Sign in as Administrator" and login.

At this point you should be presented with emptiest web page you've probably ever seen (that wasn't completely blank).  There should be two gauges sitting there, both with values at zero.  

To test that the SDK is working properly, we are going to replicate the GET request the Arduino will make.  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".  That's good, so go back to the main local host page

http://localhost:8080/

You should see the gauges change to reflect the new values.

Step 4: Going Live

If everything has gone well to this point, it's time to put the webapp in the cloud.  In a terminal, change directories into your App Engine directory.  Then type

./appcfg.py update ~/IAquaponics_DHT/myapsystem       ** Notice the dot-slash to start this off  ./    It's an executable script.**

Again, substitute your path where necessary.  The terminal will show the updates as the app is uploaded and you will probably have to supply your email and password (but you don't have to every time you update your app).  When the update is done, fire up your web browser and point to your full webapp url.

http://myapsystem.appspot.com

If you are not logged into your Google account and you kept the login: admin, you will be prompted to login.  When you have, you should see the same thing you saw in the SDK, two gauges.  Go ahead and repeat the fake Arduino call to the datastore.

http://myapsystem.appspot.com/adacs/dht?Temp=79&RH=60

Go back to the main screen and you should see the gauges change position.

Step 5: Breadboard the Circuit

Using the Fritzing diagram, breadboard up your circuit.

The pins in the diagram are specific to the Arduino code.  If you change the pins, remember to change the code in the next section.

The bicolor LED is used to tell you if the Arduino is connected to the webapp.  If you haven't used on before, the pins are opposite, allowing electricity to flow both directions.  When the Arduino is connected to the web, pin 9 is LOW and pin 8 is HIGH.  This will produce a green color.  When the client is disconnected, the pins are reversed and the led glows red.

Every sixty seconds, the Arduino will test its connection to the webapp.  If it connects, the LED is green and the Arduino will read from the DHT22.   The temperature is converted to Fahrenheit and the temperature and relative humidity are sent in a GET request to GAE.

The Arduino code needs to be pointed at your application identifier, so update the code in the three places highlighted in the pictures.  Take note, only change the highlighted part of the url.

Finally, select the Arduino Uno board from the Tools menu, assign the right serial port, then upload the code,  open a serial monitor, and wait.  The Arduino isn't set to read for 60 seconds.

Step 6: Final Notes

There are two final notes to be made.  One of the nice things of Google App Engine is that you get a free daily quota and we take advantage of that here.  Specifically, you are given 50,000 Read/Write operations per day and 28 instance hours.  The Arduino is set to make requests every sixty seconds to keep the servers from spooling up more than one instance.  The client (web browser) makes AJAX requests to App Engine every 45 seconds, to keep the screen updated with accurate readings.  If you choose to lower these down (Arduino requests every 30 seconds and AJAX every 15 seconds), you should still fit inside your daily quota, but monitor it to be sure, especially if you are running both the Arduino and a client all day.

Secondly.  This code is not perfect.  In fact, it is a very small part of a much larger whole.  The web application here is bare bones to make it as easy as possible to assimilate it into your own (even pre-existing) application.  As such it has one large flaw that was purposely omitted.  If the Arduino were to lose contact with the web server, the web server won't know it (push vs. pull).  The client will simply be getting the same data over and over.  

The work around was to record the timestamp when the Arduino sent data.  In addition to the recording the request timestamp, the application also creates a deferred task and adds it to the queue.  The task is deferred for five minutes and then executes.  It analyzes the current timestamp of the task and compares it to the last timestamp made by the Arduino.  If that time difference is greater than two minutes, we presume the Arduino has lost contact and notify the user via email, a visual icon (not included) was changed and the temperature and humidity values were reset to zero.  But as you'll add this code to your own projects we figured you may have your own alert system in place, so we didn't include it.

If you are interested in expanding your automation and control check out our upcoming book Automating Aquaponics with Arduino where this project, and others, are developed in depth.  We'll not only monitor the environment, but we'll also automate a pump and grow light connected to relays, controlled remotely via a full app on App Engine.