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

Major Project 2: Smart Home Control Relay System

In this chapter, we will be creating yet another full-scale project using the concepts we learned in the initial chapters of this book. The main objective of this chapter is to create a smart home device for controlling wall switches using the Node-RED dashboard hosted on the Raspberry Pi. The device will be based on the popular ESP32 development board.

This chapter is a step-by-step tutorial to build this project from scratch. We will be covering the following aspects of the project:

  • Hardware requirements and setup
  • Code explanation
  • Raspberry Pi setup
  • Project enhancements

For this project, we will be preparing a PCB instead of creating the circuit on a breadboard for a more finished and professional look. The final hardware is shown in Figure 6.1.

Figure 6.1 – Smart home control relay system (ESP32)

In this figure, we can see that the hardware has four relays, and the functionality...

Hardware requirements and setup

This project has been chosen to demonstrate the control capabilities of our Raspberry Pi MQTT system. Hence, there will be no sensors used in this project. But that is very possible and extremely easy to implement. In fact, that can be a challenge for you: build a project with both monitoring and control components.

For now, the components required to build this project are as follows (Figure 6.2):

  • ESP32 development board
  • 5V non-latching relay
  • Hi-Link 5V power supply
  • Two resistors – 10k ohm and 330 ohm
  • Two-pin terminal connectors
  • BC547 transistor
  • 1N4007 diodes
  • Buzzer
  • LEDs

Figure 6.2 – Required components to build this project

Now, we will briefly go through each major component’s role, as we did for the first project.

ESP32 development board

We have chosen the ESP32 development board as the brains for this project (Figure 6.3). One major reason for that...

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...

Raspberry Pi setup

The Raspberry Pi will be the host for the local MQTT broker in this project and also the dashboard hosting device. The dashboard for this project will be created using Node-RED and the Node-RED dashboard module, which will both be running on the Raspberry Pi.

Setting up MQTT and Node-RED on the Raspberry Pi has already been covered, so we will start with the Node-RED setup portion straight away. The first step is to start Node-RED after booting up your Pi.

Just open a new terminal on your Pi and type in the following command:

node-red-start

Please refer to Figure 6.11 for reference:

Figure 6.11 – Starting Node-RED on the Pi

This command will start your Node-RED and give you the IP address from which you can access the Node-RED editor from any device connected to the same network as the Pi. Once you open Node-RED, the next step is creating the flow and, in turn, the dashboard for this project. As this is a control project...

Project enhancements

Project enhancement is a crucial part of project development. We always strive to make things better than they already are. This case is no different.

There are several possible enhancements, both on the hardware and software. Let’s walk through some of them:

  1. The first and main hardware enhancement is adding manual feedback to our system. In fact, the PCB supports it.

The current system, as it stands, does not allow the user to use manual switches, and even if we managed to use them, we cannot get their statuses (that is, at any given moment, we can’t see the state of a switch). But the PCB we are using has a special function: it can provide the current state of any connected application (on or off) on particular ESP32 digital pins. Hence, we can get feedback. Please refer to the circuit diagram for the PCB to see how you need to connect the switch wires to the PCB.

The portion on the PCB that helps achieve this is marked in...

Summary

We developed our second full-scale project in this chapter, creating a smart relay system based on ESP32 and operated over MQTT. We will walk through the key points we covered in this chapter just to refresh your memory.

We started with the hardware requirements and setup of those components. This includes setting up our system and connecting all the components on a custom PCB. Next, we moved on to the explanation of the code, wherein we broke the code into several snippets to make it easier to understand. After that, we set up the project dashboard on the Raspberry Pi. Finally, we moved onto the demonstration part of the project to see our project in action.

In the next chapter, we will cover how to take this concept even further by taking the MQTT broker global so that we can access our devices through MQTT from anywhere without the constraint of local network coverage.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi and MQTT Essentials
Published in: Sep 2022Publisher: PacktISBN-13: 9781803244488
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.
undefined
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 $15.99/month. Cancel anytime

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