Quick IoT Prototyping with Ruby/Rails and the Spark Core

Eli Fatsi, Former Development Director

Article Category: #Code

Posted on

The Internet of Things revolution is upon us. Our phones are getting smarter, internet is becoming more accessible, even dogs have connected accessories. Making these products isn’t limited to electrical engineers or obscurely funded start-ups; with a little bit of programming knowledge and the right tools, you can make your own connected ______ in no time.

A Brief Introduction

Arduino

An amazing player in the “making hardware easy” space. The company and project itself is open sourced, accompanied by a massive community, and is the go-to solution for many hobby hardware projects. Bringing these projects online has long been doable, but was expensive and challenging before Particle came along.

Particle (formerly Spark)

Another awesome company with a great community that entered the playing field in 2013. The Spark Core, Particle's flagship product, has significantly lowered the barrier for making connected devices. Programming them is as easy as programming an Arduino, but these guys connect to the internet out of the box (with wifi credentials) and allow for uploading code online.

Ruby and Rails

Ruby on Rails has been powering modern websites for nearly ten years, including many of the sites built here at Viget. Ruby remains one of my favorite languages to code in through the years and Rails makes it ridiculously easy to create a fully functioning website in minutes.

Establishing Communication

With a mild understanding of the above technologies, and a Spark Core in your possession (or better yet, the newer-better-half-as-expensive Photon), you’re ready to go.

The power that a Spark Core gives you is one of easy internet access. If you’re familiar with Arduino development, you know that the software you write follows a setup/loop pattern. You define the setup and loop functions in your code, and after booting up the chip runs setup once, and then loop infinitely. Defining your online endpoints on a Spark Core is a simple matter of defining available functions and variables in your setup:

void setup() {
 Spark.function("setColor", setColor);
 Spark.variable("buttonStatus", &buttonStatus, STRING);
}

Then, with the use of the Ruby Spark gem, you can easily call your defined functions and retrieve accessible variables using Ruby like so:

# ruby
require "ruby_spark"

core = RubySpark::Core.new(spark_access_token, core_id)

if core.variable("buttonStatus") == "pressed"
 core.function("setColor", "red")
end

Pretty straight-forward Ruby code here, we’re checking to see if the buttonStatus variable is equal to “pressed”, and calling the setColor function on the Core if it is. OVER THE INTERNET!

Putting the Pieces Together

Since we have the power to control our Spark Core with Ruby, we can throw this code into a Rails application, push it up to a free Heroku instance and our device suddenly has it’s very own online interface. This means that as long as your Spark Core is on and connected to the internet, you can interact with your device from anywhere in the world.

I’ve put together a sample Rails application, currently live at particle-interfacer.herokuapp.com. The code is available on github. This app can make function and variable calls to your Spark Core like the ones mentioned above, and is meant to demonstrate just how easy it is to whip up a connected device.

screenshot

Using these concepts, we could similarly build a button that literally does anything online (oh hey Dash), a thermostat that’s controlled by your phone (Nest), or a refrigerator that does your shopping for you (this one might be harder… but doable!). The point is, we have the tools that lowers the barrier for creativity, which is a great thing. Join me in making the world a cooler, quicker, and likely much weirder place!

Related Articles