Reid Carlberg

Connected Devices, salesforce.com & Other Adventures

Category Archives: Tech Life

Bootcamp Bandwagon Comes to the World of IoT

0

Interesting to read about hardware accelerators, this one in Berlin from Deutsche Telekom. TechCrunch notes:

“Specifically, the telco is on the hunt for early-stage companies operating in the following three categories: smart home, consumer electronics and hardware; wearables and mobile; and B2B commercial applications.”

TC goes on to list a few more accelerators and startups.  These are great, but what can you really get done in three days?  Feels to me that it’s more like an investor meet and greet than anything else, not that there’s anything wrong with that.  I hope to hear more about the B2B Commercial Applications segment.  Not as sexy as consumer, but lots and lots of upside.

Also noteworthy: Deutsche Telekom’s hub:raum has locations in Berlin, Krakow and Tel Aviv, all with local programs for tech startups and all of which claim to be a version of Silicon Valley.  DT definitely has the money and technical chops to make great stuff happen, but I’m always skeptical when Location B claims to be the next SUPER AWESOME LOCATION EVERYONE ALREADY LOVES.

Philips Hue, Raspberry Pi, Node.js, Salesforce and You

5

One of the devices I brought to the Dreamforce Connected Device Lab was a Philips Hue Starter Kit. A friend of mine whipped up a little Ruby script to flash the lights and change colors at random for the Lab, and I got to wondering what it would look like to control the whole thing through Salesforce.  Here’s what I did.

TL;DR: Connected a Raspberry Pi to the Hue bridge using Node.js.  The same app is connected to a Salesforce app using the Streaming API and is controllable using the Salesforce1 mobile app.  Salesforce Package (and raw code).  Node.js Code.

The Hue system has a pretty straightforward architecture.  The bulbs connect to a bridge and that bridge connects to your network.  Once it’s on the network, you can start to control them via the Hue app or the Hue API.

Step 1. The first thing you need to do is choose the device you want to use to control the bridge. This device will have to live on the same physical network as the Hue bridge.  I chose a Raspberry Pi.  Configuring the Raspberry Pi is pretty easy, these are my standard steps.  This includes installing Node.js which isn’t part of the Raspbian distro.

Step 2. Use NPM to install the Hue interface library.  “Node-Hue-API” seems to be the winner in this department.  Follow the instructions on the Github repo.  They’re really good.  The basics are:

  • Find the bridge and get the IP address (code).
  • Create a user on the bridge (code) — note that you’ll need to update the code with your bridge’s IP address.
  • Remember everything it just spat out for later.

Step 3. You are now ready to configure Salesforce.  There are a few steps to this process.  If you don’t have a developer org, it’s time to go get one.

Configure a new connected app in your developer org (detailed instructions from Help).

Start by clicking on Setup at the top of your screen, click on Create on the side, click on Apps, and then click on the New button under Connected Apps.

Apps___salesforce_com_-_Developer_Edition-10

You will now see the New Connected App screen.  Enter the required fields indicated in red.  The API Name is an internal name that will fill itself in once you complete the name field.  Check the box labeled “Enable OAuth Settings” and add http://localhost:3000/oauth/_callback as your callback URL.  Select all of the available OAuth scopes, and click the add button.  Click on Save.

Connected_App___salesforce_com_-_Developer_Edition-17

Once you click on “Save”, you’ll have a fully configured connected app.  You’ll be using the Oauth2 web server flow, which requires your consumer key and your consumer secret.

Connected_App__Hue_Sample___salesforce_com_-_Developer_Edition-2

Note: it may take a few minutes for your new connected app to be fully active.  This is normal.

Finally, configure your personal security token.  (Lots of information about Salesforce Security Concepts including the security token is available in the API docs.)   Start by clicking on your name at the top of the screen, click on My Settings in the drop down menu, then click on Reset My Security Token on the left.

Hello__Reid_Carlberg____salesforce_com_-_Developer_Edition-10

Click on the big green “Reset My Security Token” button.  This will result in an email which we’ll use again in a minute.

