We have already discussed some ESP8266 NodeMCU based projects in previous. Now we are going to build a so-called popular WiFi Smart Plug using the Blynk application. In Home Automation System in our daily life, we all love to control our devices using just one device or main controller. WiFi Smart Plug is one such device that helps us to do that very easily. It allows you to control almost every wired device like table fan, lights, AC, etc. In my case, I have long wanted to make such WiFi Smart Plug that would be able to switch my AC on/off.
Many of that WiFi Smart Plugs are available on today’s market, but these smart plugs are pretty expensive to buy. So making your own WiFi Smart Plug like “Sonoff” is more interesting and quite easy. Also, it is easy to control, just plug it into any existing socket and on the other hand plug any electronic device into the smart plug. Then just keep the main switch of the existing socket always on and control devices from your smartphone directly.
What is WiFi Smart Plug?
A WiFi Smart Plug is like a power-point adapter. It fits between your power socket and the appliance you wish to plug in. Most of the smart plugs communicate (any Wi-Fi network) to a central control hub. Then, the hub uses your Wi-Fi to send information to an app on your smartphone.
Principle Behind the WiFi Smart Plug
The principle behind this WiFi Smart Plug is to turn on and off of devices from your smartphone or any other controlling device. It’s very simple, just plug it into a socket and turn the power on. Now plug any devices or lamp into the WiFi socket. That’s it. Now the AC power from the main socket gives the power to the WiFi Smart Plug. By the user’s command from the android app, it turns on and off the following devices.
Project

Circuit Diagram
Components Required
- ESP8266 WiFi Module (ESP-01/NodeMCU)
- 5V Relay Module
- 5V AC to DC Hi-Link Converter
- 3 Pin Multiplug for Enclosure
- Any WiFi Router/Mobile Hotspot Connection
- Vero board
- Wire
- AC Source
Pin Description Used in This Circuit
- VCC: Power supply pin of ESP8266 which can be supplied with 3.3V DC.
- GND: Ground reference pin of ESP8266 for ground supply to the circuit.
- RST: It is used to reset the module and it is an active LOW pin.
- GPIO16: For enabling reset pin.
- GPIO5: For output of relay module.
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 WiFi Smart Plug Gerber.
PCB View


