Reader small image

You're reading from  Raspberry Pi and MQTT Essentials

Product typeBook
Published inSep 2022
PublisherPackt
ISBN-139781803244488
Edition1st Edition
Right arrow
Author (1)
Dhairya Parikh
Dhairya Parikh
author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh

Right arrow

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 page is locked
Previous PageNext Page
You have been reading a chapter from
Raspberry Pi and MQTT Essentials
Published in: Sep 2022Publisher: PacktISBN-13: 9781803244488

Author (1)

author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh