Arduino Aquaponics: JSON

**UPDATE**
To see JSON, have a look at the Pump Controller project.

In a previous post, Online Relay Control, we demonstrated how to manually trigger a relay from a web application.  The Arduino polled App Engine on a set interval and received the status of the relay as a string; the Arduino sketch parsed the response and then analyzed the response using "if" conditionals.

While that works well for setups involving a single relay, we realize it is inherently limiting, particularly for those who want to control two relays with their Environment DAQ.  Similarly, we wanted to pair the Depth Sensor with two pumps that could be controlled independently and we needed a method of differentiating them with data from a single response.  Enter JSON.

To parse the JSON, you need aJson Library made by Marcus Nowotny.  Download, extract it and put a copy in your libraries folder.  Name the it aJSON.

The Arduino sketch below receives the following response for a GET request:

{'RELAY1': 'ON'}

and then toggles the relay.






You can see then, that returning more than one key-value pair can still be parsed from a single request.

Depth Sensor

The hardware was built directly from the Practical Arduino project, but the source code was almost completely overhauled to handle App Engine and our tank visualization, which you can see in action on the Aquaponics Tracker page.

Figure 1.  Depth sensor PCB.  The differential pressure transducer is bottom right.

Why track depth?  Actually, there are a number of reasons.  As you can see in Aquaponics Tracker, you can combine stock information with the depth to monitor the stocking density during system cycling.  You can monitor the depth for potential leaks, automatically refill the stock tank from a sump tank (due to losses from plant growth, evaporation, etc.) and when paired with pump control, you can prevent overflows and/or cycling too much water.

You can see the schematic for the depth sensor below.  The drawing is nearly identical to the Practical Arduino schematic, but we added the missing capacitor for noise reduction.

Figure 2.  Depth sensor schematic with missing capacitor.

Real-Time Clock - Part II: Grow Light Controller

In Part I of this tutorial, you learned how to set up the ChronoDot  with the Arduino and use it to track time.  Part II continues the discussion by creating a timer that can be used to control grow lights.

In a previous post we talked about the importance of light for plants and the role grow lights play in Controlled Environment Agriculture either by monitoring current light levels and providing supplemental light, or by providing all of the light for a fixed time every day.  Part II focuses on the latter, creating a timer which toggles a light on at a certain time of day, toggles it off at another and uses the ChronoDot to track the time.


Parts List
1 x Arduino Uno R3 (IDE 1.0.3)
1 x ChronoDot
1 x PowerSwitch Tail II Relay
6 x Jumper wires


Fritzing Diagram


Figure 1.  Grow Light Timer diagram.

Arduino Libraries
In addition to the libraries from Part I, you will need two new libraries for this sketch:  Time and TimeAlarms.  The Time library is used to set the Arduino's system time as well as to compare times during the initial setup.

TimeAlarms is used to create two alarms, one for the time each day the light is turned on, and a second alarm for the time of day the light is turned off.

Download both zip files, extract them and move a copy into your Arduino's libraries directory.


Arduino Sketch
This sketch takes two times, given as hour, minute and second and creates an alarm for each.  When you first launch the script, the start time and end time are compared to the current time.  If the current time is between the two times, the relay is toggled ON.

During each operational loop, the current time is retrieved from the ChronoDot, the system time is reset and the current time is displayed.


Real-Time Clock - Part I

A fundamental necessity of any controls system is the ability to track time.  As far as we are aware, the Arduino has three methods it can employ:
  1. Serial.  Repeatedly get the time over the Serial connection.
  2. External Hardware.  Real-time clocks, like the ChronoDot from Macetech, establish a base time when the Arduino sketch is compiled.  When you request the current time in the sketch you actually receive a time based on the time that has elapsed since compilation.
  3. Ethernet.  Access time using the internet NTP service.
This tutorial set focuses on option 2.  In Part I we explain the basics of getting the ChronoDot set up and displaying the current time over serial.

The ChronoDot

The ChronoDot is a high precision real-time-clock (RTC) and boasts a number of features needed for Aquaponics. The V2.1 release introduced the DS3231SN chip, which has an industrial temperature range of -40C to +85C and outputs a temperature compensated time - important for aquaponic control systems that reside outdoors in the heat and direct sun.  
Figure 1.  The ChronoDot V2.1, credit Macetech.

The ChronoDot includes an onboard battery cell for a CR1632 battery, allowing the clock to keep track of time should the Arduino lose power, regain power and restart. Anyone in aquaponics can appreciate the ability of a control system to automatically reboot and resume operation in the event of a power glitch. The disadvantage of the ChronoDot, and RTCs in general is the inability to handle Daylight Savings Time.

The Environment DAQ can be configured with the ChronoDot using the prototyping area, which is exactly wide enough to handle the RTC (coincidence?). If mounting to the shield, you can access the pins from the bottom.
Figure 2.  Environment DAQ Shield with ChronoDot.

Parts List
1 x ChronoDot
1 x Arduino Uno R3
4 x Jumper Wires

Mounting the ChronoDot
The Fritzing diagram below shows how to connect the ChronoDot to the Arduino. Note that the RTC connections are on the right-hand side - the pins on the other side are not used.


Figure 2.  Connecting the ChronoDot.

Arduino Library
The ChronoDot requires two libraries
  1. Wire.h - Included with the Arduino
  2. RTClib.h - Download here.
Download the zip file and extract it. If necessary, rename the extracted folder "RTClib", and then move a copy into your Arduino libraries directory.

Arduino Sketch
Part I of this tutorial simply outputs the current time from the RTC to Serial; part two shows how to set create a toggle time.