Internet of Things Programming Projects: Build exciting IoT projects using Raspberry Pi 5, Raspberry Pi Pico, and Python
By Colin Dow
()
Related to Internet of Things Programming Projects
Related ebooks
BeagleBone By Example Rating: 0 out of 5 stars0 ratingsAugmented Reality with Kinect Rating: 0 out of 5 stars0 ratingsAn iOS Developer's Guide to SwiftUI: Design and build beautiful apps quickly and easily with minimum code Rating: 0 out of 5 stars0 ratingsCoding with ChatGPT and Other LLMs: Navigate LLMs for effective coding, debugging, and AI-driven development Rating: 0 out of 5 stars0 ratingsGetting Started with UDOO Rating: 0 out of 5 stars0 ratingsThe FPGA Programming Handbook: An essential guide to FPGA design for transforming ideas into hardware using SystemVerilog and VHDL Rating: 0 out of 5 stars0 ratingsUltimate Robotics Programming with ROS 2 and Python Rating: 0 out of 5 stars0 ratingsAlgorithmic Probability: Fundamentals and Applications Rating: 0 out of 5 stars0 ratingsApplied Machine Learning Solutions with Python: SOLUTIONS FOR PYTHON, #1 Rating: 0 out of 5 stars0 ratingsBuilding Modern GUIs with tkinter and Python: Building user-friendly GUI applications with ease (English Edition) Rating: 0 out of 5 stars0 ratingsWhy AI Hallucinates: The BotVerse Begins Rating: 0 out of 5 stars0 ratingsRoblox Game Development: From Zero To Proficiency (Intermediate): Roblox Game Development: From Zero To Proficiency, #1 Rating: 0 out of 5 stars0 ratingsFeedforward Neural Networks: Fundamentals and Applications for The Architecture of Thinking Machines and Neural Webs Rating: 0 out of 5 stars0 ratingsAI For Amateurs: Artificial Intelligence, #1 Rating: 0 out of 5 stars0 ratingsWebAssembly Essentials Rating: 0 out of 5 stars0 ratingsA Joosr Guide to... Chaos Monkeys by Antonio García Martínez: Obscene Fortune and Random Failure in Silicon Valley Rating: 0 out of 5 stars0 ratingsSwarm Intelligence: Fundamentals and Applications Rating: 0 out of 5 stars0 ratingsAlgorithms and Parallel Computing Rating: 0 out of 5 stars0 ratingsReact and React Native: Build cross-platform JavaScript and TypeScript apps for the web, desktop, and mobile Rating: 0 out of 5 stars0 ratingsEnhancing Deep Learning Performance Using Displaced Rectifier Linear Unit Rating: 0 out of 5 stars0 ratingsRobot Operating System: Mastering Autonomous Systems for Seamless Integration and Control Rating: 0 out of 5 stars0 ratingsThe Lindahl Letter: 3 Years of AI/ML Research Notes Rating: 0 out of 5 stars0 ratingsNamed Entity Recognition: Fundamentals and Applications Rating: 0 out of 5 stars0 ratingsIan Talks Unix Tools A-Z: ToolsAtoZ, #2 Rating: 0 out of 5 stars0 ratingsRobotics And Virtuality: How The Future Will Be Transformed By Robotics And Virtual Reality Rating: 0 out of 5 stars0 ratingsTailor Made Video Games Rating: 0 out of 5 stars0 ratings
Hardware For You
Chip War: The Fight for the World's Most Critical Technology Rating: 4 out of 5 stars4/5Build Your Own PC Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5CompTIA A+ Complete Review Guide: Exam Core 1 220-1001 and Exam Core 2 220-1002 Rating: 5 out of 5 stars5/5Unlock Any Roku Device: Watch Shows, TV, & Download Apps Rating: 0 out of 5 stars0 ratingsExploring Arduino: Tools and Techniques for Engineering Wizardry Rating: 4 out of 5 stars4/5iPhone For Dummies Rating: 0 out of 5 stars0 ratingsCompTIA A+ Complete Review Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 5 out of 5 stars5/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5Electrical Engineering | Step by Step Rating: 0 out of 5 stars0 ratingsmacOS Sonoma For Dummies Rating: 0 out of 5 stars0 ratingsLinux All-in-One For Dummies Rating: 3 out of 5 stars3/5Linux All-In-One For Dummies Rating: 2 out of 5 stars2/5Amazon Web Services (AWS) Interview Questions and Answers Rating: 5 out of 5 stars5/5Practical Digital Forensics Rating: 0 out of 5 stars0 ratingsUpgrading and Fixing PCs For Dummies Rating: 0 out of 5 stars0 ratingsBurners and Black Markets Rating: 0 out of 5 stars0 ratingsCreative Selection: Inside Apple's Design Process During the Golden Age of Steve Jobs Rating: 5 out of 5 stars5/5iPhone For Seniors For Dummies Rating: 4 out of 5 stars4/5Mastering ChatGPT Rating: 0 out of 5 stars0 ratingsHow to Hack Like a GOD: Hacking the Planet, #2 Rating: 5 out of 5 stars5/5Chromebook For Dummies Rating: 5 out of 5 stars5/5Apple Watch For Dummies Rating: 0 out of 5 stars0 ratingsHacking Electronics: Learning Electronics with Arduino and Raspberry Pi, Second Edition Rating: 0 out of 5 stars0 ratingsiPhone X Hacks, Tips and Tricks: Discover 101 Awesome Tips and Tricks for iPhone XS, XS Max and iPhone X Rating: 3 out of 5 stars3/5iPhone For Seniors For Dummies: Updated for iPhone 12 models and iOS 14 Rating: 4 out of 5 stars4/5Arduino Essentials Rating: 5 out of 5 stars5/5The Android Smartphone & Tablet Course Rating: 0 out of 5 stars0 ratings
Reviews for Internet of Things Programming Projects
0 ratings0 reviews
Book preview
Internet of Things Programming Projects - Colin Dow
Internet of Things Programming Projects
Copyright © 2024 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capital. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Preet Ahuja
Publishing Product Manager: Vidhi Vashisth
Book Project Manager: Uma Devi
Senior Editor: Mohd Hammad
Technical Editor: Rajat Sharma
Copy Editor: Safis Editing
Proofreader: Mohd Hammad
Indexer: Tejal Soni
Production Designer: Aparna Bhagat
DevRel Marketing Coordinator: Sharon Sandhya
Senior DevRel Marketing Coordinator: Rohan Dobhal
First published: October 2018
Second Edition: June 2024
Production reference: 1070624
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK
ISBN 978-1-83508-295-9
www.packtpub.com
I would like to thank my wife, Constance, for her encouragement, support, and assistance, without which this book would not be possible. I would also like to thank my sons, Maximillian and Jackson, for making me the proudest father there could be.
– Colin Dow
Contributors
About the author
Colin Dow has been involved with technology since the early home computers caught his eye. He has worked as a programmer/analyst at some of Canada’s biggest companies. He is the author of the Packt books Internet of Things Programming Projects (2018), Hands-On Edge Analytics with Azure IoT, and Simplifying 3D Printing with OpenSCAD. When he’s not nerding out on programming or 3D printing, he likes to compose electronic music, which can be found on Spotify under the name Project Josephine.
About the reviewers
Muhammad Afzal is a senior software engineer, with more than 14 years of experience working on web-based and IoT systems in multinational organizations. He always enjoys working and solving real-world business problems with technology. He is also a book author and has published the book Arduino IoT Cloud for Developers with Packt Publishing, as well as working with EC-Council for the CodeRed project, where he develops courses regarding IoT.
He provides freelance services to IoT-based product companies, writing technical reviews and projects, as well as providing consultancy to organizations.
I would like to express my heartfelt gratitude to my parents (Mr. and Mrs. Muhammad Aslam), my wife, and my children; they have all always supported me in my decisions, especially my brothers and sisters. And to the memory of my grandfather, Muhammad Ameer, for his sacrifices and exemplifying the power of determination.
Jackson Dow is currently working toward a Bachelor of Science at Wilfrid Laurier University in Waterloo, Ontario, Canada. A computer science major, Jackson has a strong interest in technology and has developed phone apps, soon to be available in the App Store. He grew up in Brampton, Ontario, playing both rep hockey and baseball, and he achieved his French immersion certificate. He is grateful for the opportunity to participate in the review of this exciting technical publication.
I would like to thank Colin Dow for writing a book that was not only easy to follow but also interesting and informative. I would also like to thank the Book Project Manager and Senior Editor for making the reviewing process easier for me.
Table of Contents
Preface
Part 1: Setting Up the Raspberry Pi for IoT Development
1
Understanding the Raspberry Pi
Technical requirements
Exploring Raspberry Pi models
Exploring alternatives to the Raspberry Pi
Looking at the power of HATs
Pibrella HAT
The Raspberry Pi Sense HAT
Investigating operating systems for the Raspberry Pi
Using the Raspberry Pi for IoT
Utilizing web services for IoT applications
Re-introducing T.A.R.A.S. – an IoT-based robotics project
Getting started with Raspberry Pi development
Raspberry Pi development tools
Raspberry Pi and Sense HAT development
Summary
2
Harnessing Web Services with the Raspberry Pi
Technical requirements
Exploring web services
Understanding approaches for web services
Connecting to a web service with our Raspberry Pi and Sense HAT
Creating a scrolling stock ticker application
Getting an API key
Writing web services client code
Enhancing our application
Developing weather display applications
Getting an API key
Creating a scrolling weather information ticker
Scrolling weather information on Sense HAT
Developing a GO-NO-GO application for decision-making
Building other GO-NO-GO applications
Summary
3
Building an IoT Weather Indicator
Technical requirements
Looking into servo motors
Connecting the SG90 servo motor to our Raspberry Pi
Understanding servo motors
Exploring LEDs
Connecting an LED to our Raspberry Pi
Controlling servo motors and LEDs using Python
Setting up our development environment
Using GPIO Zero to control a servo
Using GPIO Zero to control an LED
Building the weather indicator stand
Assembling the weather indicator stand
Developing code for our application
Calibrating the needle
Creating the WeatherData class
Creating the WeatherDashboard class
Adding the updateDashboard() function and main methods
Summary
4
Building an IoT Information Display
Technical requirements
Investigating displays compatible with our Raspberry Pi and exploring screen types
Creating an IoT information display
Setting up our development environment
Creating a WeatherData class
Creating a TrafficMap class
Adding Dashboard and MyApp classes
Running the IoT information display application
Summary
Part 2: Building an IoT Home Security Dashboard
5
Exploring the GPIO
Technical requirements
Introducing the GPIO on Raspberry Pi
Exploring the Raspberry Pi GPIO pinout diagram
Understanding GPIO pin communication protocols
Understanding sensors, actuators, and indicators
Setting up our development environment
Exploring the PIR sensor
Building a simple alarm system
Summary
6
Building an IoT Alarm Module
Technical requirements
Investigating MQTT
Understanding the publish-subscribe model in MQTT
Understanding QoS in MQTT
Exploring MQTT fundamentals with the MQTTHQ web client
Using a Raspberry Pi Pico W with MQTT
Introducing the RP2040 chip
Configuring our alarm circuit
Setting up our development environment
Writing the alarm module client code
Building an IoT alarm module case
Identifying the parts of the custom case
Building the alarm module case
Summary
7
Building an IoT Button
Technical requirements
Introducing IoT buttons
Utilizing IoT buttons
Exploring various technologies in IoT button development
Creating our IoT button using the M5Stack ATOM Matrix
Exploring M5Stack devices
Flashing the firmware to our ATOM Matrix
Configuring the ATOM Matrix for programming
Turning our ATOM Matrix into an IoT button
Testing our IoT button
Improving on our IoT button with the Raspberry Pi Pico W
Setting up a CloudAMQP instance
Modifying our alarm module code
Building our Raspberry Pi Pico W IoT button
Installing the components in a custom case
Summary
8
Creating an IoT Alarm Dashboard
Technical requirements
Exploring IoT alarm dashboards
Using IoT alarm dashboards for industrial processes
Exploring the IoT security alarm dashboard
Creating a Raspberry Pi 5 alarm dashboard
Modifying the IoT alarm module code
Writing the dashboard code
Building the external alarm buzzer stand
Identifying the parts
Building the stand
Running our application
Summary
Part 3: Creating a LoRa-Enabled IoT Monitoring Station
9
Understanding LoRa
Technical requirements
Exploring LoRa
Practical uses for LoRa technology
Investigating the radio frequency spectrum
Understanding the LoRa SF
Using LoRa with the Raspberry Pi Pico and Pico W
Building a LoRa sensory transmitter
Constructing our circuit
Developing the code
Installing the components in a custom case
Building a LoRa receiver
Wiring an LED to the Raspberry Pi Pico W
Creating code to receive LoRa messages
Testing our application
Summary
10
Integrating LoRa with the Internet
Technical requirements
Connecting our LoRa receiver to the internet
Installing the CircuitPython library for MQTT
Creating a CloudAMQP instance for our application
Adding MQTT functionality to the LoRa receiver
Creating a new weather indicator
Building the split stand
Building the faceplate
Configuring the RGB LED indicator
Configuring the servo motor
Programming our weather indicator
Exploring other IoT communication protocols
Summary
Part 4: Building an IoT Robot Car
11
Introducing ROS
Technical requirements
Exploring ROS
Reviewing our TurtleSim controller ROS application
Understanding ROS node communication
Investigating ROS project structure and organization
Aligning ROS distributions with Ubuntu LTS versions
Installing Ubuntu and ROS onto our Raspberry Pi
Installing Ubuntu on our Raspberry Pi 4
Adding ROS to our Ubuntu installation
Testing our ROS installation
Running and controlling a simulated robot
Launching and testing TurtleSim
Creating an ROS workspace and package
Modifying the generated Python code
Updating package.xml
Compiling and running our code
Controlling our robot with an MQTT message
Summary
12
Creating an IoT Joystick
Technical requirements
Understanding our IoT joystick application
Wiring up our circuit
Developing the code for our IoT joystick
Setting up our Raspberry Pi Pico WH
Creating a Joystick class
Sending MQTT messages from our IoT joystick
Creating a custom ROS node for our application
Creating our custom robot_control node
Controlling a ROS TurtleSim robot using our IoT joystick
Constructing the IoT joystick case
Summary
13
Introducing Advanced Robotic Eyes for Security (A.R.E.S.)
Technical requirements
Exploring our A.R.E.S. application
Constructing A.R.E.S.
Identifying the 3D-printed frame parts
Identifying the components used to create A.R.E.S.
Building A.R.E.S.
Wiring up A.R.E.S.
Software setup and configuration
Installing Ubuntu onto our Raspberry Pi 3B+
Running the installation script
Creating alarm code for the Pico H
Testing and controlling the motors
Testing communication between Pi and Pico
Testing the ToF sensor
Streaming video from A.R.E.S.
Programming A.R.E.S. with ROS
Summary
14
Adding Computer Vision to A.R.E.S.
Technical requirements
Exploring computer vision
Introducing OpenCV
Understanding YOLO and neural networks
Exploring object detection
Adding computer vision to A.R.E.S.
Creating the DogTracker class
Building a smart video streamer
Sending out a text alert
Setting up our Twilio account
Adding text message functionality to A.R.E.S.
Summary
Index
Other Books You May Enjoy
Preface
Internet of Things Programming Projects is a comprehensive hands-on guide, designed to walk you through a series of progressively advanced Internet of Things (IoT) projects using Raspberry Pi and associated peripherals.
In the initial stages, you will be introduced to the foundational concepts and components surrounding the Raspberry Pi, including a detailed exploration of various models, accessories such as hardware attached on top (HAT), and compatible operating systems. You will begin your journey into IoT by engaging in projects that demonstrate the Raspberry Pi’s capacity to interface with real-time data and control physical devices, setting the stage for more advanced undertakings.
As you proceed, you will immerse yourself in the development of web services and IoT applications, creating real-time data displays and innovative solutions such as a weather indicator application. These applications not only provide Python programming and data acquisition skills but also venture into the physical world, utilizing hardware components to create tangible outcomes.
In the middle portion of the book, emphasis is laid on building a home security system from scratch. You will learn about the general-purpose input/output (GPIO) pins of the Raspberry Pi, sensor integrations, and hands-on alarm system development, using protocols such as message queuing telemetry transport (MQTT). A pivotal point will be reached when we create an MQTT-based standalone IoT alarm module and its associated peripherals.
Toward the latter part of the journey, the narrative progresses to more advanced and autonomous IoT projects. You will construct a LoRa-enabled IoT monitoring station capable of measuring various environmental factors, including air quality. This station, powered by a battery, communicates data through LoRa to a LoRaWAN network, illustrating the integration of IoT devices with wider network infrastructures and emphasizing sustainable, battery-operated solutions.
Culminating the series of projects is the creation of an IoT robot car, leveraging the Robot Operating System (ROS). This advanced endeavor guides you in building a robot car equipped to send sensory information over the internet through MQTT, facilitating remote control via a web browser or other applications. This project represents a pinnacle of IoT mastery, incorporating robotics, network communications, and remote control functionalities into a unified system.
Throughout the book, you will be empowered with the knowledge and skills to build practical, real-world IoT solutions, nurturing creativity and innovation through continuous project enhancement suggestions. From Python programming to hardware interfacing, this book promotes a rich, layered understanding of IoT principles, encouraging a readiness to tackle a myriad of complex problems in the IoT landscape, and stands as a beacon for aspiring IoT enthusiasts to develop robust, versatile, and innovative IoT solutions.
Let’s get into it!
Who this book is for
Internet of Things Programming Projects is geared toward tech enthusiasts, hobbyists, and professionals who are eager to dive into the world of IoT. The book covers a range of topics, including web services, LoRa communication, Raspberry Pi, Raspberry Pi Pico, and interacting with GPIO. You will also learn about ROS, building a robot car, and implementing vision recognition. To get the most out of this book, you should have a basic understanding of programming, electronics, and networking. This comprehensive guide is ideal for those looking to expand their knowledge and skills in IoT by engaging in practical, hands-on projects.
What this book covers
Chapter 1
, Understanding the Raspberry Pi, explores the Raspberry Pi before we embark on creating IoT projects using it.
Chapter 2
, Harnessing Web Services with the Raspberry Pi, delves into writing Python code to transform our Raspberry Pi into an IoT device, by leveraging web services to pull data and create visual displays on the Sense HAT. This lays the groundwork for more advanced IoT web services development, through practical projects such as a stock ticker, weather display, and decision-maker applications.
Chapter 3
, Building an IoT Weather Indicator, explores the integration of servo motors and LEDs with the Raspberry Pi to create a practical IoT weather indicator, leveraging its precision, control, and real-time feedback capabilities for enhanced system functionality.
Chapter 4
, Building an IoT Information Display, shows you how to build an IoT information display, using the Raspberry Pi-branded 7-inch touchscreen to show real-time weather and local traffic information. The chapter starts with an exploration of compatible screens and culminating in a comprehensive dashboard project.
Chapter 5
, Exploring the GPIO, dives deeper into the GPIO port functionality on both the Raspberry Pi and Raspberry Pi Pico by constructing an IoT home security application, including a basic alarm system with a passive infrared sensor (PIR) motion sensor, pushbutton, and buzzer.
Chapter 6
, Building an IoT Alarm Module, looks at enhancing our basic alarm system by using a Raspberry Pi Pico W, a public MQTT server, and the MQTTHQ web client to build an IoT alarm module, where motion detection triggers messages and remote buzzer activation. This will form the foundation of our IoT home security system.
Chapter 7
, Building an IoT Button, shows you how to build an essential component of our IoT home security system, the IoT button, using both the compact M5Stack ATOM Matrix and the versatile Raspberry Pi Pico W for different versions of the button.
Chapter 8
, Creating an IoT Alarm Dashboard, covers using a Raspberry Pi 5 with a 7-inch touchscreen to create an IoT alarm dashboard, allowing us to arm and disarm the alarm module, review MQTT notifications, and display a map of alarm locations, thereby completing our advanced IoT alarm system with global application capabilities.
Chapter 9
, Understanding LoRa, explores LoRa (short for Long Range) technology for IoT communication, its applications in agriculture and smart cities, and how to build a LoRa sensory transmitter and receiver using Raspberry Pi Pico and Pico W, respectively, demonstrating LoRa’s extensive range capabilities and efficient low-power data transmission.
Chapter 10
, Integrating LoRa with the Internet, shows you how to use our Raspberry Pi Pico W-equipped LoRa receiver to publish sensory data from a remote LoRa transmitter to an MQTT server, modify our analog-metered weather indicator to utilize this data, and explore various IoT communication technologies such as LoRaWAN and cellular.
Chapter 11
, Introducing ROS, introduces the ROS, detailing its significance in robotics, setting it up on a Raspberry Pi 4 with Ubuntu, and using turtlesim to learn basic ROS concepts and operations, ultimately preparing us to build the advanced IoT robot A.R.E.S. (Advanced Robot Eyes for Security).
Chapter 12
, Creating an IoT Joystick, shows you how to create an IoT joystick using a Raspberry Pi Pico WH. You will use it to remotely control a ROS TurtleSim robot, building on previous projects and demonstrating IoT’s practical application in robotics.
Chapter 13
, Introducing Advanced Robotic Eyes for Security (A.R.E.S.), focuses on converting our TurtleSim virtual robot into a real-life robot called ARES, which features a video feed accessible via the VLC media player and is controlled by the IoT Joystick from Chapter 12. We will use a Raspberry Pi for sensory input and a Raspberry Pi Pico for motor, LED, and buzzer control, with a 3D-printed frame.
Chapter 14
, Adding Computer Vision to A.R.E.S., finally sees us add computer vision to ARES, enabling it to recognize objects and send text alerts. We will also use OpenCV and the You Only Look Once (YOLO) object detection system to build a smart video streaming application.
To get the most out of this book
You should have some experience with Python and JavaScript programming. Important skills include being able to work with Raspberry Pi, Python, and web services to create IoT applications, as well as engaging in Raspberry Pi-controlled robotics.
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
Download the example code files
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Internet-of-Things-Programming-Projects-2nd-Edition
. If there’s an update to the code, it will be updated in the GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/
. Check them out!
Conventions used
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: We then define an activate_buzzer() function.
A block of code is set as follows:
def timer_callback(self):
if self.mqtt_message.should_draw_circle:
self.vel_msg.linear.x = 1.0
self.vel_msg.angular.z = 1.0
else:
self.vel_msg.linear.x = 0.0
self.vel_msg.angular.z = 0.0
self.publisher.publish(self.vel_msg)
Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: We do so by clicking on the Menu icon in the Raspberry Pi taskbar, navigating to the Programming category, and selecting Thonny.
Tips or important notes
Appear like this.
Get in touch
Feedback from you is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected]
and mention the book title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata
and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected]
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com
.
Share Your Thoughts
Once you’ve read Internet of Things Programming Projects, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page
for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Download a free PDF copy of this book
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily
Follow these simple steps to get the benefits:
Scan the QR code or visit the link below
https://packt.link/free-ebook/9781835082959
Submit your proof of purchase
That’s it! We’ll send your free PDF and other benefits to your email directly
Part 1: Setting Up the Raspberry Pi for IoT Development
In Part 1, we begin by understanding the Raspberry Pi, and then we will harness web services for IoT applications, build an IoT weather indicator using sensors and web data, and create an IoT information display with a Raspberry Pi and touchscreen for real-time weather and traffic updates.
This part has the following chapters:
Chapter 1
, Understanding the Raspberry Pi
Chapter 2
, Harnessing Web Services with the Raspberry Pi
Chapter 3
, Building an IoT Weather Indicator
Chapter 4
, Building an IoT Information Display
1
Understanding the Raspberry Pi
Welcome to the wonderful world of Internet of Things (IoT) projects and the Raspberry Pi in this, the second edition of Internet of Things Programming Projects.
In this book, we will take a journey into IoT projects using the Raspberry Pi. In the first part of this book, we’ll explore IoT projects on the Raspberry Pi, initially transforming it into a weather station with the Sense HAT to capture real-time data. Later, we’ll use motors and general-purpose input/output (GPIO) pins to repurpose the Pi as an analog metering device.
We will then create an IoT home security system, using the Raspberry Pi as the central hub for an alarm system. We’ll also develop a LoRa-enabled IoT monitoring station for remote sensing. The book culminates with our most ambitious project: building an IoT robot car powered by the Raspberry Pi.
We will start this chapter by exploring the various Raspberry Pi models and their significance, observing the evolution and advancements in processing power, memory, and capabilities over time.
We will also look at alternatives to the Raspberry Pi available in the IoT landscape, enabling us to make informed decisions based on our project requirements. We compare these alternatives with the Raspberry Pi, highlighting their unique specifications and capabilities.
Furthermore, we dive into Raspberry Pi’s Hardware Attached on Top (HAT), which expands the Raspberry Pi’s capabilities through add-on boards. Specifically, we explore the Pibrella HAT and the Raspberry Pi Sense HAT, highlighting their ability to enhance the Raspberry Pi with additional features and functionalities.
Our Sense HAT projects will make use of an optional custom Raspberry Pi case we see in Figure 1.1. This case allows us to display our Raspberry Pi in a vertical format and make use of the dot matrix display on the Sense HAT. The build files for the Raspberry Pi 4B and Raspberry Pi 5 versions’ case are available in the GitHub repository for the book.
Figure 1.1 – Raspberry Pi 4B and Sense HAT in a custom caseFigure 1.1 – Raspberry Pi 4B and Sense HAT in a custom case
We will also discuss a range of operating systems compatible with the Raspberry Pi beyond the official Raspberry Pi OS. These options cater to various applications, including networked audio systems, aviation-related projects, retro gaming, and 3D printing.
In the context of IoT applications, we will highlight the Raspberry Pi’s versatility and power as it serves as an optimal platform for processing real-time data and controlling physical devices, pivotal in the development and deployment of versatile IoT projects.
Finally, we will explore the Raspberry Pi as a robust development platform, equipped with pre-installed tools for software development. We conclude the chapter with a series of programming projects using the Raspberry Pi with the Sense HAT to extract sensory information as we build a scrolling environmental data display.
Our hands-on dive into programming in this chapter will fine-tune our programming abilities and ready us for exciting IoT project development throughout this book. Although there is a lot of information that is covered in this chapter, we should not worry if we feel overwhelmed or if we can’t digest all the information from the first chapter right away. As we progress through the book, we will gain more experience and understanding, making it easier to grasp the concepts introduced early on.
We will cover the following:
Exploring Raspberry Pi models
Exploring alternatives to the Raspberry Pi
Looking at the power of HATs
Investigating operating systems for the Raspberry Pi
Using the Raspberry Pi for IoT
Getting started with Raspberry Pi development
Technical requirements
The following are required to complete this chapter:
Late-model Raspberry Pi, such as the Raspberry Pi 5 4/8 GB model or Raspberry Pi 4B 4/8 GB model
Keyboard, mouse, and monitor
Raspberry Pi Sense HAT is optional but encouraged; we will be making use of the Sense HAT software emulator
The GitHub repository for the chapter is located at https://github.com/PacktPublishing/-Internet-of-Things-Programming-Projects-2nd-Edition/tree/main/Chapter1
.
For those that have access to a 3D printer or 3D printing service, there are .stl files available in the Build Files directory of this chapter’s GitHub repository for the construction of the optional custom case. Files are available for the Raspberry Pi 5 and Raspberry Pi 4B versions of the case.
A general knowledge of programming is also required. We will be using the Python programming language in this book. We will start with basic code and work our way toward more advanced coding as we go through the projects in the book.
Exploring Raspberry Pi models
Every Raspberry Pi model, from the initial Raspberry Pi 1 to the current Raspberry Pi 5 and the compact Pi Zero, possesses distinct characteristics and functionalities that make it an exciting choice for IoT developers. Each model excels in different areas, such as processing power, size, and energy efficiency.
The Raspberry Pi models have evolved significantly over time, with each iteration bringing notable advancements in processing power, memory, and capabilities.
In the following list, we compare the various models of the Raspberry Pi, starting with the first one:
Raspberry Pi 1: Launched in February 2012, the Raspberry Pi 1 changed the world of computing with its affordability and accessibility. Equipped with a 700 MHz processor, 512 MB RAM, and a $35 price point, the first model of the Raspberry Pi spurred digital innovation and highlighted the potential of single-board computers.
Raspberry Pi 2: Released in February 2015, the Raspberry Pi 2 improved on the first model with a 900 MHz quad-core processor and doubled RAM at 1 GB. The Raspberry Pi 2 also expanded the GPIO from 26 to 40 pins, allowing for a new wave of 40-pin HATs. These advancements allowed the Raspberry Pi 2 to become a hub for complex projects, from robotics to IoT applications.
Raspberry Pi 3: Released in February 2016, the Raspberry Pi 3 sported a 1.2 GHz quad-core processor. This improved performance by 50-60% and enabled more resource-intensive applications. Like the Raspberry Pi 2, it maintained 1 GB RAM. Wi-Fi and Bluetooth 4.1 were integrated, simplifying connectivity and freeing USB ports (a USB Wi-Fi dongle was required on the Raspberry Pi 2). A new dual-core VideoCore IV GPU enhanced multimedia projects with improved video capabilities. Figure 1.2 provides a layout of a Raspberry Pi 3, highlighting several of its key components:
Figure 1.2 – Raspberry Pi 3BFigure 1.2 – Raspberry Pi 3B
Raspberry Pi 4: Unveiled in June 2019, the Raspberry Pi 4 marked a significant evolution in the series, pushing the boundaries of single-board computers closer to conventional desktop PCs in terms of capabilities, all while preserving its compact size and affordability. What set the Pi 4 apart was the variety of memory options it offered, 2 GB, 4 GB, and 8 GB LPDDR4-3200 SDRAM, a substantial improvement over the previous 1 GB LPDDR2 RAM, enabling smoother multitasking and handling of data-intensive tasks. Improved connectivity featured Gigabit Ethernet, dual-band 802.11ac Wi-Fi, and Bluetooth 5.0. Its multimedia capabilities saw a boost with two micro-HDMI ports supporting 4K resolution, allowing the operation of two monitors at once. The Raspberry Pi 4 introduced two USB 3.0 ports for quicker data transfer and replaced the micro-USB power connector with a USB-C, supporting its enhanced features. In Figure 1.3, we see a layout of a Raspberry Pi 4 with several of its key components highlighted:
Figure 1.3 – Raspberry 4BFigure 1.3 – Raspberry 4B
The Raspberry Pi 4 includes two micro-HDMI ports (for dual displays), four USB ports (two 3.0, two 2.0), a Gigabit Ethernet port, a USB-C power port, a micro-SD slot, a camera port, and a 3.5mm audio-composite video jack.
Raspberry Pi 5: The Raspberry Pi 5, released in October 2023, marks a significant advancement in the series, enhancing computational and multimedia capabilities for educational and DIY applications with its upgraded CPU and GPU.
Priced at $60 for 4 GB and $80 for 8 GB, the Raspberry Pi 5 features a 2.4GHz quad-core Arm Cortex-A76 CPU, VideoCore VII GPU, dual 4Kp60 HDMI outputs, and various connectivity options including Wi-Fi and Bluetooth. It also introduces a power button, enhanced memory, and I/O capabilities, including two four-lane Mobile Industry Processor Interface (MIPI) camera/display transceivers. These transceivers offer the flexibility to connect any combination of two cameras or displays, making them ideal for advanced multimedia projects.
The Raspberry Pi 5 also features a PCIe 2.0 x1 interface, allowing the connection of fast peripherals to expand its capabilities for advanced applications, such as high-speed networking or storage solutions.
The accompanying Raspberry Pi Active Cooler, with its efficient heatsink and fan design, reduces CPU temperatures by approximately 20 degrees Celsius, vital for intensive tasks. It offers easy installation and up to 8000 RPM fan speed, enhancing performance and longevity. In Figure 1.4, the Raspberry Pi 5 is displayed alongside its Active Cooler, with key components such as the PCIe 2.0 Interface, GPIO Header, USB Ports, Ethernet Port, dual micro-HDMI Ports, and the two MIPI Transceivers labeled:
Figure 1.4 – Raspberry Pi 5 (right) and Raspberry Pi Active Cooler (left)Figure 1.4 – Raspberry Pi 5 (right) and Raspberry Pi Active Cooler (left)
We will feature the Raspberry Pi 5 for our projects that involve a single-board computer, although the late-model Raspberry Pi 4B should suffice too.
Raspberry Pi Zero and Zero W: Launched in November 2015, the Raspberry Pi Zero shrank the Raspberry Pi to a size of