Introduction: Aquaponics: Arduino Email & Text Messaging
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) uses 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.
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
Google App Engine Python SDK: 1.7.4
*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 Application on App Engine
Step 2: 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.
Step 3: App Engine: Models.py
The model of the data resides in 'models.py'.
The only class is 'UserPrefs' which has three properties
1. Time zone offset - unused for this project
2. Email address - saves the email address as a string property
3. Text address - saves the text message address as a string property
Step 4: App Engine: Main.py and Templates
'main.py' has two classes, 'MainPage' and 'SaveAddress'.
To begin add the import statements to the top of 'main.py' - Fig 1.
The first three imports are common to request handlers. The users API captures your admin email which is used as the key name for querying the UserPrefs entity. models is the file from Step 2 and contains the data model.
In order to send emails and texts you'll need to provide addresses, so we add a simple form below the header. There are two text boxes and two buttons with onclickhandler references. The full header file is Fig 4.
The footer template is truly unremarkable and consists of the two closing tags:
The utils.js file is shown in Fig 1. It contains one function to create a request.
Main.js contains the two functions referenced by the onclickhandlers in the form.
Finally, Fig 4 shows the end of main.py
Step 6: Sending Text Messaging From Email
Most major carriers have public addresses for sending texts through email clients such as Gmail. You can see a list here of how to format the addresses to send SMS and MMS. I have only confirmed Sprint's format, so I will use that as an example.
SMS format for Sprint is:
<your phone number>@messaging.sprintpcs.com
So, an example would be:
This is the format a Sprint user would put into the text message box in the client.
A last note is to keep your SMS short. Some carriers will split long messages into multiple SMS messages, while others will outright deny an SMS exceeding 160 characters. You'll have to test your carrier to see.
Step 7: App Engine: Alerts.py
alerts.py has one class and one function. The class, Alert, is a request handler for the Arduino requests. The function, SendAlert, is a generic function to send either a text or email alert.
Again, we'll start off with the imports - Fig 1. Even though the users API is listed again, it's not used here.
Fig 2 shows the request handler for the Arduino requests. The Arduino will request a type of alert and the SendAlert will be called, passing in the alerty type and a message. The nice part of passing these in is the SendAlert function stays generic and can be reused by other functions (if you expand the application).
The SendAlert function is seen in Fig 3 and requires a few special notes. First, App Engine cannot tell who you are when the Arduino makes its request, so our previous method of using the users API to get your email address and use it to query for the UserPrefs entity won't work here. As such, I've hard coded the user email. You could pass it in with the Arduino request, but for brevity, I've chosen not to do that.
Second is the mail API. The sender email address must be listed as one of the administrators of the webapp. You created the webapp with one email address, but you can add others with Administrative privileges in you Admin control panel. If you try to use an address that isn't registered you will get an unauthorized sender error.
Finally, in Fig 4, you can see the bottom of alerts.py
Step 8: Arduino - Fritzing Diagram
The circuit for this instructable consists of two pushbuttons; one creates a request for an email and the other a text message.
The Fritzing diagram shows a tiny breadboard sitting on an Arduino Uno R3. In reality, the breadboard should reside on a proto shield on an Ethernet Shield on the Arduino, but I always seem to forget that part.
Step 9: Arduino - Code
The Arduino code starts with the assignments in Fig 1. There are push buttons in pins 2 and 3. Then we set up the Ethernet Shield and select the webapp server. You'll need to change this to the app identifier you made.
The setup function starts the Ethernet shield and sets the pin mode for the push buttons. Finally, it notifies you over serial the setup is complete.
Fig 3 shows the main loop and simply looks for a high value in either pin 2 or 3.
A high value causes the sendAlert function to be called with the the string of the alert type you want. You'll get notified over serial which button you pressed and what alert you are requesting.
The response from App Engine is parsed and displayed.
Step 10: Final Thoughts
And that's it - two buttons to send either a text or email. While the button format may not seem useful, you've probably already realized simple conditionals analyzing sensor values can prompt alert requests.
When we created the Arduino Data Acquisition and Control System for our upcoming book, Automating Aquaponics with Arduino, we jammed a lot of programming into the Arduino. Instead of applying data analysis in the ~32Kb of space we have, we simply uploaded the values to the webapp and put the conditionals in App Engine. This freed up quite a bit of valuable programming space.
Finally, note that App Engine only allows 100 free emails a day. It's a penny for each email after that.
If you see errors, post a comment and we'll fix it sharpish.
We have a be nice policy.
Please be positive and constructive.