With all of the lauded benefits of aquaponics, there are a number of drawbacks and they primarily lie in the need for electricity. Typically there is at least one pump, but if you also use grow lights, water heaters and other electronics, you'll inevitably increase possible points of failure. Wouldn't it be nice if you knew right away when pumps fail, grow lights burn out or don't come on, water heaters die, water temp plummets, a grow bed or fish tank springs a leak or overflows, humidity gets too high/low or the summer sun roasts your greenhouse?
Automation can immediately act to correct some of these conditions but sometimes you don't have a backup pump, grow light or stock tank heater. Leaks, especially in basement systems, can ruin property if it's not addressed quickly.
What you want is a way to know what's going on as it happens and a very good way of doing that is through text message or email. That's what this guide is all about and it's not unique to aquaponics or hydroponics - it can be used any time the Arduino needs to alert you of a sensor reading.
The Arduino Data Acquisition and Control System (ADACS) projects in Automating Aquaponics with Arduino use the mail API to notify owners of failed/blocked pumps, when environmental conditions move outside of user-defined ranges, and when grow lights fail or don't turn on. Additionally, you'll be notified when the Arduino has failed to connect to the webapp for a set period of time to indicate a potential power loss or loss of an internet connection. Knowledge of what is happening in your system is the most powerful tool of all and when you combine the Arduino in your aquaponics or hydroponics system, you get an inexpensive means of acquiring that knowledge.
How It Works
Like all of our web-based projects we'll use Google's cloud infrastructure, App Engine. We'll set up an Arduino Uno with two push buttons, which, when pressed, will send a web request to App Engine and prompt either an email or a text message. The client consists of a very basic form to collect the designated email address and text address.
Hardware
1 x Arduino Uno R3
1 x Arduino Ethernet Shield R3
2 x Momentary button or switch
2 x 10k ohm resistor
1 x Small breadboard
Breadboard jumper wires
Software
Arduino-1.0.3 IDE
Google App Engine Python SDK: 1.7.4
Python 2.7
Ubuntu 12.04*
*These instructions are probably very easy to translate for Windows or Mac, but as we have neither I can't help you there.
Step 1: Create a New App Engine Application
The first step will be to create a new application on App Engine. As all of our web application use App Engine, we've created a standalone tutorial to cover it: Creating a New Application.
Step 2: App Engine - Configure app.yaml
- The email alerts reside in a file called alerts.py. We need to add that file to our app.yaml file, Fig 1.
Fig 1. Add alerts to app.yaml |
- In the typical MVC (Model, View, Controller) pattern, we need to create a model of our data, which resides in models.py and is imported to the files that need to access it.
- We have one class to create, the UserPrefs class, which has three properties
- Time zone offset - not used in this tutorial
- Email Address - string property
- Text Address - string property
Fig 2. models.py |
- Open main.py and add the import statements, Fig 3.
Fig 3. main.py import statements |
- The first three imports are used to display the templates and run the application. The users import is used to get the email address of you (the email address you used to create the application).
- models is used is the file containing the UserPrefs data, which we covered in step 3.
- main.py consists of just two classes: MainPage and SaveAddress. You can see MainPage in Fig 4.
Fig 4. MainPage class. |
- The title of the page is sent to the header template, which you can see in Fig 5. This is how data can be passed into generic templates, reducing redundant programming.
Fig 5. The first part of header.html |
- In order to send emails or text messages you'll need to provide an address, so we add a very simple form to header.html. You can see the complete file in Fig 6. The form consists of two text fields and two buttons. Each button is given a JavaScript onclick handler discussed in the next step.
Fig 6. Full header.html file |
- footer.html is a truly uninspiring file and only consists of closing tags
</body>
</html>
Step 5: Email & Text Messages
A word on email addresses. The email address you put as the sender must be an email address associated with the account you used to make the webapp. You can also invite other Gmail addresses to be admins. The email address you are sending to can be any valid email address.
Text messages can be sent through an email client such as
Gmail given you format the address correctly. Here is a link on the different formats carriers use for SMS and MMS. We have only confirmed the Sprint format so I will use that as the example.
Sprint's SMS email format looks like this:
<your phone number>@messaging.sprintpcs.com
Example:
1234567890@messaging.sprintpcs.com
A Sprint user would substitute their phone number in and use this for the text message box.
Step 6: JavaScript and Form Processing
Step 8: Arduino - Fritzing Diagram
Step 6: JavaScript and Form Processing
- With our template and form in place, we will use JavaScript to send an asynchronous request to the server to process the form. I've separated the JavaScript into two separate files. The first is utils.js, Fig 7, and contains the function to create a request. Looking back at Fig 6, you'll see utils.js is loaded before our second JavaScript file, main.js.
Fig 7 - utils.js |
- Next, main.js contains the two onclickhandlers we referenced in the form. I created them separately for readability in this tutorial, we could have easily created a single button to do this work. When a save button is clicked, a series of events takes place and you can read that in the comments.
Fig 8. main.js |
- The webapp code to process the JavaScript requests is the second class in main.py, SaveAddress.
Fig 9. SaveAddress in main.py |
- The JavaScript request is parsed and the UserPrefs entity with the email address you used to create the app used as the key_name. If the entity exists, you will update the emailAddress or textAddress properties, otherwise a new entity is created. Finally, the entity is put into the datastore and a response is made.
- alerts.py has one class, Alert, used to handle the Arduino request. There is also one function, SendAlert used to send an email or text message. First we list our imports
Fig 10. alerts.py import statements |
- The Alert request handler parses the request from the Arduino to determine the type of alert you want, email or text, Fig 11.
Fig 11. Alert request handler |
- The request handler for the Arduino parses the request to find out which type of alert you want, text or email. Then it calls the SendAlert function passing in the type of alert and a message. This makes the alert function generic and reusable.
Fig 12. SendAlert |
- The circuit for this tutorial is very straight forward and consists of two push buttons. One push button is used to request an email, the other a text alert.
Fig 13. Fritzing Diagram |
- In Fig 13 you see a tiny breadboad sitting on an Arduino Uno R3. In reality, the breadboard resides on an Ethernet Shield, or better yet, a proto shield on an ethernet shield on an Arduino R3.
Step 9: Arduino - Code
- Start by setting up the Arduino with your assignments
Fig 14. Arduino assignments and imports |
- The setup loop begins the serial output and the Ethernet. It sets the pin mode for the pushbuttons to INPUT and finally notifies the user that setup is complete.
Fig 15. Arduino setup loop. |
- The operating loop monitors digital pins two and three to see if the push buttons have been pressed. If they have, we call the sendAlert function with the type of alert we want.
Fig 16. Operating loop |
- sendAlert outputs via serial the type of alert being requested. Then it creates a GET request to the Arduino and finally outputs the response to the screen.
Fig 17. sendAlert Arduino function |
- The final two functions in the Arduino program are our normal response parsing and httpRequest functions.
Fig 18. HTTP response parsing and request functions |
And there you have it - two buttons to send either a text message or an email. While the button format may seem useless think about this - the Arduino read the state of a pin before making the request and the GET request was generalized to allow either button to request the same function.
By extension, the Arduino can read the state of any input pin, analyze the reading via conditionals and send an alert if it needs to. The Arduino Aquaponics code in the ADACS is rather large, so rather than apply conditionals on the Arduino, the values were automatically sent to App Engine and App Engine applied the conditionals to decide if an alert was needed. This freed up valuable programming space and extended the number of conditionals that could be applied.
Tell us what you think in the comments below.
Related Tutorials:
EnvDAQ with Water Temperature Sensor
Online Temperature and Humidity
Real-Time Graphing
Online Relay Control
Related Tutorials:
EnvDAQ with Water Temperature Sensor
Online Temperature and Humidity
Real-Time Graphing
Online Relay Control