Order PCB From PCBWay
PCBs are the neatest and professional-looking among the assembled circuit types. Any electronics circuit that needs to be professional looking uses PCB boards. But soldering is still required on the copper area on the PCB. In the early 1900s, the only way to make circuits for electrical or electronic components was by wiring them point to point on a chassis. These chassis were either pan or sheet. These methods made circuits very fragile and large or bulky, and expensive.
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 and prototyping and assembling of PCBs. PCBWay have proved their focus to their customers’ needs in terms of cost-effectiveness, delivery, and quality. And this can be proved by their outstanding customer reviews. Not only has this PCBWay provided a platform for students and electronic hobbyists to display their projects to the community but also give sponsorships if their work is attractive. Check out the large pool of sponsored projects here.
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 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.
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 Program NodeMCU Using Arduino IDE?
To use the Arduino IDE software to program the NodeMCU, we need to first install the appropriate board into the IDE software. For this copy the following link (http://arduino.esp8266.com/stable/package_esp8266com_index.json) and paste it into the blank box as shown in the picture.
First, click Preferences in the File menu and enter the copied link in the Additional Board Manager URLs part. Then press OK.
Next, find the ESP8266 option in Boards>boards manager from the Tools menu. Then install the ESP8266 board. After successful installation, we can compile code in IDE software.
Blynk Application Setup for WiFi Smart Plug
We are going to use the Blynk app as our IoT server to enable devices to communicate with the cloud server. Install new Blynk IoT application from smartphone and login or sign up to the app. After open click on the Settings icon from the upper right corner and click Add Template and name it.
Then open this and click again on another type of settings icon from the upper right corner. We can see the template id and we need to copy it for coding purposes. From there choose desired Data Stream button and select the mode for the Switch option. That’s it.
Now back once and click on the triple bar icon from the right upper corner. From there choose the Button icon. Then click on this button icon and set the Data Stream value to zero and mode to Switch.
After that, back to the home screen and click on Add New Device. After the WiFi scan, the available devices will be shown there. Then select the device and control it through this app.
Benefits of WiFi Smart Plug
- A smart plug can turn any of your existing appliances and electrical devices into smart objects that can communicate with other smart products in your home.
- Using the smartphone app, you can turn appliances on and off remotely anywhere in the world and even check to see if you left an appliance running after you’ve left your home.
- You can also monitor how much energy an appliance used directly from your smartphone. Smart plugs will even notify you if a device suddenly starts using unusual amounts of energy.
- Burglars often target homes which is empty. WiFi Smart Plugs can be used to improve home security by turning lights and appliances on and off while you’re on vacation. Burglar will think you are home while you are far from home.
- Using a smart plug, you can control your devices also with voice commands like Google Assistant, Alexa, Siri, etc.
NodeMCU Code
For compiling code, we first need some library files that are all included in this link Blynk Library Files. Just download it and paste it all with the main NodeMCU code. That’s it.
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #define BLYNK_TEMPLATE_ID "TMPLC9GS7yP8" #define BLYNK_DEVICE_NAME "WiFi Smart Plug" #define BLYNK_FIRMWARE_VERSION "0.1.0" #define BLYNK_PRINT Serial #define APP_DEBUG #define USE_NODE_MCU_BOARD #include "BlynkEdgent.h" #define RELAY_PIN_1 5 // D1 #define RELAY_PIN_2 4 // D2 #define PUSH_BUTTON_PIN_1 14 // D5 #define PUSH_BUTTON_PIN_2 12 // D6 #define VIRTUAL_RELAY_1 V0 #define VIRTUAL_RELAY_2 V1 #define BUTTON_NUM 2 #define BUTTON_NO_PRESS -1 #define BUTTON_PLUG_1 0 #define BUTTON_PLUG_2 1 #define ON 1 #define OFF 0 short gsArray_Button[BUTTON_NUM] = {PUSH_BUTTON_PIN_1, PUSH_BUTTON_PIN_2}; unsigned short gusIsRelay_1_On = OFF; unsigned short gusIsRelay_2_On = OFF; unsigned short gusIsBut_Pressed = 0; void setup() { Serial.begin(115200); delay(100); BlynkEdgent.begin(); pinMode(RELAY_PIN_1, OUTPUT); pinMode(RELAY_PIN_2, OUTPUT); pinMode(PUSH_BUTTON_PIN_1, INPUT_PULLUP); pinMode(PUSH_BUTTON_PIN_2, INPUT_PULLUP); vRelay_Control(BUTTON_PLUG_1, OFF); vRelay_Control(BUTTON_PLUG_2, OFF); vBlynk_Write_Button_State(BUTTON_PLUG_1, OFF); vBlynk_Write_Button_State(BUTTON_PLUG_2, OFF); } void loop() { short sBut = BUTTON_NO_PRESS; BlynkEdgent.run(); sBut = sRead_Button(); if(sBut == BUTTON_PLUG_1) { if(gusIsBut_Pressed == 0) { if(gusIsRelay_1_On == ON) { vRelay_Control(sBut, OFF); vBlynk_Write_Button_State(sBut, OFF); } else { vRelay_Control(sBut, ON); vBlynk_Write_Button_State(sBut, ON); } gusIsBut_Pressed = 1; } } else if(sBut == BUTTON_PLUG_2) { if(gusIsBut_Pressed == 0) { if(gusIsRelay_2_On == ON) { vRelay_Control(sBut, OFF); vBlynk_Write_Button_State(sBut, OFF); } else { vRelay_Control(sBut, ON); vBlynk_Write_Button_State(sBut, ON); } gusIsBut_Pressed = 1; } } else { gusIsBut_Pressed = 0; } } short sRead_Button(void) { short sStatus = BUTTON_NO_PRESS; for(short i = 0; i < BUTTON_NUM; i++) { if(digitalRead(gsArray_Button[i]) == 1) { delay(50); if(digitalRead(gsArray_Button[i]) == 1) { sStatus = i; } } } return sStatus; } BLYNK_WRITE(VIRTUAL_RELAY_1) { short sVirtual_Status = 0; sVirtual_Status = param.asInt(); vRelay_Control(BUTTON_PLUG_1,sVirtual_Status); } BLYNK_WRITE(VIRTUAL_RELAY_2) { short sVirtual_Status = 0; sVirtual_Status = param.asInt(); vRelay_Control(BUTTON_PLUG_2,sVirtual_Status); } void vRelay_Control(short sBut, short sOnOff) { if(sOnOff == ON) { if(sBut == BUTTON_PLUG_1) { digitalWrite(RELAY_PIN_1, LOW); gusIsRelay_1_On = ON; } else if(sBut == BUTTON_PLUG_2) { digitalWrite(RELAY_PIN_2, LOW); gusIsRelay_2_On = ON; } } else { if(sBut == BUTTON_PLUG_1) { digitalWrite(RELAY_PIN_1, HIGH); gusIsRelay_1_On = OFF; } else if(sBut == BUTTON_PLUG_2) { digitalWrite(RELAY_PIN_2, HIGH); gusIsRelay_2_On = OFF; } } } void vBlynk_Write_Button_State(short sBut, short sStatus) { if(sBut == BUTTON_PLUG_1) { Blynk.virtualWrite(VIRTUAL_RELAY_1, sStatus); } else { Blynk.virtualWrite(VIRTUAL_RELAY_2, sStatus); } } |
Can I see more details like the setup of the Blynk app and how to monitor the energy consumption?
What type of details?
Pls send details