Skip to main content
Top

2020 | Book

Rust for the IoT

Building Internet of Things Apps with Rust and Raspberry Pi

insite
SEARCH

About this book

Get started programming Rust applications for the Internet of Things (IoT). This book is a programming skills migration book that teaches you the Rust programming techniques most useful for IoT applications. You'll step through from server to board development in creating a set of IoT applications.

In Rust for the IoT, you'll learn how to build a modern server-side application using Rust on the backend. Then you'll use docker and Kubernetes to deploy these to a managed cloud. Finally you will use a Raspberry Pi with a SenseHat and Camera to capture the world around you and send that information to the cloud.

While you will be able to follow along without any cloud or hardware, to make the most of it we recommend a few cloud pieces and hardware that is designed to integrate with the software in this book. After reading and using this book, you'll see how to apply Rust to the Internet of Things.

What You Will Learn

Create a modern Rust backend complete with handling eventual consistency and interacting via a GraphQL interface Use the Raspberry PI to serve as a cheap IoT device that one can easily deploy around the house Capture temperature, video, and use the interactive joystick to interact with the software you’ve created Use OpenCV to perform facial detection from the PI’s camera and save that information to the cloud. Create deployable helm charts for the cloud, and for the device create complete ISOs that allow you to easily deploy the Pi’s OS + custom software

Who This Book Is For

You will need to have a basic understanding of cloud application development at a minimum and the basics of Rust coding. This book is for those interested in or working with the IoT and the Raspberry Pi who want to learn how Rust can work for them.

Table of Contents

