Showing posts with label Hydroponics. Show all posts
Showing posts with label Hydroponics. Show all posts

Aquaponics: Arduino Email & Text Messaging

Background
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
Step 3:  App Engine - models.py
  • 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
    1. Time zone offset - not used in this tutorial
    2. Email Address - string property
    3. Text Address - string property
Fig 2.  models.py
 Step 4:  App Engine - main.py and Templates
  • 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
  • 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.
Step 7:  App Engine - alerts.py
  • 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
Step 8: Arduino - Fritzing Diagram
  • 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.

The Vertical Farm

Eco-Laboratory by Weber Thompson.
The first time I came across the Vertical Farm concept, it was in an issue of Popular Science.  A large pullout showed an awe-inspiring skyscraper with a white-bearded man posing in front, cupping an ear of corn and staring off in the distance.  The Future of Farming.

Upon hearing of aquaponics I was immediately reminded of that pullout and dug out my old PopSci issue.  A few days later I checked out, "The Vertical Farm," by Dr. Dickson Despommier, from my local library.

"The Vertical Farm" is a two part book.  We start off looking at the past and current state of agriculture, coming to the conclusion it is failing to provide nutritious foods, it is insufficient to sustain us in the future and the ecological destruction caused my modern farming methods has taken an incredible toll on the planet.

Dr. Despommier's solution is to utilize skyscrapers to grow food hydroponically in the middle of cities.  According to Dr. Despommier, the vertical farm has a number of advantages over traditional agricultural methods.  In the remainder of this article I discuss his proposed advantages and in my next article I will argue aquaponics not only has these benefits, but it takes them to a whole new level and then adds more.

"The Living Skyscraper:
Farming the Urban Skyline"
by Blake Kurasek
1.  Year-round crop production.  By growing crops inside controlled environments, crop production is not dependent upon the seasons.  Instead of one season of tomatoes, staggered planted tomatoes can be harvested year round.

Related:  CEA and grow lights

2.  No Weather-related Crop Failures.  As I write this, the corn crop in Iowa is in trouble.  There has been very little rain this season and the crops have suffered accordingly.  By growing foods in controlled environments, droughts, floods, hurricanes, tornadoes and other natural phenomena are irrelevant (assuming the vertical farm survived the tornado).  The world will feel the effects of the drought in corn country since products from Coca Cola to Starbucks, ethanol to beef all contain corn products and the increased price per bushel of corn is going to permeate the global economy.

3.  No Agricultural Runoff.  "According to the USDA, Agricultural nonpoint source pollution is the primary cause of pollution in the U.S." - The Vertical Farm.  Soil is composed of pesticides, herbicides, fungicides, rock, etc. and when it rains the runoff inevitably ends up in streams and heads down river.  By growing hydroponically in a controlled environment, these toxic chemicals are unnecessary.

4.  Allowance for Ecosystem Restoration.  The first part of The Vertical Farm talks about the current state of the ecosystem, specifically how poor a state it is in.  By moving agriculture into city skyscrapers, Dr. Despommier argues traditional agriculture won't be necessary and the land can be turned back over to nature to recover.

5.  No Use of Pesticides, Herbicides, or Fertilizers.  Guess what?  There are no weeds to pull in hydroponic/aeroponic/aquaponic systems!  Dr. Despommier argues hydroponics is a superior method of growing foods; by adding only the nutrients the plants need to the water the plants live in, a balance is achieved.  The plants absorb all the nutrients and clean the water.  The nitty-gritty details on this point are really where aquaponics and hydroponics shine as an alternative to traditional agriculture.  But I disagree with Dr. Despommier on this point and believe aquaponics is a far superior method to hydroponics.

6.  Use of 70-95 percent less water.  According to The Vertical Farm, "Today, traditional agriculture uses around 70 percent of all the available freshwater on earth, and in doing so pollutes it, rendering it unusable for those living downstream." Water is by far the most precious resource on Earth. The pollution of our water by traditional agriculture is the primary need to alter our farming methods. Of all the benefits on this list, this is the most important.

7. Greatly reduced food miles. A common argument for alternative farming is that the average distance food travels from farm to table is 1500 miles. That is a staggering number! The Vertical Farm proposes growing food in the center of cities, drastically cutting this distance down. I propose an even greater reduction in food miles by growing food at your home!

8. More Control of Food Safety and Security. The Vertical Farm is designed using the same equipment hospitals use in intensive care units to prevent pathogens and pests from affecting the crops. Security is proposed to prevent people from sabotaging the environment.


Artist's rendition of a
desertVertical Farm
9. New Employment Opportunities. When farming exists in a skyscraper there are plenty of job opportunities. The traditional farmer goes through stages: they buy their seeds, tills, fertilizes, plants, maintains, harvests and sells. In the Vertical Farm, the crops exist in all of these stages all the time, necessitating the need for labor.

10. Purification of Grey Water to Drinking Water. Grey water reclamation will become an even hotter topic as the scarcity of clean water becomes more desperate. Plants provide a natural (bio) filtration process that can clean toxins out of the water. Clean water from plant transpiration can be recovered using dehumidifiers. While you can't eat the plants, Dr. Despommier argues the plants can be incinerated using plasma arc gasification to create energy.

11. Animal Feed from Post-harvest Plant Material. You don't eat all parts of a plant and what is leftover can be used as animal feed. I argue you can take this one step further and suggest you could also compost this material for seedling beds.

Wow, those are some good arguments!  

Overall, I thoroughly enjoyed reading The Vertical Farm. But there are problems here. Firstly, and even Dr. Despommier freely admits, you need to suspend your belief any government will sponsor a vertical farm. States are so strapped for cash, projects like this, on the scale proposed, are pipe dreams. I find it hard to believe a project like this will ever be subsidized, a need the author believes is necessary.  

Second, if the project did get off the ground, the costs are staggering to the point the product may not be affordable. The price drives consumer choice. Yes, there are niche demographics that are willing to pay a premium for a premium product, like customers of farmer markets or Whole Foods. I must say, I feel different now shopping at a store like Whole Foods. I might buy avocados grown in a sustainable way, but they are grown in Mexico. Is it really better to buy foods grown so far away? Do the benefits offset the the food miles? I don't think so. Enter the birth of groups like The Hundred Mile Diet.

The Vertical Farm - Dr. Dickson DespommierWhen I finished The Vertical Farm I admit I was disappointed. Like Food, Inc. I was left with the question, "What can I do now?". I want to buy foods that are better for my family, better for the environment, better for society. How am I supposed to source locally grown organic tomatoes in January?

But there is an even better method than the vertical farm, a more sustainable method, that takes these advantages to a whole new level and I will show you what you can do right now.






Related:  Grow Lights and Automation

----------

Need help monitoring your aquaponics system?  Check out Aquaponics Tracker, an Android app for keeping tabs on the key parameters that keep your system healthy.