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’ll talk about what you need to do in order to prepare the Raspberry Pi for this project.
There are a few things that we need to do in order to get the Raspberry Pi ready for both our code and our sensor.
We need to get the OS installer loaded onto the microSD card
Then we need to plug everything into the Pi
We need to install Raspbian
We need to enable SSH
We need to enable 1-wire
We need to update Node.js
So let’s dive right in. If you’re familiar with the Raspberry Pi then you know that there are plenty of operating systems that you can run. However we’re going to use Raspbian Jessie, and we’ll install it with an installer called NOOBS.
I’m not going to walk you through installing Raspbian step by step, because the best source for this is the documentation. So I recommend that you check the official documentation and follow the steps.
Just to give you a high level run down of installing Raspbian, you’ll need to download the latest version of NOOBS, which is a tool that can install different operating systems onto the Raspberry Pi. https://www.raspberrypi.org/downloads/noobs/
You’ll also need to download the SD formatter from sdcard.org and format your microSD card. https://www.sdcard.org/downloads/formatter_4/index.html
Once you’ve formatted the card you can unzip the noobs file and copy them over it the card. And that’s it, eject the card and you’re ready to start setting up the hardware.
Okay, I’m here with my Raspberry Pi 3 B, and it’s time to actually install the OS. First let’s install the SD card. On the bottom of the Pi is the microSD slot, the card pops and out without much effort.
Now let’s add the ethernet cable. You could optionally use wireless.
Next I’ll add the USB keyboard. And finally I’ll add the HDMI cable so I have a monitor for installing and configuring the OS.
What I didn’t add here is the mouse, because it isn’t needed to install the OS. However you’ll want one on hand.
With all of this added, it’s time to power up the device. The red light here shows that it’s powered up.
If you have the SD card prepared correctly you’ll be welcomed by an installer screen. If you’re using wired internet you’ll see multiple options for operating systems you can install. Otherwise you’ll only see Raspbian. Either way, you only want to select and install the Raspbian full version.
This will take a few minutes to install before it needs to reboot. Once it boots back up, it’ll boot into the Pixel desktop.
Next we need to adjust some OS config settings. Recall I said we need to enable SSH and 1-wire. So let’s do that now since we set them in the same place.
Start by opening up a terminal and typing
Select Interfacing Options
Navigate to and select SSH
Next let’s enable 1-wire. The ds18B20 sensor uses a serial protocol called 1-wire, that allows a single pin for data. So click on interfacing options again.
Navigate to and select 1-wire
It should prompt you to automatically reboot, however if it doesn’t, you’ll want to run:
$ sudo reboot
Once it comes back online, open up a terminal again.
Before we can use 1-wire we also need to enable a couple modules. For that we can run the modprobe command to enable w1-gpio and w1-therm. This will map GPIO4 to use for 1-wire.
$ sudo modprobe w1-gpio
$ sudo modprobe w1-therm
Alright, takes care of enabling SSH and 1-wire.
Since we’re going to use SSH to connect from now on, we need to find the IP address for the Raspberry Pi. So type:
$ hostname -I ← Capital Eye
You’ll want to make note of the IP address so you can connect.
Alright, with SSH enabled I can try connecting from my laptop.
If you’re using Windows you’ll want to use Putty to connect via SSH. So I’ll type
$ ssh pi@
Followed by the IP address of the Pi.
If you got the IP address right it’ll prompt you to accept the connection, so type “yes”
The default ssh password is “raspberry.” And it would help if I typed it correctly...there we go.
Alright, we’re now connected to the pi. Now we need to upgrade nodejs to a recent version.
If you’re using a recent version of Raspbian you can run the update-nodejs-and-nodered script, which will get node and npm updated for you.
As the script says, this can take 20 to 30 minutes. I won’t make you watch this being upgraded, however as long as it doesn’t throw any errors, you’ll be ready to go.
Okay, so now the we have the OS installed, we’ve enabled SSH, and we’ve upgraded nodejs.
So we’re now ready to start wiring up our sensor, and that’s what we’ll do in the next lesson. If you’re ready to wire up our sensor, then I’ll see you 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.