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|
Welcome back! In this lesson we’ll be setting up the breadboard with our sensor and LEDs.
I’m going to be using a breakout board for this project, because I find it easier to work with, because attaching the wires directly to the pins on the Pi can get cramped.
If you’re not going to use a breakout board, you’ll still be able to complete this project with some female-to-male wires.
Before we start in, let’s attach the breakout board, then I’ll lay out all the materials needed for the rest of the project.
First, I’ll attach the ribbon to the breakout board... and now I’ll attach the breakout board to the breadboard... In reality, it doesn’t matter which side you attach this to, as long as you’re aware of which rail is positive and which is negative. In this demo, the blue side of these rails is positive, and the red side is negative.
Now attach the other side of the ribbon to the Pi. If you’re new to this sort of thing, this is basically mapping the pins to the holes on the board.
Okay, I’m going to jump forward to when I have all of the components laid out. And here we are…
You can see that I have the breadboard with the breakout board attached. I have 3 groups of components; the first is at the top, it’s for the green LED, there are two wires, the LED and a 220 ohm resistor.
Then there’s the bottom right, which is the same as the top group of components, except it has a red LED.
The final components are for the temperature sensor, so there’s the DS18b20 sensor, three wires and 10k ohm resistor.
Let’s start by adding the LEDs, and we’ll go with the green first. So, let’s add the green LED to the board. With LEDs there’s a positive and negative pin, the way you can determine which is which, is because the positive side is slightly longer. Notice they’re just slightly different.
Let’s place this on the board...we’ll put the negative pin in J55 and the positive in J56. Then we need to add a resistor, one side will go in I56, the other will be I60. Now we’ll need a longer wire to connect F60 to J10. This will connect to GPIO 18. Now, in code we’ll be able to use GPIO18 to control the LED. Now let’s complete the circuit by adding a wire at F55, and connecting it to the ground.
That’s it for the green LED, so let’s do the same for the red. Looking at the pin length, you can see which is the positive and negative. I’ll add the negative to J45 and the positive to J46.
Next, I’ll connect one end of the resistor to I46 and the other to I48….
Now I need to connect a wire from H48 and then over to H24, which corresponds to GPIO 21.
And once again, we need to complete the circuit by adding a wire from F45 to the ground line.
So, now we have two LEDs wired up to general purpose I/O pins. Now GPIO 18 controls green, and 21 controls red. By the way, you could use any GPIO you want other than 4. So long as you update the code to use the correct pins.
Now let’s add the sensor...this is the DS18B20 temperature sensor. If you’re using a 4 pin version, or a different temperature sensor, then you’ll need wire it up based on the manufacturer's specifications.
For this particular sensor, the left pin is the ground, the middle is the 3.3 volt power, and the right pin is for data. Even if you are using the same sensor, double check the pins to make sure you’re wiring this up correctly.
So let’s add this to the board…
The ground pin will connect to E25, which means the power is E24 and data at E23….Be honest, it kind of feels like a game of battleship, doesn’t it?
Next, lets add the ground line at C25, and the other end at C36. You could just as easily use any of the holes along the ground rail, however, I want this to be a bit more visible to you, so using this ground keeps it out of the way a bit.
Next up we’ll add the resistor to C24 and C23, which sits in front of the power and data pins.
Now let’s add the data line, to A23 and the other end needs to connect to GPIO4, which is any of these pins in the 52 section, so let’s use B52. The reason we need to use GPIO 4 specifically is that when we enabled 1-wire, which is the way this chip sends data to the Raspberry Pi, it enables it on GPIO 4 by default.
Now let’s complete the circuit by connecting the 3.3 volt power. So I’ll connect the wire to the 3.3 power, and the other end to A24.
With that connected, we’re done wiring up our project. I recommend if you’re following along you double check all of your connection and make sure everything looks good. In the next lesson we’ll take a look at the code that’ll make all of this work. So if you’re ready to see how the code works, 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.