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 I’ll talk a bit about the decisions that went into making this course, and then I’ll make some suggestions for ways that you could keep learning by changing up what we’ve built.
This course was about getting data from a device into the cloud. I chose to use a Raspberry Pi because it’s a widely used device for hobbyists, and companies looking to prototype IoT solutions. It’s inexpensive and has a lot community support as well, making it a great starting place for a wide range of projects. It also allows you to use a wide range of operating systems, and that opens the door to the tools and languages you can use to build solutions.
The raspberry Pi isn’t the only option, there are a lot of different choices for low cost hardware you can use for IoT and prototyping. In fact, Microsoft has some options for starter kits available, including the Raspberry Pi and Arduino.
I chose Raspbian for the OS because it’s well supported and like the Raspberry Pi, it has a community around it that can help answer any questions.
On the cloud side of things, I chose IoT Hub because it makes it really simple to focus on the device instead of the all the supporting cloud functionality. And that’s because IoT Hub does a great job, in my opinion, of encapsulating all of the complexity that’s required to support messaging from millions of devices. It also has a device registry that’s easy to use.
Once the messages are in the message queue you can use whatever you want to process them, and I chose Azure Functions because as you saw, for a proof of concept, there’s almost no code required. Also, the functions run when triggered, so you don’t need servers always running.
I went with DocumentDB for a couple reasons, first, it’s easy to save a document from a function. Second, it’s a fully managed NoSQL database that’s easy to query. So it’s very easy to use, which makes it easy to demonstrate.
So, that’s a glimpse into some of the decisions that went into planning this course.
As for some next steps. You probably noticed that we only scratched the surface of what we could do. If you followed along, then you have everything wired and running and you may be wondering what to do next.
Here’s my recommendations.
The first thing would be to use a device twin to allow you to dynamically change the maximum temperature variable. This would allow you to change the desired temperature property in IoT Hub and have the device updated.
Next, try capturing all of the sensor reading to a file, and then periodically upload the file.
Another thing that may be worth trying is to create an alert message when the temperature alert is triggered. Create a new route in IoT Hub that sends alert messages to an Event Hub, and then process them with a new function that sends you an SMS message.
Alright, that’s going to wrap up this course.
If there are topics or IoT Projects you’d like to see covered in future courses, or if you have any questions, feel free to reach out.
You can reach me on the community forums. Also, I’m @sowhelmed on Twitter.
I love hearing from you all! Let me know what kind of projects you’re working on or what sort of topics you’d like to see covered further.
I hope you enjoyed this course as much as I enjoyed making it. Thanks for watching!
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.