Arduino Circuit and Software : A Closer Look
I will assume that you have done an Arduino Sketch or two - in which case, you are able to use the Arduino SDK on your computer, connect your Arduino device, and upload (flash) new software to the Arduino. If you don't, please check out other online tutorials; or if you're a Hawaii Hacker on Oahu, come out to the Honolulu Makerspace
sometime and ask about any upcoming Arduino sessions.
This article examines the Arduino's role in the overall project. As with most Arduino circuits -- especially those designed on breadboards -- this is a throw-away mock-up of what will ultimately be used in the final circuit. For our purposes of exchanging both commands and sensor readings via JSON messages, an Arduino hooked up to a few simple circuits will work just fine.
Knowing that this portion is a throw-away mock, however, there's little need to spend a lot of time designing the final circuitry just yet. Instead, a handful of established, "tried and true" circuits are dropped onto the breadboard. Furthermore, a few programming conveniences are added to the Arduino Sketch source code to yield a useful mock-up circuit as quickly and as painlessly as possible.
The Arduino Source Code to operate this circuit is on my GitHub Project page.
Software Feature 1 : Adding printf() to Arduino
When rapid prototyping of any sort is done, it's nice to have as many conveniences as possible at your disposal. For Arduino, one convenience that's almost always absent in most examples on the Web is the use of formatted-string output -- like what you'd find in good old printf(). IMHO, that's either extreme laziness, or excessively minimalist. It's certainly not a philosophy I'd buy into for something I intend to build as a quick throw-away mock.
Fortunately, the Arduino "language" is just a simplified dialect of C, and everything is ultimately compiled by a real C compiler under the hood. With the proper incantation, it's entirely possible to make use of vsprintf
() or its bigger brother vsnprintf() to construct a printf() equivalent function that outputs on the serial port. In this project, I use the less-convenient vsnprintf() to future-proof against the possibility of encountering Unicode characters, though the possibility of Unicode reaching an Arduino in production is a bit of a stretch. Still, it's better to be safe than sorry for a routine I'll probably reuse in future projects.
void serialDebug(char *format, ...)
// string_buffer[ sizeof(string_buffer)/sizeof(string_buffer) - 1]='\0';
Software Feature 2 : Adding JSON Support to Arduino
To accomplish this without the pain of rolling our own JSON parser, we'll just download a freely available JSON library (aJSON) for the Arduino. Drop that into ~/sketchbook/libraries (or appropriate Windows/MacOS equivalent directory), and reference the library from the Arduino sketch. It doesn't get much simpler than that.
Circuit 1 : Reading Ambient Light Levels
This circuit is a textbook Arduino example - +5V to one end of a Photo Resistor, and the other end goes to a junction point connecting Arduino Analog Pin (A0), and a grounded 10K resistor.
Circuit 2 : Controlling a Single LED's Brightness
The PWM features of this circuit are also from a textbook example, with the micro-switch pair derived from yet another textbook example. If it's not obvious by now, I prefer to "stand on the shoulders of giants" when it comes to demonstrating circuits, as there's far better documentation available from the original source that I don't need to repeat here.
Circuit 3 : Controlling an LED Light Strip's Brightness
The LED Light Strip circuit, which uses a TIP-120 power transistor, is derived from a Bildr.com article.
Putting it all together
Alrighty then. The Arduino portion is the easiest part of the project thus far, and it's really just a mock (stand-in) for the final circuit I'll be using for the Aquaponics Controller. The purpose of this bit is to get the JSON messaging working correctly across a Serial I/O link, so that we can build up the Web Service code (on the Raspberry Pi), and ultimately the Android Mobile Application, before pulling out the Arduino mock and replacing it with either a DigiSpark or a Teensy3, combined with a RadioBlock transceiver. Things pretty much have to be developed this way, as my set of RadioBlocks is arriving a bit later than originally anticipated.
End Goal Modifications
The end-goal (an aquaponics controller) is to replace (or augment) the Photo-Resistor circuit with something that reads air and water temperature (will eventually be a data-logged feed for a nice graph in the Ruby on Rails application); and replace the PWM-controlled 12 VDC LED Light Strip with a 12 VDC water pump. With those modifications in place, the Android Application can be modified to control the aquaponics water pump's pressure.
The Android and Ruby-on-Rails application can also be enhanced to turn the water pump on and off on a scheduled basis. Most aquaponics setups are just clock-timers set to enable the pump for 15 minutes per hour, during daylight hours only. In the future, since this project can track light and temperature levels, I may want to enhance the system to automatically adjust the schedule based on light or temperature readings.
Coming Up in Part 3 . . .
In Part 3, we will dive deeper into the Raspberry Pi parts of this project - going over the RefineryCMS (Ruby on Rails) application, using Node.js as a Serial Port to Socket I/O bridge, and exposing a simple Web Page to test the overall JSON connectivity and command/control structure.