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
Here’s some recommended reading if you’re new to IoT.
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|
In this lesson we’re going to head into the Azure Portal so we can create all of the Azure services that we’ll need for this project.
The three services that we’ll use in this project are IoT Hub, Azure Functions and DocumentDB.
As I mentioned in the intro, we’ll be sending messages from a Raspberry Pi to IoT Hub.
Then we’ll use an Azure Functions app to take the messages from the queue, and save them into Document DB.
We’re not going to configure the services in this lesson, that’ll happen in the next lesson. So if this is something you’ve done a million times before, feel free to skip this lesson, and create these on your own.
Okay let’s start in!
Here I am in the portal, and the first service we’ll use is going to be IoT Hub. So let’s start by clicking New and searching for IoT Hub, and selecting the top option, and then clicking create.
Next we’ll give it a unique name, I’ll name this ca-iot-hub-demo. For this we can leave all of the default settings.
Since we don’t yet have a resource group, we need to create one, and for that I’ll use the same name as the IoT Hub instance with “rg” appended to the end.
And I’ll pin this, and create it.
While that’s creating let’s create the functions app.
So let’s again click New and search for functions...and we’ll select “Functions App” and click create.
This needs a name, so I’ll paste in this name and append “functions” to the end. I’ll need a storage account, and it doesn’t like this name, so I’ll remove the hyphens so this is happy...and that should work.
I need to change this to be East Us so it’s in the same location as the other services.
And I need to set a resource group, and I’ll use the same group as the other services... Perfect.
Let’s pin this and create it.
While this is being created, let’s create a new DocumentDB resource.
Click new and search for documentDB, select it from the list...and click create.
Next we’ll name it… and we’ll select the resource group...and all the rest of the settings are fine with the defaults.
So let’s pin this, and click create!
And it looks like the first resource to be created is Azure Functions...if we head back to the dashboard you can see IoT Hub and DocDB are still being created. I’m going to skip ahead to once they’re both complete.
Okay, DocDB finished in second place after a minute or so, and IoT Hub took just a moment longer.
So let’s pause here, because we now have the 3 services that we’ll use up-and-running. We still need to configure these, and that’s what we’ll do in the next lesson. So if you’re ready to wire up these services and test them out with a simulator, then let’s get started with 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.