Hands-on ESP32 with Arduino IDE: Unleash the power of IoT with ESP32 and build exciting projects with this practical guide
()
About this ebook
ESP32 is a versatile microcontroller and a great starting point for anyone venturing into the IoT realm, but its configuration and interfacing of sensors can be challenging for new users. Arduino Integrated Development Environment (IDE) simplifies programming, uploading code, and utilization of ESP32 capabilities, enabling users to incorporate it into their IoT projects with ease. This book will help you learn the essentials of sensing, networking, data processing, and applications with ESP32, laying a strong foundation for further IoT development.
Starting with ESP32 and Arduino Ide 2.0 basics, you'll first explore practical implementation examples of interfacing sensors with ESP32. These examples will also teach you how to interface the ESP32 camera and display modules with ESP32. As you progress, you’ll get to grips with IoT network and data protocols, as well as the many options they unlock within IoT applications. The book will also help you leverage your newly acquired knowledge with exciting projects ranging from smart connected devices to data loggers and automation.
By the end of this book, you'll confidently navigate ESP32 projects with newfound knowledge and skills, know what IoT protocol to select for your applications, and successfully build and deploy your own IoT projects.
Related to Hands-on ESP32 with Arduino IDE
Related ebooks
Mastering IoT For Industrial Environments: Unlock the IoT Landscape for Industrial Environments with Industry 4.0, Covering Architecture, Protocols like MQTT, and Advancements with ESP-IDF Rating: 0 out of 5 stars0 ratingsLaw and Digital Technologies - The Way Forward Rating: 0 out of 5 stars0 ratingsIntel Edison Projects: Build exciting IoT projects with Intel Edison Rating: 0 out of 5 stars0 ratingsDeveloping IoT Projects with ESP32: Automate your home or business with inexpensive Wi-Fi devices Rating: 0 out of 5 stars0 ratingsInternet of Things with Arduino Blueprints Rating: 0 out of 5 stars0 ratingsDeveloping IoT Projects with ESP32: Unlock the full Potential of ESP32 in IoT development to create production-grade smart devices Rating: 0 out of 5 stars0 ratingsLearn Arduino Prototyping in 10 days Rating: 0 out of 5 stars0 ratingsInternet of Things from Scratch: Build IoT solutions for Industry 4.0 with ESP32, Raspberry Pi, and AWS Rating: 0 out of 5 stars0 ratingsHands-On Industrial Internet of Things: Create a powerful Industrial IoT infrastructure using Industry 4.0 Rating: 0 out of 5 stars0 ratingsFundamentals of IoT: Get familiar with the building blocks of IoT (English Edition) Rating: 0 out of 5 stars0 ratingsBuilding Wireless Sensor Networks Using Arduino Rating: 4 out of 5 stars4/5Arduino: Building LED and Espionage Projects Rating: 0 out of 5 stars0 ratingsArduino Data Communications: Learn how to configure databases, MQTT, REST APIs, and store data over LoRaWAN, HC-12, and GSM Rating: 0 out of 5 stars0 ratingsArduino Android Blueprints Rating: 0 out of 5 stars0 ratingsInternet of Things with ESP8266 Rating: 5 out of 5 stars5/5Practical Contiki-NG: Programming for Wireless Sensor Networks Rating: 0 out of 5 stars0 ratingsRapid BeagleBoard Prototyping with MATLAB and Simulink Rating: 0 out of 5 stars0 ratingsHands-On Internet of Things with MQTT: Build connected IoT devices with Arduino and MQ Telemetry Transport (MQTT) Rating: 0 out of 5 stars0 ratingsMastering Linux Device Driver Development: Write custom device drivers to support computer peripherals in Linux operating systems Rating: 0 out of 5 stars0 ratingsRaspberry Pi IoT Projects: Prototyping Experiments for Makers Rating: 0 out of 5 stars0 ratingsIntelligent IoT Projects in 7 Days: Build exciting projects using smart devices Rating: 0 out of 5 stars0 ratingsLearning C for Arduino: A comprehensive guide that will help you ace C's fundamentals using the powerful Arduino board Rating: 0 out of 5 stars0 ratingsCommercial and Industrial Internet of Things Applications with the Raspberry Pi: Prototyping IoT Solutions Rating: 0 out of 5 stars0 ratingsIntelligence Devices for Smart World Rating: 0 out of 5 stars0 ratingsLearn IoT Programming Using Node-RED: Begin to Code Full Stack IoT Apps and Edge Devices with Raspberry Pi, NodeJS, and Grafana Rating: 0 out of 5 stars0 ratingsInternet of Things with Raspberry Pi 3: Leverage the power of Raspberry Pi 3 and JavaScript to build exciting IoT projects Rating: 0 out of 5 stars0 ratingsPractical Arduino Robotics: A hands-on guide to bringing your robotics ideas to life using Arduino Rating: 0 out of 5 stars0 ratings
Networking For You
Networking For Dummies Rating: 5 out of 5 stars5/5The Compete Ccna 200-301 Study Guide: Network Engineering Edition Rating: 5 out of 5 stars5/5Nmap Essentials Rating: 4 out of 5 stars4/5CompTIA Network+ Practice Tests: Exam N10-008 Rating: 0 out of 5 stars0 ratingsCybersecurity: The Beginner's Guide: A comprehensive guide to getting started in cybersecurity Rating: 5 out of 5 stars5/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5A Beginner's Guide to Ham Radio Rating: 0 out of 5 stars0 ratingsCompTIA Network+ Certification Guide (Exam N10-008): Unleash your full potential as a Network Administrator (English Edition) Rating: 0 out of 5 stars0 ratingsLinux Bible Rating: 0 out of 5 stars0 ratingsThe Windows Command Line Beginner's Guide: Second Edition Rating: 4 out of 5 stars4/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5Windows Command Line Administration Instant Reference Rating: 0 out of 5 stars0 ratingsQuantum Computing For Dummies Rating: 0 out of 5 stars0 ratingsCCNA Certification Study Guide, Volume 2: Exam 200-301 Rating: 0 out of 5 stars0 ratingsAWS Certified Cloud Practitioner Study Guide: CLF-C01 Exam Rating: 5 out of 5 stars5/5Networking All-in-One For Dummies Rating: 5 out of 5 stars5/5Concise and Simple Guide to IP Subnets Rating: 5 out of 5 stars5/5Practical Ethical Hacking from Scratch Rating: 5 out of 5 stars5/5Home Networking Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5Emergency Preparedness and Off-Grid Communication Rating: 3 out of 5 stars3/5Networking Fundamentals: Develop the networking skills required to pass the Microsoft MTA Networking Fundamentals Exam 98-366 Rating: 0 out of 5 stars0 ratingsMicrosoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsSharePoint For Dummies Rating: 0 out of 5 stars0 ratingsAmazon Web Services (AWS) Interview Questions and Answers Rating: 5 out of 5 stars5/5Alt-Right: From 4chan to the White House Rating: 3 out of 5 stars3/5Group Policy: Fundamentals, Security, and the Managed Desktop Rating: 0 out of 5 stars0 ratingsOSINT 101 Handbook: Expert-Level Intelligence Gathering: Advanced Reconnaissance, Threat Assessment, And Counterintelligence Rating: 0 out of 5 stars0 ratingsCompTIA Network+ Study Guide: Exam N10-009 Rating: 0 out of 5 stars0 ratings
Reviews for Hands-on ESP32 with Arduino IDE
0 ratings0 reviews
Book preview
Hands-on ESP32 with Arduino IDE - Asim Zulfiqar
Hands-on ESP32 with Arduino IDE
Copyright © 2023 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 capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Preet Ahuja
Publishing Product Manager: Suwarna Rajput
Senior Editor: Sayali Pingale
Technical Editor: Yash Bhanushali
Copy Editor: Safis Editing
Project Coordinator: Uma Devi
Proofreader: Safis Editing
Indexer: Hemangini Bari
Production Designer: Alishon Mendonca
Marketing Coordinator: Rohan Dobhal
First published: January 2024
Production reference: 1141223
Published by Packt Publishing Ltd.
Grosvenor House
11 St Paul’s Square
Birmingham
B3 1RB, UK
ISBN 978-1-83763-803-1
www.packtpub.com
Dedicated to my parents, Zulfiqar Ahmed and Aneela Zulfiqar, as well as my wife, Alfiya Shaikh, for their continuous support and encouragement.
Contributors
About the author
Asim Zulfiqar is a blogger and tech content creator who has been writing tutorials on embedded systems and the IoT on his blog and YouTube channel, High Voltages. Currently, he is working as a scientific programmer for IoT research projects.
He completed his bachelor’s degree in electronic engineering at Sir Syed University of Engineering and Technology, Pakistan. After that, he completed his Erasmus Mundus joint master’s degree program in photonics integrated circuits, sensors, and networks at Scuola Superiore Sant’Anna (Italy), Aston University (UK), and Osaka University (Japan).
I express my sincere gratitude to my parents and family for their continuous support and encouragement during this project.
I would also like to extend my thanks to Uma Devi, Suwarna Patil, Sayali Pingale, Aryaa Joshi, Shagun Saini, and the entire Packt team for their invaluable guidance throughout this project, as well as Kalbe Abbas and Vishwavardhan for their technical insights.
About the reviewers
Vishwavardhan Yeseskar has worked in the design and development of embedded systems for more than 12 years, especially focused on IoT. He has work experience with some of the best German companies in this domain, and he has been a part of teams that have rolled out multiple embedded devices with IoT integration.
He has a bachelor’s degree in electronics and communication from Visvesvaraya Technological University and a postgraduate diploma in artificial intelligence and machine learning from Amity University. He is currently employed as a product architect at a leading German firm.
I would like to thank my wife, who encouraged me to contribute to the learning community by active participation, and also for her patience during the period of review of this book.
Kalb e Abbas, Jafferi is an embedded systems engineer with more than five years of experience working in the embedded systems domain. His main interest is creating networks of devices using wireless technologies such as BLE, Wi-Fi, and LPWAN, and monitoring these devices remotely.
He has collaborated with many web and application developers that required integration of data collection and monitoring using embedded devices.
I am indebted and thankful to my parents, friends and colleagues, and all the employers who saw potential in me and always made me feel special.
I am very much thankful to the author of the book and my friend, Asim Zulfiqar. His knowledge, experience, and humbleness have inspired me in many ways.
Table of Contents
Preface
Part 1 – Introduction: Getting Familiar with ESP32
1
IoT with ESP32 using Arduino IDE
A brief introduction to IoT
IoT and its main characteristics
The basic architecture of IoT
Applications of IoT
Understanding the capabilities of ESP32 for IoT
A brief overview of the ESP32 board and its variants
ESP32 versus other development boards
Deep dive into the Arduino IDE 2.0 to program ESP32
How can ESP32 be programmed?
The Arduino IDE 2.0
Installing the Arduino IDE 2.0
An overview of the Arduino IDE 2.0 user interface
Setting up the Arduino IDE 2.0 for ESP32
ESP32 Hello World
example using the Arduino IDE 2.0
Bonus – Simulating ESP32 projects
Summary
2
Connecting Sensors and Actuators with ESP32
Technical requirements
Getting hands-on with ESP32 GPIO pins and an overview of them
ESP32 peripherals
ESP32 basic input/output example
ESP32 PWM example
Mastering UART communication
How the UART protocol works
UART communication between two ESP32s
Sensors that use UART communication
I2C communication with ESP32
How I2C communication works
An I2C communication example using ESP32
Devices that use I2C communication with ESP32
Understanding SPI communication
How does SPI communication work
SPI communication example using ESP32
Devices that use SPI communication
Summary
3
Interfacing Cameras and Displays with ESP32
Technical requirements
Using the ESP32 camera module
How to use ESP32-CAM with the Arduino IDE
ESP32 camera example
Interfacing displays with ESP32
Interfacing a 16x2 LCD with ESP32 using I2C
Interfacing an OLED with ESP32 using I2C
Interfacing a TFT display with ESP32 using SPI and I2C
Interfacing an e-paper display with ESP32
Comparison of displays
Summary
Part 2 – IoT Protocols and ESP32
4
Implementing Network-Based Protocols with ESP32
Technical requirements
Types of networks
Exploring wireless capabilities with Wi-Fi
How the Wi-Fi protocol works
ESP32 built-in Wi-Fi capabilities
Using Wi-Fi Direct for P2P connections
Creating a personal area network with BLE
How the BLE protocol works
ESP32 BLE capabilities
BLE server and client using ESP32
ESP32 as BLE Beacon advertiser
Expanding ESP32 connectivity beyond Wi-Fi and BLE
Cellular communication with ESP32
The LoRaWAN protocol
Comparison of protocols
Summary
5
Choosing the Right Data-Based Protocols for Your ESP32 Projects
Technical requirements
Exploring HTTP with ESP32 – enabling IoT devices to communicate with web servers
What is HTTP?
How does HTTP work?
ESP32 as an HTTP web server
ESP32 as an HTTP client
Configuring and managing Wi-Fi using the HTTP protocol in ESP32
Exploring MQTT for IoT communication with ESP32
What is MQTT?
How does MQTT work?
MQTT pub-sub example
Adding real-time notifications using webhooks
What are webhooks and how do they work?
Webhook example
A real-life analogy of HTTP, MQTT, and webhooks
Comparing HTTP, webhooks, and MQTT
Summary
Part 3 – Practical Implementation
6
Project 1 – Smart Plant Monitoring System Using ESP32, Messaging Services, and the Twitter API
Technical requirements
Interfacing sensors with ESP32
Connecting the sensors
Reading the sensor data
Sending emails using SMTP
Setting up a Gmail account to send emails using ESP32
Writing code for sending emails using ESP32
Using the CallMeBot API to send WhatsApp and Telegram messages
Setting up WhatsApp messages
Setting up Telegram messages
The ESP32 code to send messages on WhatsApp and Telegram
Publishing update tweets on Twitter
Setting up the Twitter API
Code for publishing the plant updates on X/Twitter
Summary
7
Project 2 – Rent Out Your Parking Space
Technical requirements
Interfacing sensors with ESP32
The connection diagram
Reading the distance using the ultrasonic sensor
Reading the push button and controlling the Servo motor
Showing a QR code on the OLED
Integrating the PayPal API
Receiving PayPal notifications in ESP32
Creating a user-friendly experience for potential renters
Real-world implementation and project limitations
Current project limitations
Security concepts in IoT
Summary
8
Project 3 – Logging, Monitoring, and Controlling using ESP32
Technical requirements
Interfacing sensors and actuators with ESP32
Connection diagram
Reading the sensor data
Setting up InfluxDB Cloud and logging the data
Cloud database setup
Logging data to InfluxDB
Monitoring and visualization using the Grafana cloud
Creating a dashboard and visualizing the data
Controlling the main entrance gate using the MQTT protocol
Summary
9
From Arduino IDE to Advanced IoT Development – Taking the Next Steps
Power of ESP-IDF
What is ESP-IDF?
ESP-IDF versus the Arduino ESP32 core
Understanding RTOS use cases and features
FreeRTOS
FreeRTOS example using the Arduino IDE
PlatformIO – an alternative to the Arduino IDE
Using PlatformIO to upload code to ESP32
Enterprise clouds
AWS IoT services
Azure IoT services
A complete IoT embedded and software roadmap
Roadmap for IoT embedded development
Roadmap for IoT applications development
IoT protocols
Message brokers
Databases
IoT platforms
IoT cloud providers
Summary
Index
Other Books You May Enjoy
Preface
The ESP32 is a great microcontroller for learning and creating Internet of Things (IoT) applications. It’s especially good for beginners in the IoT world. Setting up and connecting sensors to the ESP32 can be complicated, but the Arduino IDE makes it easier to write code, upload it to the ESP32, and use its features. This book is designed to help you understand the basics of using sensors, connecting to networks, processing data, and creating applications with the ESP32, giving you a solid foundation for IoT development.
Starting with the basics of the ESP32 and the Arduino IDE 2.0, you’ll learn how to connect sensors to the ESP32 through practical examples. Then, the book shows you how to use the ESP32 camera and display modules with examples. It also explains IoT networks and data protocols, providing you with different options for various IoT applications. Toward the end of the book, you’ll use your knowledge to work on exciting projects, from smart connected devices to data loggers and automation, experiencing real-world applications.
By the end of the book, you’ll be able to confidently work on ESP32 projects, choose the right IoT protocol for your application, and successfully create and deploy your own IoT projects.
Who this book is for
This book is for people who like electronics and IoT, whether you’re just a fan, enjoy it as a hobby, or work in the field. It’s written to help you really understand ESP32 and IoT protocols by giving you practical examples. You should know a bit about electronics and have some experience with programming, but it’s designed to be easy for beginners. So, if you’re interested in learning about ESP32 and IoT, this book is a good choice for you.
What this book covers
Chapter 1
, IoT with ESP32 using Arduino IDE, begins with an introduction to IoT, outlining its characteristics and various applications. Following that, the ESP32 is introduced, and its capabilities are explored in detail. The chapter concludes by explaining and utilizing the Arduino IDE for a Hello World
example.
Chapter 2
, Connecting Sensors and Actuators with ESP32, provides an overview of the ESP32’s General Purpose Input/Output (GPIO) and peripherals. The chapter also explores communication protocols used to connect different sensors, including examples of UART, I2C, and SPI.
Chapter 3
, Interfacing Cameras and Displays with ESP32, offers both theoretical insights and practical guidance on utilizing the ESP32-CAM board, along with various display modules in conjunction with the ESP32.
Chapter 4
, Implementing Network-Based Protocols with ESP32, provides an overview of network-based protocols such as Wi-Fi, Bluetooth Low Energy (BLE), Cellular (5G and NB-IoT), and LoRaWAN. The chapter also explores the distinctions between these protocols, offering insights to help you select the most suitable network protocol for your upcoming projects.
Chapter 5
, Choosing the Right Data-Based Protocols for Your ESP32 Project, explores various data-based protocols such as HTTP, MQTT, and webhooks. The chapter outlines the differences between these protocols, aiding you in selecting the most appropriate data-based protocol for your ESP32 projects.
Chapter 6
, Project 1 – Smart Plant Monitoring System Using ESP32, Messaging Services, and the Twitter API, offers a step-by-step guide to reading environmental parameters such as temperature, humidity, and soil moisture using ESP32. Additionally, the chapter covers how to share these status updates on Twitter, Email, WhatsApp, and Telegram using ESP32.
Chapter 7
, Project 2 – Rent Out Your Parking Space, provides a step-by-step guide to creating a prototype system. This system enables you to accept payments via PayPal to rent out your parking space.
Chapter 8
, Project 3 – Logging, Monitoring, and Controlling Using ESP32, outlines a step-by-step approach to reading data from various sensors in different locations within a house, such as the kitchen and bedroom, using ESP32. Moreover, the chapter covers how to log this data in a database, visualize and monitor it, and control components using your smartphone.
Chapter 9
, From Arduino IDE to Advanced IoT Development – Taking the Next Steps, offers a roadmap to advance your IoT development skills and suggests the next steps you can take to build on the knowledge gained from this book.
To get the most out of this book
This book is designed for beginners, so you only need a little knowledge about electronics and programming. To work on the projects in the book, you’ll also need some devices listed in the following table. For the software part, you’ll need the Arduino IDE, which works on all major operating systems. Additionally, the book mentions some free third-party services in different chapters, such as the OpenWeatherMap API, Twitter API, CallMeBot API, InfluxDB, Grafana, and PayPal API.
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.
The specific uses of both software and hardware are thoroughly explained in their respective chapters with detailed instructions.
Download the example code files
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Programming-ESP32-with-Arduino-IDE
. 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: The WiFi.softAP() function is used to set up the ESP32 as an access point.
A block of code is set as follows:
#include
const char* ssid = MyESP32AP
;
const char* password = password123
;
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid, password);
IPAddress apIP(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.softAPConfig(apIP, apIP, subnet);
Serial.print(Access Point IP Address:
);
Serial.println(WiFi.softAPIP());
}
void loop() {
// Your code goes here
}
Bold: Indicates a new term, an important word, or words that you see on screen. For instance, words in menus or dialog boxes appear in bold. Here is an example: Now, you can upload the code using Arduino IDE and open Serial Monitor.
Tips or important notes
Appear like this.
Get in touch
Feedback from our readers 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 Hands-on ESP32 with Arduino IDE, 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/9781837638031
Submit your proof of purchase
That’s it! We’ll send your free PDF and other benefits to your email directly
Part 1 – Introduction: Getting Familiar with ESP32
In this section, you’ll familiarize yourself with the fundamental elements of this book, including ESP32, the Internet of Things (IoT), and the Arduino IDE 2.0. Additionally, you’ll learn the utilization and integration of sensors, actuators, cameras, and displays with ESP32 and the Arduino IDE. By the end of this part, you’ll have acquired the necessary hardware-interfacing knowledge, enabling you to move on to exciting IoT projects.
This part has the following chapters:
Chapter 1
, IoT with ESP32 using Arduino IDE
Chapter 2
, Connecting Sensors