Introduction
In today’s smart home age, automation and remote control of average home appliances have gotten less expensive and more accessible. If you want a simple way to remotely control devices and help them become more energy efficient, a smart plug is what you need. With the NodeMCU ESP8266 pinout, you can turn a custom smart plug that wirelessly controls appliances, sets schedules and even monitors their power usage.
In this post, we’ll look at how to make your own smart plug using the ESP8266 NodeMCU development board. We’ll also focus on addressing the question: Going further, “NodeMCU what is?”, is going to give a comprehensive overview of the key microcontroller.
Understanding the ESP8266 NodeMCU: What is NodeMCU?
NodeMCU is an open source IoT platform consisting of firmware running on the esp8266 WiFi SOC, and its corresponding hardware. An integrated ESP8266 Wi-Fi SoC is used to simplify the development of IoT applications. The framework supports several development environments and also supports development from beginners to professionals such as Arduino IDE. Depending on the answer of the “NodeMCU what is”, the user has to take into account the ease of use and the versatility in IoT projects.
Features of NodeMcu ESP8266
Built-in Wi-Fi: The built in Wi-Fi capabilities of ESP8266 make it perfect for use in IoT applications.
GPIO Pins: Multiple General Purpose Input/Output (GPIO) pins on the development board can be interfaced with the external devices.
Compatibility with Arduino IDE: It fires up with the Arduino IDE and is easy to program and debug.
Cost-effective: This is a highly affordable solution to create the IoT applications.
As Internet of Things (IoT) has become a new thing, the enthusiasts are able to make smart devices that create functional convenience for the daily life. An ESP8266 based Smart Plug which enables you to remotely control your appliances using a smartphone is one of those innovations. Tonight we’ll look at building a custom Wi Fi smart plug using an ESP8266 module, a solution which is cheaper than commercial alternatives like Sonoff Smart Plug (TH16) and Moko WiFi Plug whilst providing that satisfaction of making something yourself. This project is intended to design a Wi-Fi Smart Plug that fits any ordinary AC socket and can deliver command to the device over the phone remotely.
Project Goal and Motivation
When my internet crashed, I wanted a smart solution to remote power cycle my Wi-Fi router, without walking all the way over there each time. Every time I called customer service they would suggest the standard ‘please restart your router’ fix. I decided to make a Wi-Fi Smart Plug and control the router without touching it physically. The trickiest part, however, was to get back in control if you decided to turn off your router and lose the internet. The good news is ESP8266 can also function as an access point, broadcasting its own Wi-Fi signal. It has this feature to keep a network up even if the primary router goes down. Through programming the ESP8266 as a captive portal users are able to connect to its network and access a web interface in order to toggle connected devices on or off.
Core Components Required :
-
- ESP8266 Wi-Fi Module: The heart of the project
- Hi-Link AC to DC Converter (3.3V): Drives the ESP8266 from mains voltage
- 3.3V Relay: Switches the load on and off
- NPN Transistor (BC547): Controls the relay
- FTDI Programmer Module: This microcontroller has to be flashed with the code mentioned above in order to function
- Arduino Wi-Fi Shield: For simplicity in programming (if present)
- Miscellaneous components: Stranded wires, a couple of resistors, and the casing for a plug.
Circuit Design and ESP 8266 Setup
While configuring the circuit, the ESP8266 powers up from a Hi-Link AC-DC converter which converts 220V AC mains voltage to 3.3V DC with 900mA of current output. As a result this output is fully sufficient to power the ESP8266 module. The relay toggled on the ESP’s GPIO pin runs to the load. You can use an NPN Transistor (BC547) as the input signal to turn on the relay is coming from an ESP8266 (with a 3.3V signal). In addition to this it is important to make sure that GPIO0 and GPIO2 pins are pulled high using 10k resistors at boot to prevent the ESP8266 from entering programming mode.
In order to efficiently design circuit around ESP8266, you need to understand ESP8266pinout; how to configure pin assignments for this module. An example is GPIO-0 and GPIO-2 must be held high during startup, and GPIO-1 may be used for TX communication.
Programming the Wi-Fi Shield to ESP8266
Programmation d’ESP8266 devient plus simple si on possède une carte Arduino Wi-Fi. The shield is used as a middleman between your computer and the ESP module. Begin by uploading the code using the Arduino IDE. If you don’t have a shield, you can use an FTDI programmer to directly upload the code. The Wi-Fi Smart Plug’s code translates to a WIFI network with an SSID like “ESP_Smart_Plug”, which you can see when looking for WIFI networks on your phone.
After connecting to this network you’re redirected to a web page. You can easily toggle the plug’s state to ON or OFF on the page. On the press of the ON button, the GPIO pin controls the relay, and the LED on the ESP board is switched on. Besides, pressing the OFF button switches off the relay and the LED.
How to Build and Assemble the Smart Plug
Once you have programed successfully, you are done with the programmer board. The final step is creating a compact circuit into which all elements are safely encased inside a small casing. The casing is ready and the Hi Link AC DC converter is connected to the mains power supply and relay and load are wired per the circuit diagram. With your appliance plugged into the smart plug’s output, it can be inserted into any regular ac socket.
First, I 3D printed the casing of the smart plug.
But the project would not be complete without an enclosure to safely house all components. I designed a 3D printed casing that holds both the circuit board and the AC socket tightly using a 3D printing software. To do this, I took calipers to the dimensions of the board, plug and socket to create a 3D model of the mesh with appropriate slots for connectors and wiring. The model was saved in an STL file and 3D printed. The final casing closed around the entire assembly neatly, with ports for the plug, socket and on/off buttons.
Testing and Final Touches
I tested that with the smart plug, ready to go, and plugging it into my router’s adapter. You plug the smart plug to an existing socket, then you plug the plugin of the adapter to the output end of the smart plug. This let me leave the router’s primary power switch always on but toggle power on radio remotely using my phone. The ESP8266pinout configuration was configured such that the plug kept running without dropping network access when toggling the load.
That said, the Smart Plug that I created is based on ESP8266 and is not only limited to use with router. You can use it to automate any low power AC appliance in your home. And when you want to turn on a lamp, fan or whatever other device, the possibilities are endless.
Conclusion: ESP8266 in IoT projects: its Potential.
This project is clarifying just how versatile the ESP8266 Wi-Fi module is — especially when used together with its proper ESP8266pinout configurations. With this module a user can turn an appliance into a client or an access point very easily, thus controlling appliances even when the appliances aren’t connected to Internet. An intuitive web interface is used to make this smart plug a practical addition to any smart home automation.
Besides, as you build this smart plug, you also get a more hands on knowledge on working with ESP8266pinout plus retarded automation using relay. As the project is enhanced with new features such as voice control integration or scheduling, the house could develop into a smarter device of the kind suitable for 21st century homes.
This DIY project paves the way for many home automation applications, yet, since it’s DIY, it’s also an economical and rewarding venture for electronics enthusiasts and makers. You are free to adjust, change, and play around with the design to best fit your needs.
CODE :
//provided to you by ElectroGlobal
/*Smart Plug – Captive Portal Home Automation
* Requires no Wi-Fi to operate*/
//Download all required header filers from circuitdigest.com
#include <ESP8266WiFi.h>
#include “./DNSServer.h”
#include <ESP8266WebServer.h>
const byte DNS_PORT = 53; // 53 is set as DNS port
IPAddress apIP(10, 10, 10, 1); // Network Server
DNSServer dnsServer; // DNS server object
ESP8266WebServer webServer(80); // Webserver object
int GPIO_2 = 2; //Pin defanition – Relay will be connected to GPIO-0
/*START OF HMTL CODE*/
String style_detials = //This String defines the style attributes for webpage
“<style type=\”text/css\”>”
” body{“
” background-color: #a69695;”
“}”
“button{“
” display: inline-block;”
“}”
“#buttons{“
” text-align: center;”
“}”
“.controllButtons{“
” margin-top: 15px;”
“margin-left: 5px;”
“background-color: white;”
“padding: 10px;”
“border:1px solid black;”
“border-radius: 10px;”
“cursor: pointer;”
“font-size: 14px;”
“}”
“.controllButtons:hover{“
” background-color: orange;”
“padding: 10px;”
“border:1px solid black;”
“border-radius: 10px;”
“cursor: pointer;”
“font-size: 14px;”
“}”
“@media only screen and (max-width: 700px) {“
” button{“
” display: block;”
“}”
“#buttons{“
” margin-top: 10%;”
“margin-left: 35%;”
“}”
” .controllButtons{“
” margin-top: 15px;”
“margin-left: 5px;”
“background-color: white;”
“padding: 15px;”
“border:1px solid black;”
“border-radius: 10px;”
“cursor: pointer;”
“font-size: 16px;”
“}”
“.controllButtons:hover{“
” background-color: orange;”
“padding: 15px;”
“border:1px solid black;”
“border-radius: 10px;”
“cursor: pointer;”
“font-size: 16px;”
“}”
“}”
“</style>”;
String Home_Screen = “” //Page 1 – Home Screen HTML code
“<!DOCTYPE html><html>”
“<head><meta name=\”viewport\” content=\”width=device-width, initial-scale=1\”>”
+ style_detials +
“<div id=\”buttons\”>”
“<a style=\”text-decoration:none;\” href=\”relay_ON\”> <button id=\”switchLight1\” class=\”controllButtons\”>Turn ON</button> </a>”
“<a style=\”text-decoration:none;\” href=\”relay_OFF\”><button id=\”switchLight2\” class=\”controllButtons\”>Turn OFF</button> </a>”
“</div>”
“<body><h1>Welcome – CircuitDigest</h1>”
“</body></html>”;
String ON_Screen = “” //Page 2 – If device is turned ON
“<!DOCTYPE html><html>”
“<head><meta name=\”viewport\” content=\”width=device-width, initial-scale=1\”>”
+ style_detials +
“<div id=\”buttons\”>”
“<a style=\”text-decoration:none;\” href=\”relay_ON\”> <button id=\”switchLight1\” class=\”controllButtons\”>Turn ON</button> </a>”
“<a style=\”text-decoration:none;\” href=\”relay_OFF\”><button id=\”switchLight2\” class=\”controllButtons\”>Turn OFF</button> </a>”
“</div>”
“<body><h1>Smart Plug – Turned ON</h1>”
“</body></html>”;
String OFF_Screen = “” //Page 3 – If device is turned OFF
“<!DOCTYPE html><html>”
“<head><meta name=\”viewport\” content=\”width=device-width, initial-scale=1\”>”
+ style_detials +
“<div id=\”buttons\”>”
“<a style=\”text-decoration:none;\” href=\”relay_ON\”> <button id=\”switchLight1\” class=\”controllButtons\”>Turn ON</button> </a>”
“<a style=\”text-decoration:none;\” href=\”relay_OFF\”><button id=\”switchLight2\” class=\”controllButtons\”>Turn OFF</button> </a>”
“</div>”
“<body><h1>Smart Plug – Turned OFF</h1>”
“</body></html>”;
/*END OF HMTL CODE*/
void setup() {
pinMode(LED_BUILTIN, OUTPUT); //LED pin as output for indication
pinMode(GPIO_2, OUTPUT); //GPIO pin as output for Relay control
WiFi.mode(WIFI_AP); //Set ESP in AP mode
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(“ESP_Smart_Plug”); // Name your AP network
dnsServer.start(DNS_PORT, “*”, apIP);
webServer.onNotFound([]() {
webServer.sendHeader(“Location”, String(“http://www.circuitdigest-automation.com/home.html“), true); //Open Home screen by default
webServer.send ( 302, “text/plain”, “”);
});
webServer.on(“/home.html”, []() {
webServer.send(200, “text/html”, Home_Screen);
});
//ON_Screen
webServer.on(“/relay_ON”, [](){ //If turn on Button is pressed
digitalWrite(LED_BUILTIN, LOW); //Turn off LED
digitalWrite(GPIO_2, HIGH); //Turn off Relay
webServer.send(200, “text/html”, ON_Screen); //Display this screen
});
//OFF_Screen
webServer.on(“/relay_OFF”, [](){ //If turn off Button is pressed
digitalWrite(LED_BUILTIN, HIGH); //Turn on LED
digitalWrite(GPIO_2, LOW); //Turn on Relay
webServer.send(200, “text/html”, OFF_Screen); //Display this screen
});
webServer.begin();
}
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
}