Embedded project: 4 channel Arduino-based power controller


20151101_170042

The Sainsmart 4 channel relay board. Digital inputs (lower right) control standard 110v through the four relays (large blue boxes).

One of the directions my PiBox project is headed is as a central hub for sensor management.  This implies the ability to communicate between the pibox and the sensor node using BLE or Wifi.  PiBox can do that already over Wifi (and supports basic BLE communication though lacks a meaningful UI or automated registration) so I only need to start work on sensor nodes.

Now it's hard for me to toss together a temperature sensor node without having a purpose for knowing the temperature.  So whatever node I created needed to have a purpose.  Enter:  my newly configured aeroponics systems.  This system uses a 5 gallon bucket with a water pump inside to spray nutrients on the roots of the plants.  Lamps are hung from a support structure (a wire frame shelving unit) .  Lamps and pump are plugged into a simple manually configured timer.  Nothing special there, except for a really leggy tomato plant that has yet to fruit.  But that's a different project.

So I thought to myself:  what if I created a programmable timer for the lamps and pump?  I could use an Arduino for the brains, plus added components to make it clever.  That's my sensors purpose:  handle power on and off of the lamps and pump for the aeroponics systems.  So was born apc:  the Aeroponics Power Controller.

Design

20150907_104113

The aeroponics tub and manual timers (lower left)

I'm sure the first comment will be:  but that's not a sensor.  Yeah, it's not.  But the concepts are the same:  embedded device controls some physical components based on input from a remote management interface.  For apc the physical components are power outlets.  APC's initial implementation will use four controlled outlets, each capable of being programmed independently.  It needs to be able to run scheduled events (power enabled and disabled on each outlet).  There should be some way of knowing the status of the controller:  running or not.  And there should be some status indication of what's happened, re: a display of some kind.

Components

Based on the design I need the following components.

20151101_170114

Pro Mini (center), RTC (lower left) and USB breakout plus LED. Only thing not shown here is the FTDI USB board, which connects to the pins on the left end of the Pro Mini.

These are the main off-the-shelf boards I need.  I also need a breadboard to hook them all up, an LED and associated resistor and a bunch of wires.  I discovered, after finishing the version of the project, that soldering is limited to adding headers to the boards and a few wires to the headers.

Why the Mini Pro?  Well, for one thing there were on sale at SparkFun back in March 2014 when I bought a bunch of similar boards without having a specific purpose for them.  So it's been sitting around for awhile waiting for an inspiratio on my part.  But it also has a very small footprint without all the bells and whistles (re: bulky connectors) required for other beginner projects.  And it has plenty of computing horsepower for this kind of project.  It's actually way overkill.  But I already had the board, so what the heck.

The TinyRTC is a real-time clock with a battery.  The battery isn't really necessary but it's nice to have if you lose power.  Though without power the apc won't work.  Dealing with that problem is beyond the scope of this project.

The 4 channel relay is the board that converts the control signals from the Arduino into the enable/disable actions of the power outlets.  The relays are optically isolated so when each is enabled they make a loud click sound.  You know they're working if the click is loud and distinct.  If it's a quiet click you probably don't have enough voltage on the control lines of the 4 channel relay.

The ftdi basic breakout board is what you use to connect your linux box to the Mini Pro.  It's what you use to upload your programs to the Arduino.

The USB power from your computer is enough to handle programming the Arduino and running the program.  But it's not enough to change the state of the relays.  So a USB breakout board is used to connect a wall-wart power supply with 5V output to the APC.  This powers the Arduino and the relay board.  Note that you can connect this power source AND the power from the FTDI Basic at the same time.  It won't hurt the board (as far as I can tell).  That's how you test your code.

Hook Up guide

The nice thing about this project is how it can be done with a series of off-the-shelf breakout boards.  The Mini Pro has plenty of digital lines to connect to more than one board.  Strangely the pins I need from it for sda/scl to talk to the TinyRTC are not aligned to the breadboard.  That's a trade-off when using a smaller microcontroller board like this, but it's a minor inconvenience.  I just soldered two pins to the A4 and A5 holes in the Mini Pro, then used female connectors to wire them to the breadboard at the SDA/SCL lines on the RTC.

Fritzing diagram for first APC implementation.

Fritzing diagram for first APC implementation.

The 3.3V power from the Mini Pro is not strong enough to switch the relays (not shown in the Fritzing diagram) so a USB breakout is used to bring in power from a wall wart that provides 5V input.  In fact, that's why I added two additional outlets to the original set of four outlets.  The two new outlets are not controlled and I can plug the wall wart into the USB breakout board.  Easy-peasy.  This is why iot is so popular.   It's a bit like Lego building, but with wires and pins.

Coding

All the code for this project is available from the git repository at gitlab.com.  The code utilizes two libraries for the RTC, the Wire library for serial communications and the Timer library for running scheduled events.  The Timer library provides for events to be run on short intervals like 100ms.  The RTC libraries allow checking the current date/time against the schedules to see if an event should be triggered.  Events are simply when the outlets are enabled and disabled.

As a side note to programming:  I'm not using the Arduino IDE.  I think IDE's are a crutch that prevents you from learning WHY things work.  So I'm using Tim Marston's arduino-mk to edit and build from the command line.   I added support for opening  serial monitor to it, which you can find in the source code under the docs directory.  I also use screen to create a bunch of virtual terminals in a single window.  This I do because I can then work from different systems (desktop or laptop in different rooms of the house, for example) without losing history or resetting my environment.  Lastly, I use cdtools with my project because I have a zillion projects and cdtools functions make it easy to bounce around them.  Config files for both screen and cdtools are in the docs directory.

Automated Registration and Web UI

The next phase of this project will add networking to the system.  An ESP8266 will be added to provide wifi connectivity.  The ESP8266 uses an AT command set to connect and send and receive data from remote servers.  The plan is to have the Pro Mini perform an initial registration with a remote server running on PiBox.  The registration is initiated with a multicast message from PiBox asking for new devices.  The device receives the message and sends a whoami packet with it's IP address and a registration token (something akin to a secure key, I think).  Once registered the device can access the PiBox web server via a RESTful interface.  The interface will provide scheduling data for the device and the device can report status back.

Of course this requires a web interface for setting schedules for all registered devices.  Fortunately PiBox already has a web UI with a RESTful API.  I just need to extend it to support IoT devices.  Then I need to create a UI, potentially using a self-defining UI format so devices can create their own configuration options without having to place the knowledge in the UI.   If done right this format won't take up much space on the device because there will be little to configure in each IoT.  That's the point:  IoT implies a device with a simple purpose that is remotely managed.

Beyond the ordinary

The ESP8266 will connect to the serial lines of the Pro Mini so that means the serial console won't be available.  There is a neat little trick around that:  a color LCD display.  These devices are cheap, around $10-$20.  I've selected a four wire device that uses SDA/SCL lines for data plus a ground and Vcc.  Luckily the TinyRTC board has extra SDA/SCL pins to piggy-back another device like the LCD.  And Adafruit has libraries for these devices that make it very easy to use.

After all this is done there is only one thing left to do: extend scheduling for my holiday lights.  After all, wasn't that really where this was headed?  I mean, do I really need brains in my outlet timer for a tomato plant?  I guess that depends on if that thing ever starts to fruit….

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.