Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi and MQTT Essentials

You're reading from  Raspberry Pi and MQTT Essentials

Product type Book
Published in Sep 2022
Publisher Packt
ISBN-13 9781803244488
Pages 272 pages
Edition 1st Edition
Languages
Author (1):
Dhairya Parikh Dhairya Parikh
Profile icon Dhairya Parikh

Table of Contents (14) Chapters

Preface 1. Part 1:Covering the Basics
2. Chapter 1: Introduction to Raspberry Pi and MQTT 3. Chapter 2: MQTT in Detail 4. Chapter 3: Introduction to ESP Development Boards 5. Chapter 4: Node-RED on Raspberry Pi 6. Part 2: Practical Implementation – Building Two Full-Scale Projects
7. Chapter 5: Major Project 1: IoT Weather Station 8. Chapter 6: Major Project 2: Smart Home Control Relay System 9. Part 3: How to Take Things Further – What Next?
10. Chapter 7: Taking Your MQTT Broker Global 11. Chapter 8: Project Prototype to Product – How? 12. Index 13. Other Books You May Enjoy

Code explanation

The hardware setup is complete and the PCB is ready. It is time to write some code for our project. This code will be written in the Arduino IDE for our ESP32 development board.

The code will have the following tasks to perform:

  1. Connect to the pre-configured Wi-Fi network.
  2. Connect to the MQTT broker hosted on the same network (on the Pi, in our case).
  3. Pin initialization for the GPIO pins we will be using in this project.
  4. Subscribe to various switch topics.
  5. Reconnect to the MQTT server if it disconnects.
  6. Develop a logic to control the relays based on the payload received on those topics (in the callback function).

Points 4, 5, and 6 will run indefinitely (part of the loop() function). The code is available on the GitHub repository of this project. Now, we will divide the code into parts to make it easier to understand, as we did before.

To import the required libraries use this code:

// Importing the required Libraries
#include...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}