Controlling Appliances with ESP32 and MQTT Protocol

We have already discussed some ESP32 based projects in the previous. Now this time we are going to build an MQTT based controlling appliances project. Nowadays we all need a home automation system that can be controlled by our mobile phone or computer. This project is also worked as a home automation system. We can control devices using it like fans, lights, ACs, etc. This project needs very few components and is less expensive than other automation systems.

Must Read Home Automation System Projects

What is MQTT?

MQTT or MQ Telemetry Transport is an open type light messaging protocol that provides resource-constrained networks. It can distribute telemetry information in low bandwidth conditions.


Controlling Appliances with ESP32 and MQTT Protocol

Circuit Diagram

Controlling Appliances with ESP32 and MQTT Protocol Circuit Diagram

Components Required

Hardware Parts

  • ESP32 Development Board
  • 5 Volt Relay Module
  • BC547 NPN Transistor
  • 100 Ohm Resistor (x2)
  • 1K Ohm Resistor
  • Red LED
  • Green LED
  • 1N4007 PN Diode
  • Veroboard
  • Wires
  • 5 Volt Hi-Link AC-DC Converter

Software Parts

  • Ubidots Dashboard
  • MQTT Protocol

About Parts for Controlling Appliances

ESP32 Development Board

It is a low-cost, minimal-power system on a microcontroller with integrated Wi-Fi and Bluetooth facility. ESP32 is created and developed by Espressif Systems, a Shanghai-based Chinese company, and it is manufactured by TSMC using its 40nm chip technology. In this controlling appliances project, we can also use ESP8266 NodeMCU instead of ESP32. ESP32 have so many analog pins.

PCB Design

For removing messy wiring and give a clean look, I designed a PCB prototype for this project. It is also helpful for troubleshooting that runs great without any errors. To design this PCB board, I used EasyEDA as it is too easy to use. For ordering PCB for this, I prefer PCBWay.

Gerber file for Controlling Appliances with ESP32 and MQTT Protocol Gerber.

PCB View

Controlling Appliances with ESP32 and MQTT Protocol 2D View
2D View
Controlling Appliances with ESP32 and MQTT Protocol 3D View
3D View

Order PCB From PCBWay

There are so many forums or communities for electronics online in today’s technical world, and one of the famous ones for PCB manufacturing is the PCBWay. PCBWay is a place that brings manufacturers and customers together. They have more than a decade of experience in this field of fabrication, prototyping and assembling PCBs. PCBWay have proved their focus to their customers’ needs in terms of cost-effectiveness, delivery, and quality.

How PCB Manufacturing Process Done in PCBWay

Standard quality for any product needs to be maintained using some parameters. PCBWay gives that opportunity by quality control in designing and manufacturing. At first, they ensure accuracy, clarity, validity of the PCB files that we sent to them.

Then all the boards will go through the most stringent tests other than the basic visual check. They adopt most of the testing and inspecting equipment used in the industry, such as Flying Probe Tester, X-Ray Inspection Machine, Automated Optical Inspection (AOI) Machine. PCBWay are having 50+ new engineers on the daily basis around the world using PCBs for their work, who trust for their reliable quality. They produce high-quality pink, orange, grey, even transparent solder mask. Moreover, according to people’s needs, they can also provide Black core PCB. Check it out for a High-Quality PCB solder mask. There are some pictures below of the new colours of solder musks.

Features of PCBWay

PCB Prototyping & Manufacturing: PCBWay produces FR-4 and Aluminum boards and advanced PCBs like Rogers, HDI, Flexible and Rigid-Flex boards, at a very reasonable price. Just check this Instant Quote to order premium PCBs for your hobby projects.

PCB Assembly: It not only offers PCB Prototyping but also offers PCB Assembly service. You can order a full PCB board with attached components by uploading your file. SMT & THT assembly starts from only 30$ including free stencil and worldwide shipping. The components can be sourced and provided by PCBWay, or by clients themselves.

Open Source Community: It not only offers PCB Prototype & Assembly service but also gives opportunities to students for sponsorships. I think this is so great. They feature their projects on the website also.

3D Printing & CNC: It also produces Rapid Prototyping like 3D printing, CNC Machining, Sheet Metal Fabrication, and Injection Molding. The quality of the product is too premium and reliable. And also the best part is that you will receive your order within a minimum of 3 days.

PCBWay Store: More boutique gift modules are waiting for you to discover, giving your creativity wings. You can order all types of electronic equipment, components and many more with exciting offers. Go with the link and buy your needs, PCBWay Store.

Services: PCBWay offers great servicing of their products. It takes only 3 to 15 days according to courier service to deliver your products. Also, it has a live support facility. Whenever you have any problems, you can always reach a live customer service person to respond to your emails or messages. They have the “Return and Refund” principle, for every unusable board caused by PCBWay, they will rebuild and refund the order soon.

How to Add Device in Ubidots for Controlling Appliances

  • At first, search Ubidots education in Google or any browser you use and visit the first link.
  • Then click on sign up and fill your details and click again on sign up.
  • Now click the next button continuously untill you reach a page where you see the add a new device button.
  • Name the device like “ESP32”. Now click on the device.
  • There you can see the temperature variable. Rename it as hall sensor.
  • Add a another variable and name it relay.
  • Now visit the temperature variable and set the API label as hall-sensor.
  • Now go to the Dashboard and click on the ‘+’ icon. For hall sensor select “indicator” then “gauge” and select device ESP32.
  • There you can see the hall sensor variable. Then select minimum value as 0 and maximum value 100. Click on finish.
  • For relay, click on the plus button in the dashboard.
  • Select “control” then “switch” and device ESP32.
  • This time select the variable name “relay”. Select on message as 1 and off message as 0. Click on finish.

Working Principle of Controlling Appliances using ESP32 and MQTT Protocol

The working of this project is very simple. As we see in the circuit diagram very few components are required to build this project. We used the Ubidots platform for sending and receiving data over the internet via the MQTT protocol. The configuration of MQTT is very simple. We mention its steps below.

Configuration of MQTT Protocol

To configure MQTT you have to move on to the code part.

  • At first enter ssid name and password of your router. I enter my own but you have to change it from the code.
  • There you can see a token is required. For this you just need to go to your Ubidots account and click on “API credentials”. Just copy the token and replace it in the code.
  • Now enter a MQTT client name.
  • At last enter api labels of publish part and subscribe part as same from your account.

Now we start to explain the code part. We subscribed to one MQTT topic and we are publishing data for another. In the subscription part, we monitor data that is coming from the server. If the data is 1 the relay is turned on and if the data is 0 the relay is turned off.

In the publish part we collect data of inbuilt Hall effect sensor from ESP32 board. Then publish the data to the server. By this process we can monitor our data and also we can control the relay from the web. We use the Ubidots dashboard for this.

ESP32 Code for Controlling Appliances

Download the library file for compiling the code pubsubclient.h.

Leave a Comment