Integrating Raspberry Pi with Azure IoT Hub

Author by Concurrency Blog

Over the past couple years I've been getting more and more involved in home automation. I recently setup and configured OpenHAB. It's running on Raspbian Linux on my Raspberry Pi 2 to communicate with smart devices around my home. Using Azure Automation, I have built some logic around scheduling lights based on sunset and sunrise. In this blog post, we'll go over the next step I've taken to cloud-connect my home by integrating my Raspberry Pi with Azure IoT Hub to support bi-directional communication with services in the cloud.

 

Until now, I was using OpenHAB's REST API to communicate with my Rasperry Pi. This meant that any events, such as opening a door, would only be recorded if I had some sort of automation on a schedule poll for these events. Using Azure's IoT Hub, you can configure devices and services to communicate bi-directionally.

 

The first step in getting start with IoT Hub, is to log in to the Azure Portal and navigate to the IoT Hub blade. Click Add to create a new hub.

 

1.png

 

You'll need to configure a few basic settings. The pricing is based on the number of messages processed per unit per day. There is a free tier to get started.

 

2.png

 

Once created, you'll be able to adjust a lot of settings around access policies, pricing and monitoring.

 

3.png

 

You'll then want to navigate to the keys blade to get the connection string for your IoT Hub. Follow the arrows below.

 

4.png

 

Now that you have the key, you can configure the Raspberry Pi to run one of the Azure IoT Hub SDKs. Currently, there is a device SDK written in C, .NET, Node.JS and Java. Service SDKs are available in .NET, Node.JS and Java. I chose to use the Node.JS SDK so I didn't have to compile anything to run on the Pi. I'm running Raspbian Jessie 4.1.18-v7+. Node.JS is pre-installed on this distro of Linux (my Node.JS version is v0.10.29). What isn't installed by default with this distro is the Node Package Manager (NPM). In order to install NPM, you'll need to use apt-get. Full instructions can be found here. I used the commands below.

 

Sudo apt-get install npm
Sudo npm install -g npm@2.x

 

Once NPM is installed, you'll want to install the latest version of the Azure IoT Hub package. You'll also need an IoT Hub transport package of some kind. I used HTTP. For the full documentation, refer to GitHub. These commands install the core package and the HTTP transport package. 

 

Sudo npm install -g azure-iot-device@latest
Sudo npm install -g azure-iot-device-http@latest

 

There is handy tool you can also install to help register your device with your IoT Hub; the IoT Explorer. It's a cross-platform, command line Node.JS app that can manage your IoT Hub and devices. You can use NPM to install it. 

 

Sudo npm install -g iot-explorer@latest

 

Once IoT Explorer is installed, you'll be able to register your device with the IoT Hub. You could actually run IoT Explorer from any box running Node.JS. It's not necessary to run this on the Pi. The syntax for registering a device is as follows. Make sure there are quotes around your connection string. You can specify whatever device name you'd like. I just called mine "Pi".

 

Iot-explorer login "<IoT Hub Connection string>"
Iot-explorer create <DeviceName> --connection-string

 

Specifying the --connection-string argument causes the iot-explorer to return a device-specific connection string you'll use with your device. You can always look up this connection string in the portal.

 

Now that everything is installed, you'll be able to run your first IoT Hub Node.JS script. Here's an example that I took from Microsoft's GitHub pages. It will send a message and waits for a message back from the cloud.

 

var connectionString = '<your device connection string>';
var clientFromConnectionString = require('/usr/lib/node_modules/azure-iot-device-http').clientFromConnectionString;
var client = clientFromConnectionString(connectionString);
var Message = require('/usr/lib/node_modules/azure-iot-device').Message;
var msg = new Message('some data from my device');
var connectCallback = function (err) {
  if (err) {
    console.error('Could not connect: ' + err);
  } else {
    console.log('Client connected');
    var message = new Message('some data from my device');
    client.sendEvent(message, function (err) {
      if (err) console.log(err.toString());
    });
    client.on('message', function (msg) {
      console.log(msg);
      client.complete(msg, function () {
        console.log('completed');
      });
    });
  }
};
client.open(connectCallback);

 

Now you can run your above script by saving it as a JavaScript file on the Pi and then running the command line below.

 

node myScript.js

 

If you want to see data that the Pi is sending, you can use the Device Explorer app to receive messages and send messages back to the Pi.

 

5.png

 

6.png

 

And that's how you hook a Raspberry Pi up to Azure IoT Hub with Node.JS! My next step will be to integrate OpenHAB's rule system with Node.JS so I can start sending events up to IoT Hub and tweeting every time I turn on a light. ;)

Author

Concurrency Blog

The latest about Concurrency