CloudAcademy
  1. Home
  2. Training Library
  3. Microsoft Azure
  4. Courses
  5. Developing For The Raspberry Pi and Azure IoT Hub

Running The Code

Contents

keyboard_tab
Intro
Summary
8
play-arrow
Start course
Overview
DifficultyIntermediate
Duration50m
Students198

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.

Raspberry Pi Kits / Sensors

CanaKit Ultimate
Azure IoT Starter Kit
DS18B20 Sensor
Kuman Sensor Kit

Here’s some recommended reading if you’re new to IoT.

How to use a breadboard
Raspberry Pi documentation
IoT Hub getting started with Node.js
Azure Node.js SDK samples
Pin Layout

 

Developing For The Raspberry Pi and Azure IoT Hub: What You'll Learn

Lecture What you'll learn
Course Intro What to expect from this course
Service Setup Creating the services
Configuring The Services Configuring the services and testing them
Preparing The Raspberry Pi Getting the OS installed and configured
Preparing The Breadboard Wiring up the solution
Reviewing The Code Reviewing the application
Running The Code Testing the solution out
Next Steps What's next

 

Transcript

Welcome back! In this lesson we’re ready to get the code on the Raspberry Pi using Git and try everything out.

Here are our objectives for this lesson.

Install the code via Git
Install the application dependencies
Create a new device in the Azure Portal
Set the credentials
Run the application

I have my Raspberry Pi powered up and I’m connected via SSH. I’m here in the home directory for the pi user, and I need to download the code from GitHub. I want to make sure that git is installed, so I’ll issue the git command...and it gives the usage info, so git is installed.

If I switch over to the browser, you can see I have the code in a repo named iothub_rpi. Clicking on the “Clone or download” link gives us this popup where we can copy the URL. So I’ll copy this, and head back to the terminal.
The command to clone this repo is “git clone” followed by the URL. Once this downloads we’ll be ready to start installing the dependencies.

Okay, it’s all downloaded. So let’s cd into the directory, the directory named the same thing as the repo.

So let’s run “npm install”

This is going to take a little while because it’s going to install all the libraries as well as C extensions. I’m guessing this will take a couple minutes, so I’m going to jump forward with some editing magic.

Okay, this did end up taking just under 3 minutes. All of this ran successfully, which means we can now test out the code. If you run into errors here, check that you’re using a recent version of node, you can see where I’m using version 6.10.2.

We now have the code and its dependencies installed, so it’s time to get a new device created in the portal to use for the raspberry pi.

So let’s head into the portal, and we need the device explorer tab. And click on Add device…
I’ll name this Raspberry Pi and leave the defaults set for everything else. And click save…

With this created, let’s select it from the list and copy the primary key by clicking the copy button. This is basically the password the device will use to connect to IoT Hub. The username is the device name, and the URL is the HostName that you can see here in the connection string, or on the Overview tab.

Let’s head back to the terminal to update the creds.js file. For this we’ll use nano. So we’ll type “nano creds.js”

Here, my hostname is already set correctly because I committed this file with the hostname. You’ll need to change it to your host name.
Let’s paste in the access key…
And let’s change the DeviceId to be the name of the device we selected in the portal. Which is “RaspberryPi.”

With this set press control-x to close out, followed by Y to save the changes...then enter to save.

There’s one more change you may need to make, and that’s to edit the pins.js. Currently it’s set to use GPIO18 for green, and GPIO21 for red. If you selected different pins, you’ll need to change the values.

Alright, this is where we see if all of our hard work paid off...it’s time to run the code and actually try out the sensor and LEDs that we wired up previously.

If you’re following along, or you’re doing your own thing, don’t be discouraged if this doesn’t work right off the bat. Anyone who’s worked with IoT projects can tell you, things don’t always work out as you expected. Differences in hardware, or changes in the OS or supporting libraries can cause things you expect to work to throw errors. So if things aren’t working out, do your best to verify that each component is working correctly.

Since we’ll be interacting with some low level functionality we’ll need to run this code as a root user, so let’s test this by running “sudo nodejs app.js”

Here’s a quick reminder of what we should expect. First the green LED should blink when a message is sent to IoT hub. And the red LED will be turned on when the temperature exceeds 70 degrees fahrenheit.

Running this you can see the raspi-io library is going to drop you into the REPL shell, though you don’t need to worry about this.

Keep an eye on the green LED, it should blink...and there it is. So, one message was just sent…
Looking at DocumentDB, I have a query already prepared here to show all documents with a temperature greater than 60 degrees, and order the results by the most recent.

Notice the top record here, this is the result that was just sent when the light blinked. These other records were from a test I did prior to recording. So it’s 64 degrees in my house.

Let’s increase the temperature of the sensor and see if we can’t trigger the red warning light. I have a hair dryer that I’ll use in short bursts to bring the temperature up. Notice the green light starts, blinking as the sensor is heating up. If I keep going, the red light kicks on, which means that the it should be registering a temperature greater than 70 degrees.

Looking at the documents sent to IoT Hub, you can see the top result is around 74 degrees. If I keep refreshing this, you’ll see that the temperature slowing starts decreasing.
Once the temperature gets below 70, we can try to disable the warning light by using a direct-method.

For that we’ll use the iothub-explorer. I already have iothub-explorer in a new terminal window, and I’m already logged into it. So, now that this is finally below 70, let’s switch over to the terminal and run the device method.

Keep in mind, iothub-explorer is running a command on my laptop, which is sending a request to IoT Hub, and IoT Hub is sending it to the device. I love this functionality, because we can execute a method on a device with just a command, and from anywhere!

So, let’s use the device-method subcommand, and we’ll tell it which device we want to run the method on...okay...followed by the name of the method...and...there we go...we get a success status back, and off goes the red LED.

With that, we’ve successfully run a device method and disabled the LED by telling IoT Hub to instruct the device to run some code.

This may not seem like much, but being able to trigger code on devices from our back-end applications is something that if you had to implement for yourself, in a secure and scalable way, would require a lot of effort.

So here we have it, our project works! If you’re like me, you probably want to keep going and do more. In the next lesson I’ll recommend some things you could try out. So I’ll see you in the next lesson!

About the Author

Students31494
Courses29
Learning paths16

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.