Reset_My_Security_Token___salesforce_com_-_Developer_Edition-2

Now that your Connected App is created, you need to install the unmanaged package which contains the objects, code and UI that helps you control the Hue.  You’ll be able to see the code once you’ve installed it, or you can see it on Github.  For now, use this link to start the install process.

https://login.salesforce.com/packaging/installPackage.apexp?p0=04tU0000000E0eU

This will guide you through a series of steps.  Since this is a demo and you’re in a demo environment, just accept all the defaults.  You’ll click “Continue”, “Next”, “Next” and finally “Install”.

Install_Package___salesforce_com_-_Developer_Edition-4

When you have done this successfully, you will land on a screen which declares “Install Complete!”.  You must now click on the app selector in the upper right which probably says “Sales”.  Then click on the app labeled “LAB Hue Interface”.

Package_Details__LAB_Hue_Demo___salesforce_com_-_Developer_Edition-4

Once you do that, you immediately see a screen telling you that setup is not yet complete.  Please go ahead and click on the big “Complete Setup” button.

Mozilla_Firefox-2This will bring you to a screen that helps you insert something called a Push Topic.  Push Topics are part of the super cool and extensively documented Streaming API.  Don’t worry, just click the button.

salesforce_com_-_Developer_Edition-2

And then click the link.

salesforce_com_-_Developer_Edition

Which brings you back to the Hue interface, which now looks like this:

Mozilla_FirefoxAs an aside, you might be thinking, wow, that’s a lot of steps.  And you’re right!  There is a way to automate this configuration, however, you wouldn’t be able to see the code as easily.  So I’ve opted for a few more steps so the example is easier to understand.

Step 4: Finish configuration of your Raspberry Pi.  The good news?  This is now going to be very easy.

First, use npm to install nforce and faye (“npm install nforce faye“).

Next, get the basic code required to connect Salesforce to the Hue.  When you get it on the Raspberry Pi and save it as “hueforce.js”, you’ll want to use nano or another editor to update the field values to yours.  You’ll need to add your user info, your OAuth / Connected App info and your Hue info.

Projects_—_pi_raspberrypi____Projects_hueblog_—_ssh_—_115×25-18

And now you can run your app using “node hueforce.js“.  Now all those great blue buttons are controlling your Hue!

Step 5.  Finally, turn it into a mobile app using Salesforce1 (iTunes, Google Play).  In Salesforce, you’ll need to navigate to the tab configurations and edit the “Hue” tab.

Custom_Tabs___salesforce_com_-_Developer_Edition-9

In the Hue tab settings, make sure you have “Mobile Ready” checked.  Be sure to save your changes.

Edit_Visualforce_Custom_Tab__Hue___salesforce_com_-_Developer_Edition-5

Now you are ready to add this page to the Mobile Configuration.  Click on Setup, select Mobile Administration, and click on Mobile Navigation.

Mobile_Navigation___salesforce_com_-_Developer_Edition-6

Once that’s done, you simply login to the Salesforce1 app, touch the menu selector in the upper left, then scroll down until you see “Hue” listing under “APPS”. The UI is exactly the same.

image__2_-6  image (3)

I’ve also added a fun trigger that automatically fires the “siren” setting when a new lead comes in.

That’s it.  Questions, comments, I’d love to hear them.

Control an Arduino with a Raspberry Pi Using Node.js and I2C

8

You might also find this post helpful: Read Data from Multiple Arduinos with a Raspberry Pi B+ using I2C and Node.js

As a software developer, I usually stay away from lower level communication protocols. My logic has historically been: they’re a lot of work, and my time is better spent higher up the stack.

A couple of weeks ago, after suffering through a variety of failures and uncertainty higher up the stack, I decided the only way out was to go at least a little deeper. So I bit the bullet and connected a Raspberry Pi to an Arduino using the I2C protocol, and then was able to control the Arduino using Node.js. This post covers how I did it.

Raspberry-Pi-Wild-Thumper.jpg