Frontmatter
Chapter 1. Introduction
Abstract
The Internet of Things (IoT) is a highly encompassing term that covers everything from your home network-connected camera to the oven that is Wi-Fi connected, all the way to your modern electric cars like the Tesla that are always connected to the network and almost always on. The most basic premise of IoT is a hardware device that is a connected network appliance. In modern days, that usually means Internet and almost always connected to a cloud service, but it can just as easily be a local area network.
Joseph Faisal Nusairat
Chapter 2. Server Side
Abstract
In the previous chapter, we went through the basics of the Rust language. This was of course not a complete course in the Rust language; there are entire books for that, but I hope for those that are not familiar with Rust, it at least gives you the basics to follow along with the coding samples in this chapter. We also started to go over the goals of this book; this will be our first chapter diving in writing code for our eventual completion of that goal and a creation of a full-service IoT application.
Joseph Faisal Nusairat
Chapter 3. File Uploading and Parsing
Abstract
In the previous chapter, we went over the design for the application, including our microservice architecture. Part of that architecture was starting the pipes to have the upload_svc call the retrieval_svc. Majority of last chapter though, we focused on really two things, setting up iron for the web server and creating our database to store media files. Most of the coding focused on the retrieval service and integrating it with the database to store comments and media data. In this chapter, we are going to expand and dive into the upload service. We will be looking at how to upload, download, and parse the media data. Media data often contains extra information about it that tells us track, authors, camera use, and so on, and that is good metadata to store in the database.
Joseph Faisal Nusairat
Chapter 4. Messaging and GraphQL
Abstract
In this chapter, we are going to modify an existing service and create an entirely new service. First up, we are going to add support for message queues (MQs); as you will learn, MQs are invaluable for communication in an IoT system. This chapter we will set up the plumbing for a basic insecure message queue. But as we move along, we will add security and more in later chapters. In addition, we will also discuss GraphQL, which, if you’ve only used REST, you are in for a treat. GraphQL helps solve many of the limitations and frustrations that come in a RESTful system.
Joseph Faisal Nusairat
Chapter 5. Performance
Abstract
One of the most common problems with any application and IoT ones in particular is cost and performance. Remember IoT devices may have to transmit huge amounts of data and depending on the device may have bad or random Internet connectivity (think of an IoT-connected vehicle or a device outside your house far from your Wi-Fi monitoring). We expect our devices to communicate quickly and without lag. No one wants to send a command to the IoT device and wait too long for the command to be processed by the device. This cost is not just in processing time, but it’s a practical cost; cloud providers charge for the amount of data being transferred. Hence, you want to reduce that cost, and there are essentially two ways:
Joseph Faisal Nusairat
Chapter 6. Security
Abstract
If we deployed our site the way it’s currently designed, it would not be a very secure site; in fact, right now anyone could access our message queues and add data to them or hit all of our endpoints in the microservice. This could somewhat work if you were running on a home network (although still vulnerable to any one who gets on your network). In fact, we don’t even have users; this obviously would not make a great application to use in a multi-customer environment. Even as a home project, we’d be locked into one person.
Joseph Faisal Nusairat
Chapter 7. Deployment
Abstract
The first half of this book is designed to focus on the cloud side of IoT development, while in the second half, we will focus on coding against the device itself that interacts with the backend. However, for this to properly work, we need to get the application off our personal computer and into the cloud and, even more importantly, into the cloud securely. The cloud is obviously the end goal for any application, but for an IoT application, it becomes critical to be scalable; last thing you want is a customer to buy an expensive device and have it not work well. In addition, it needs to be cost-effective. While oftentimes we have subscriptions, many times you make the money on the device and then lose money in the cloud. Netflix in particular spends excessive cycles making sure the amount that is downloaded is not only compressed, only what you need, but it can be done without causing their cloud deployment to be too costly. Why lose money where you don’t have to?
Joseph Faisal Nusairat
Chapter 8. Raspberry Pi
Abstract
Now that we have all the deployments of our code for the backend cloud services, it’s time to dive into the Raspberry Pi (Pi) device itself. This next set of the book will all revolve around coding to the Raspberry Pi device. We will perform a variety of coding tasks on the Raspberry Pi mostly using libraries, some going bare metal. Much of this is to give a glimpse of how we can create a connected environment between device, cloud, and user.
Joseph Faisal Nusairat
Chapter 9. Sense HAT
Abstract
In the last chapter, we started to have the Pi communicate with our backend servers and running a more advanced version of "Hello World". In this chapter, we are going to build up on this existing application by adding and using a set of hardware components. After all, one of the things that makes the Pi so great is its hardware extensibility. That is one of the biggest selling points of the Raspberry Pi, being able to add on sensors, cameras, or even custom components that communicate with the board and the GPIO specifically. We will be using a few of these components, but to start with, I want us to use a component that gives us an all-in-one board to use, the Sense HAT. We will be interacting with this board throughout the chapter, gathering the temperature for the board as well as using it as the basis for our future command interaction with the board. In addition, we will integrate it with our login authorization flow.
Joseph Faisal Nusairat
Chapter 10. Camera
Abstract
In the previous chapter, we added our SenseHat which contains six types of sensors, and we made use of the humidity, barometric, and joystick sensors. This allowed us to have a more intelligent Pi. In this section, we are going to add an additional peripheral, the camera. The camera is probably the most important peripheral when writing an application whose main feature is video and still capture.
Joseph Faisal Nusairat
Chapter 11. Integration
Abstract
In this second to last chapter, we will be going over everything to do with finishing the integration. We are not adding any new sensors to the Pi or any drastic functionality. What we are adding is the ability to interact with these components as a unit. This chapter becomes very important, because without it, we have many random one-off applications. This will tie all these pieces into a more cohesive unit.
Joseph Faisal Nusairat
Chapter 12. Final Thoughts
Abstract
We have touched on many topics in this book that have ranged the gauntlet in creating a full cycle IoT application in Rust. This application uses many crates, which are in various phases that I can’t control and may not be compatible with future versions of Rust. My main goal for this book was to help put together a path to creating an IoT application and showing you how to make all the crates needed work together, since often I’ve found that the biggest challenge when creating a larger application is to get everything to play well together. Even our small application with a scope of video and a SenseHat still required quite a bit of work in the front and back to make it work. As we are at the end, I wanted to touch on one final topic. In Chapter 7, we covered deploying the application; I wanted to end with discussing how to deploy the binary.
Joseph Faisal Nusairat
Backmatter
Metadata
Title
Rust for the IoT
Author
Joseph Faisal Nusairat
Copyright Year
2020
Publisher
Apress
Electronic ISBN
978-1-4842-5860-6
Print ISBN
978-1-4842-5859-0
DOI
https://doi.org/10.1007/978-1-4842-5860-6

Premium Partner