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

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


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.
Guide for Ordering PCB From PCBWay
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <WiFi.h> #include <PubSubClient.h> #define WIFISSID "SSID" // Type your WifiSSID here #define PASSWORD "PASS" // Type your wifi password here #define TOKEN "TOKEN" // Type your Ubidots' TOKEN #define MQTT_CLIENT_NAME "RandomName" // MQTT client Name, it should be a random and unique ascii string and different from all other devices #define VARIABLE_LABEL "hall-sensor" #define VARIABLE_LABEL_SUBSCRIBE "relay" #define DEVICE_LABEL "esp32" #define relay 26 // Set the GPIO26 as relay char mqttBroker[] = "things.ubidots.com"; char payload[100]; char topic[150]; char topicSubscribe[100]; char str_sensor[10]; WiFiClient ubidots; PubSubClient client(ubidots); void reconnect() { // Loop until reconnected while (!client.connected()) { Serial.println("Attempting For Connection.."); if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) { Serial.println("Connected!"); client.subscribe(topicSubscribe); } else { Serial.print("Failed, rc="); Serial.print(client.state()); Serial.println(" try again in 4 seconds"); delay(4000); } } } void callback(char* topic, byte* payload, unsigned int length) { char p[length + 1]; memcpy(p, payload, length); p[length] = NULL; String message(p); if (message == "0") { digitalWrite(relay, LOW); } else { digitalWrite(relay, HIGH); } Serial.write(payload, length); Serial.println(); } void setup() { Serial.begin(115200); WiFi.begin(WIFISSID, PASSWORD); pinMode(relay, OUTPUT); Serial.println(); Serial.print("Waiting for WiFi.."); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi Connected"); Serial.println("IP Address: "); Serial.println(WiFi.localIP()); client.setServer(mqttBroker, 1883); client.setCallback(callback); sprintf(topicSubscribe, "/v1.6/devices/%s/%s/lv", DEVICE_LABEL, VARIABLE_LABEL_SUBSCRIBE); client.subscribe(topicSubscribe); } void loop() { if (!client.connected()) { client.subscribe(topicSubscribe); reconnect(); } sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL); sprintf(payload, "%s", ""); sprintf(payload, "{\"%s\":", VARIABLE_LABEL); float sensor = hallRead(); Serial.print("Value of Sensor is:- "); Serial.println(sensor); dtostrf(sensor, 4, 2, str_sensor); sprintf(payload, "%s {\"value\": %s}}", payload, str_sensor); // Adds the value Serial.println("Sending data to Ubidots Cloud"); client.publish(topic, payload); client.loop(); delay(1000); } |