Control office lights using SF1 + NodeJS + Arduino

Last friday I gave a talk on our local Salesforce Developers Group named
“SF1 and the Internet of Things” (slides).

In that talk I explained how to hook your office lights to an arduino and control that using SF1 from your mobile.
In this post I’m going to give the technical details of that project.

Disclaimer:
The goal of this post is to explain how to connect SF1 + NodeJS + Arduino, I’ll not be posting schematics
or any instructions on how to connect the Relays to the Arduino.
Be careful when doing it, since you’ll be dealing with electricity!
I’ve used LEDs in my presentation and in this post to avoid any hazard, but the system will work with relays instead of LEDs.

Setting up the environment to control

The entire project will be something like this:

First, let’s set up the environment we want to control using the phone.

Arduino, relays and NodeJS

To control the lights we are going to use an Arduino UNO board.
Arduino boards contain a microcontroller that can run C++ code.

We’re going to use NodeJS to control an Arduino through the USB port of the board using a library called Johnny Five.
That library interacts with the Arduino using the Firmata protocol, wich must be loaded to the microcontroller.

Standard Firmata comes as an example in the Arduino IDE:

Besides that we will need relays, to control the power given to the lights.

MORE WARNINGS:
Be extra careful when connecting anything with relays, research about schematics on how
to connect your Arduino and your Relay. There are different relays that
allow different AMPs and Voltages.

In summary, where are going to tell the Arduino to turn ON or OFF a given pin, and that will
trigger the relay to turn on or off the lights.

As In my talk, the rest of this post is using LEDs.

Finally, we will need a server to control the Arduino (and give the “Internet” part of IoT to our board).

This can be a laptop, an old computer or a raspberry pi, as long as it has an internet
connection, a USB port and can run a NodeJS interpreter.

Salesforce configuration

Let’s start with the setup on Salesforce.

This is pretty straigthforward, we’re going to create an object called “Light”,
with 2 fields: State and PIN.

To do so we need to head to Setup/Create/Objects click “New Custom Object”, and then add the 2 custom fields.

After that we need to create “Light” records using the pin of the relay connected to the Arduino, and the initial state.
Give the records names of the places that the lights will be located, for example “Lobby”.

And that’s it! every time we post “!lightsON” all lights will go on, and “!lightsOFF” will do the opposite.

FINAL WARNING:
Do not connect electronics/lamps that require more amps than the relay is rated for!This is a basic guide to demonstrate how to connect SF1 to NodeJS and interact with the Arduino,
do more research before connecting ANYTHING to your Arduino board