My use case was fairly simple.  I decided to drive a Wild Thumper using the Salesforce1 mobile app.  The Wild Thumper is controlled by an Arduino compatible board, aptly named the “Wild Thumper Controller“.  You might remember I had a couple of these with me at the Connected Device Lab this Dreamforce.  In order to communicate with Salesforce, it needs to have some kind of a gateway that can talk to the Internet using HTTPS, which is where the Raspberry Pi comes in.  If you have a different flavor or Arduino sitting around this should still work.

There are a lot of really great technical introductions to I2C (including — gasp — one from 2001), but you don’t really need them to get started.  Keep these two basic ideas in mind:

  • You’re going to work in a Master-Slave configuration.
  • You need to connect three pins.  SDA, SCL and Ground.

That’s it.

Step 1: Slave Configuration on the Arduino compatible Wild Thumper Board is pretty easy.  In fact, you can simply use the “Wire > Slave Receiver” sample code included with the IDE to get started (below). Load this in the Arduino IDE as is, send to your device and then start the serial monitor.  Put it aside.

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

Step 2: Setting up the Raspberry Pi is a little more difficult, but it’s well documented.  If you are using a standard Raspbian distro, the I2C port is not yet enabled.  Adafruit has an excellent post on enabling the I2C port.  Follow those instructions, then c’mon back.

Step 3: Wire your Raspberry Pi and your Arduino together and make sure the RPi can see the Arduino.  Pinouts on the RPi are a little irritating.  I use the Pi Cobbler.  It gives you a handy, easy to read guide to which pins are where.  If you are connecting directly to the GPIO pins, be sure to consult a pinout digram.

Before you connect everything, your RPi should produce this when you use the i2cdetect command.

Raspberry-Pi-Before-Arduino.png

The basic configuration is really simple.  Master SDA pin to Slave SDA, Master SCL pin to Slave SCL, Master GND pin to Slave GND.  On the Wild Thumper Controller, SDA is A4, SCL is A5 and GND is any pin on the outside edge of the board.  And an Arduino Uno R3, there are special pins for I2C, above the AREF pin, clearly labeled on the back side of the board. In my case, the result looks like this (that’s the Wild Thumper Controller on the right, Pi Cobbler on the left):

Raspberry-Pi-Arduino-I2C-Wiring.jpg

After connected, your RPi should produce this (note that there is now a device on 4).

Raspberry-Pi-Arduino-After-I2C.png

Step 4: Install Node.js on to your Raspberry Pi using this simple link.  If you prefer to be a bit more hands on, you can follow these instructions.  If you prefer to be really hands on, you can install it by compiling from the source, but it will probably take > 2 hours and so far in my experience it’s no different.

Step 5: Install something that lets Node.js talk to the Raspberry Pi’s I2C system.  You should use the I2C library.  There are a couple of others out there, but this is easy and popular.  Now you should be able to do something like this:

var i2c = require('i2c');
var device1 = new i2c(0x18, {device: '/dev/i2c-1', debug: false});
device1.setAddress(0x4);
device1.writeByte(0x2, function(err) { console.log("error"); console.log(err); });

And see a “2” in the Arduino serial port monitor (like so).

Arduino-Serial-Monitor.png

There you have it — the basics of controlling an Arduino using a Raspberry Pi with Node.js and I2C. My original goal was to control the Wild Thumper via the Salesforce1 app.  I was able to accomplish that pretty easily.  Here’s my basic controller for the Thumper, and here’s the Node.js app running on the RPi.  I created a simple UI on the Salesforce1 mobile app (below)  that sends commands to the Thumper using the Streaming API and connects to Salesforce using the excellent nforce.

Salesforce1-Arduino.png

Enterprise IoT To-dos: Play, Hack, Make

0

The last couple of months have been pretty interesting from a travel point of view. I boiled down my thoughts into the following presentation. It’s modified slightly from my presentation in Miami in order to be easier to read.

Original posts for the Dreamforce 2013 Connected Device Lab and ThingMonk are also still around.

%d bloggers like this: