Summary

Description

Developing For The Raspberry Pi and Azure IoT Hub

When it comes to IoT there are multiple layers. It’s not just an application deployed out on virtual machines, where the users interact with a web browser. In this course we’ll go through the process of setting up both the cloud and device side of an IoT solution.

This course focuses on how to implement a basic IoT solution. We’re going to setup a Raspberry Pi 3 B, with the Raspbian operating system. We’ll use a breadboard to wire up a DS18B20 temperature sensor, and 2 LEDs. And we’ll use a Node.js application to interact with the sensor, LEDs, and IoT Hub.

We’ll check the temperature every second, and if it’s changed since the last read, we’ll send a message to IoT Hub. Any time we send a message, we’ll make the green LED blink. And if the temperature hits 70 degrees or higher, we’ll turn the red LED on, as a warning light that it’s getting too warm; the only way to disable the warning light is to use an IoT Hub device-method.

So that’s what we’re going to build on the device side of things. On the cloud side of things, we’re going to use IoT Hub to hold the messages in its queue. We’ll implement an Azure Function to listen for messages, and then it’s going to take the message and save it in Document DB.

Here’s what you’ll need to build this for yourself.

First, you’ll need an Azure Subscription, because we’re going to use 3 Azure Services as our cloud back-end. We’ll use IoT Hub, Azure Functions and DocumentDB. You’ll also need a Raspberry Pi. I’m using a Raspberry Pi 3 B, for this demo. Since the pin layout may be different for different versions, you may need to adjust things for your implementation. You’ll also need an SD card for the Pi and a power supply. Most kits come with these. You’ll need a mouse, keyboard and HDMI compatible display for the initial OS setup. You’re going to need a breadboard. You’ll need two LEDs, ideally two different colors. You’ll need a temperature sensor, in particular, if you want to follow along and use the code I’ve prepared, you’ll want to use a DS18B20. When it comes to wiring up this project, you could use some male-to-female wires and connect to the pins on the Raspberry Pi directly. Or, you can use a breakout board, with male-to-male wires, which is what I’ll be using. You’ll also need some resistors, I’m using a 10k resistor with the sensor, and then a 220 ohm resistor with each LED.

Transcript

Welcome back! In this lesson we’ll take a look at the code required to get the sensor we just wired up to work.

The code has a few of requirements. It should send temperature messages to IoT Hub whenever the temperature changes, but not more than once per secondIt should blink the green LED whenever a message is sent to IoT HubIt should turn on the red LED if the temperature reaches the maximum threshold that we set.It should allow you to disable the red LED with an IoT Hub device method

So let’s see how it’s implemented. This is a proof-of-concept written in JavaScript. This is a basic Node.js app, and it’s only a couple of files.

Starting with the package.json you can see that this app depends on the IoT Hub SDK for node. The SDK is out on GitHub, and it has some good examples to help get you up and running.

I’m also using Johnny-five, which is a great library for using node.js to control hardware. Johnny five is an abstraction that allows you to write code for lot of different hardware. Because it runs across different devices, you’ll need to select the IO library for your device. That’s what this raspi-io library here is used for. Then I’m also using this ds18x20 library to read from the sensor, because while Johnny-five does support this sensor, raspi-io didn’t seem to play nicely. Though, it could certainly have been something I did wrong.

The bulk of the code is in the app.js. Starting at the top we import the different libraries, followed by a main function.

The main function starts by grabbing all of the values in the creds.js and creating a device connection string. Looking at the creds.js you can see that we have a HostName, SharedAccessKey, and DeviceID. This is where you’ll end up setting your device credentials.

After we setup the connection string, we create an event emitter. You can see here that we’re listening for temperature change events, and if we get a change event we run the “temeratureChanged” function. We’ll look at that function in a moment.

Once we have an emitter, we create a new board and pass in the emitter. Then we create a new IoT Hub client based on the connection string we created earlier. And we pass it in the protocol. I’m using MQTT because it’s the that currently supports the most IoT Hub functionality.

Then we pass in the “connect” method to the open function of the client.

The connect method sets up some listeners for the client, including an error and disconnect listener. Then at the bottom here we set up an iot hub device method listener. This will listen for any device method calls for the device. This listens for a device method named disableAlert, and it it’s called, it’ll run the “disableAlert” method.

The disableAlert method emits a disableAlert event using the event emitter. Device method functions expect HTTP request and response arguments, and it expects you to call the response.send, along with an HTTP status, to indicate if the method was successful.

Looking at the temeratureChanged function, you can see that it expects an object which stores the temperature, as well as the green and red LEDs. The first thing it does is creates an object with the device id, the temperature, and timestamp, then converts it to a string, and then passes that into a new IoT Hub message object. Then the client sends the message to IoT Hub and makes the green LED blink once. Let’s look at the board.js file. It starts out with a few imports, followed by a function for fetching the temperature, that passes the results into a callback method.

Scrolling down is where we use johnny-five to create a new board and we specify the io plugin to use, which is raspi-io.

Since you can use any GPIO pin you want, there’s a pins.js file where you can set the pins that your red and green LEDs are using.

Once the board is ready we set up the LEDS, as well as fire off a function that’ll run every second and look for a temperature change.

We also use the event emitter to listen for “disableAlert”, and turn off the red LED when it’s fired off. And once the board exits, we turn off the LEDs to make sure they aren’t left on.

So, at the start I said the code should accomplish a few tasks.

I want to summarize this lesson by showing the bit of code that handles each of the tasks I outlined at the start.It should send temperature messages to IoT Hub whenever the temperature changesIn the board.js there’s a timer that runs the “fetchTemperature” function every second, and then it broadcasts the temperature using an event emitter. In the app.js we’re listening for change events and when one is triggered we’re sending the message to IoT Hub.It should blink the green LED whenever a message is sent to IoT HubIn the app.py, inside temperatureChanged function, once the client sends the message, we blink the LED.It should turn on the red LED if the temperature reaches the maximum threshold that we set.In the temperatureChanged we’re performing some local checks to see if the code hit the max value, if so we enable the red LED.It should allow you to disable the red LED with an IoT Hub device method The disableAlert function is setup to be an IoT Hub device method and at the bottom of the app.js it broadcasts a disableAlert event. In board.js the emitter listens for the event and disables the LED.

Okay, let’s wrap up here, in the next lesson we’re going to deploy the code to the Raspberry Pi and then try it out, which should be fun! So, if you’re ready see if all of this works, then let’s test it all out in the next lesson!

About the Author

Ben Lambert is the Director of Engineering and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps.

When he’s not building the first platform to run and measure enterprise transformation initiatives at Cloud Academy, he’s hiking, camping, or creating video games.