Over the past few weeks, I’ve been getting deeper into “The Internet of Things” (aka “IoT”) and experimenting with the Arduino.

By way of background, you should take a look at Chatter Bot that Mike Leach put together for a hackathon last year, and you should also take a look at Kevin O’Hara’s I Integrated Salesforce to My House.  These are both awesome examples, and I’m sure there are more out there I just don’t know about.  Yet.

My goal was simple.  I wanted to create a system that would record data from the real world, analyze that data based on events recorded on the Force.com platform, and then automatically take actions based on that analysis.  And since I’m also a train nerd, I decided model trains were a great way to try this out.

Before I break the project down, take a look at this short demo video.

I started by purchasing a Sparkfun Arduino Inventor’s Kit.  This kit is a great way to get to know the Arduino.  You might also be able to find this at Microcenter stores or find something similar at Fry’s or even your local Radio Shack.  The great thing about this kit is that it includes sample circuits and basic education.  It was a huge help for me.

On top of that, I added an Ethernet Shield.  These fit right on top of the Arduino and let you add network connectivity.  There are lots of ways to add network connectivity, but the shield is cheap and easy (which I like).

Now, you might have noticed I also used an LOLbooster.  Interestingly, you can get a little bit of movement out of either analog N-scale trains or HO-scale trains by simply connecting the Arduino 5V lines to the train pick ups, but once you try to actually do anything real, like have them move along a track, the Arduino just doesn’t cut it.  The LOLbooster fixes all that by taking a signal from the Arduino and boosting it up.  It also gives you an excuse to cut up an old 14v 4a laptop power adapter to drive that additional output.  Great fun!

On the software side, there are really three pieces.  When working together, they look a little like this:

One  of the challenges of working with an Arduino is that it’s lightweight enough that it can’t handle HTTPS.  Whether this is good or bad depends on your perspective, but either way you pretty much have to have a local proxy that handles the translation between what the Arduino produces (HTTP) and what the Force.com platform consumes (HTTPS).  I created a really simple ThingProxy in Ruby and Rails.  It needs hardening, but has proven to be useful for my purposes.

The second piece of software I needed was an Arduino compatible DCC driver.  The good people behind the LOLbooster have also created CmdrArduino and it is fantastic!  Unfortunately, once I added the Ethernet Shield I ran into some conflicts.  Turns out CmdrArduino relies on pin 10 as does the Ethernet Shield.  Now, and this is part of what makes them “the good people”, the author sent me a fix that should get around that.  (YAY!)  I haven’t tried it yet, but will shortly.  In the mean time, I decided I’d mess around with a less robust (but very cool) DCC libary put together by Michael Blank.  Here’s the Frankenstinian result I cobbled together.

Most of my changes to the simple DCC code were about communicating with the proxy.  However, one change is around detecting when the train passes.  I elected to do this with a photocell.  Photocells are great because they give you an idea of how bright the environment is.  Once you know that, you can detect changes and make decisions based on those.  I have the photocell positioned in a way that the passing engine casts a shadow and when that shadow goes away, that triggers a message to the proxy, which sends a REST API call to Force.com.

Finally, I created a simple object in a Force.com Developer Org (free!) to store data, a utility class to issue commands via HTTP callouts, and a trigger to analyze the inbound data for a COUNT mod 5.  When that’s 4, it turns on the yellow LED, and when that’s 0, it stops the train and turns of the LED.  Cool stuff.

There’s a lot more you can do with some time and energy.  For example, you might want to check out Dawson Station.  It’s a great n-scale model controlled by two Arduino’s.

If you’re doing something with “The Internet of Things,” I’d like to hear about it.  Drop me at a note over on @ReidCarlberg or on that new-fangled social network all the kids are talking about.

Have fun!

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS