In today's interconnected world, having control over your devices from anywhere can greatly enhance convenience and efficiency. In this blog post, I will guide you through the process of building a Wi-Fi controlled power outlet using the ESP8266 microcontroller. With this project, you will be able to remotely turn devices on and off through a web interface or smartphone app. So let's dive in and get started!
Hardware Required:
1. ESP8266 development board (e.g., NodeMCU)
2. Power outlet adapter (compatible with your country's electrical standards)
3. Jumper wires
4. Breadboard (optional, for prototyping)
5. USB cable (for programming and power)
Software Required:
1. Arduino IDE
2. ESP8266 library for Arduino IDE
3. Wi-FiManager library for Arduino IDE
4. Blynk app (for smartphone control)
Part 1: Setting Up the ESP8266 Development Board
To begin, make sure you have the Arduino IDE installed on your computer. Here are the steps to set up your ESP8266 development board:
1. Connect your ESP8266 board to your computer using the USB cable.
2. Open the Arduino IDE and go to "File" -> "Preferences."
3. In the "Additional Boards Manager URLs" field, enter the following URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json4. Click "OK" to close the Preferences window.
5. Go to "Tools" -> "Board" -> "Boards Manager."
6. In the Boards Manager, search for "esp8266" and click on "esp8266 by ESP8266 Community."
7. Click the "Install" button to install the ESP8266 board package.
8. After installation, select your ESP8266 board from the "Tools" -> "Board" menu.
Now that we have set up the development board, let's move on to configuring Wi-Fi connectivity.
Part 2: Configuring Wi-Fi Connectivity with Wi-FiManager
To make our power outlet accessible over Wi-Fi, we will use the Wi-FiManager library. This library allows us to create a web interface that enables users to configure Wi-Fi credentials without modifying the code.
1. Open the Arduino IDE and create a new sketch.
2. Install the Wi-FiManager library by going to "Sketch" -> "Include Library" -> "Manage Libraries." Search for "Wi-FiManager" and click the "Install" button.
3. Paste the following code into your sketch:
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
delay(1000);
// Connect to Wi-Fi using Wi-FiManager
WiFiManager wifiManager;
wifiManager.autoConnect("SmartPowerOutlet");
Serial.println("Wi-Fi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Your code here
}
4. In the code, the line `WiFiManager wifiManager;` creates an instance of the Wi-FiManager class.
5. The `wifiManager.autoConnect("SmartPowerOutlet");` line creates a Wi-Fi access point with the SSID "SmartPowerOutlet" if a saved configuration is not found. Connect your computer or smartphone to this access point.
6. Once connected, open a web browser and go to http://192.168.4.1. You will see a configuration page where you can enter your Wi-Fi credentials.
7. After entering the credentials, click "Save." The ESP8266 will attempt to connect to the Wi-Fi network you specified.
8. Open the Serial Monitor in the Arduino IDE and set the baud rate to 115200. You will see the ESP8266 connecting to Wi-Fi and displaying the assigned IP address.
With Wi-Fi configured, we can now move on to integrating the power outlet control and setting up remote access through a web interface or smartphone app.
Part 3: Power Outlet Control and Remote Access
1. Adding Power Outlet Control:
To control the power outlet, we will use a relay module connected to the ESP8266. The relay module acts as a switch, allowing us to turn the power outlet on and off programmatically.
- Connect the relay module to the ESP8266 as follows:
- VCC to 3.3V (or 5V, depending on the relay module)
- GND to GND
- IN1 (or any other input) to a digital pin (e.g., D1)
2. Modify the previous sketch with the following code to control the power outlet:
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#define RELAY_PIN D1 // Pin connected to the relay module
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
delay(1000);
// Connect to Wi-Fi using Wi-FiManager
WiFiManager wifiManager;
wifiManager.autoConnect("SmartPowerOutlet");
Serial.println("Wi-Fi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Set relay pin as an output
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Turn off the power outlet initially
}
void loop() {
// Your code here
}
3. Now, inside the `loop()` function, we can add the logic to control the power outlet. For example, let's add a simple function to turn the outlet on and off:
void turnOutletOn() {
digitalWrite(RELAY_PIN, HIGH); // Turn on the power outlet
Serial.println("Power outlet turned on.");
}
void turnOutletOff() {
digitalWrite(RELAY_PIN, LOW); // Turn off the power outlet
Serial.println("Power outlet turned off.");
}
4. You can now call these functions wherever necessary. For example, you can add a button to the web interface or app to trigger the `turnOutletOn()` and `turnOutletOff()` functions. You can also add additional functionality like timers or scheduling to automate the power control based on specific conditions.
Next, we will set up remote access using the Blynk platform.
Part 4: Remote Access with Blynk
1. Install the Blynk app on your smartphone from the App Store or Google Play Store.
2. Create a new account or log in if you already have one.
3. Create a new project and select the ESP8266 board as the hardware.
4. Once the project is created, you will receive an authentication token via email. Keep this token handy as we will use it in the code.
5. Install the Blynk library by going to "Sketch" -> "Include Library" -> "Manage Libraries." Search for "Blynk" and click the "Install" button.
6. Modify the previous sketch with the following code to integrate Blynk:
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <BlynkSimpleEsp8266.h>
#define RELAY_PIN D1 // Pin connected to the relay module
char auth[] = "YOUR_BLYNK_AUTH_TOKEN"; // Replace with your Blynk authentication token
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
delay(1000);
// Connect to Wi-Fi using Wi-FiManager
WiFiManager wifiManager;
wifiManager.autoConnect("SmartPowerOutlet");
Serial.println("Wi-Fi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//
Set relay pin as an output
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Turn off the power outlet initially
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
}
void loop() {
Blynk.run();
// Your code here
}
7. Replace `"YOUR_BLYNK_AUTH_TOKEN"` with the authentication token you received from the Blynk app.
8. Now, in the Blynk app, add a button widget to the project's interface. Set its output to a virtual pin (e.g., V1).
9. Add the following code to the sketch to control the power outlet based on the button's state:
BLYNK_WRITE(V1) {
int buttonState = param.asInt();
if (buttonState == HIGH) {
turnOutletOn();
} else {
turnOutletOff();
}
}
10. Upload the sketch to your ESP8266 board and open the Serial Monitor. You should see the ESP8266 connecting to Wi-Fi and Blynk.
11. Now, when you press the button on the Blynk app, it will trigger the corresponding function to turn the power outlet on or off.
Congratulations! You have successfully built a Wi-Fi controlled power outlet using the ESP8266 microcontroller. You can now control and monitor your devices remotely through a web interface or the Blynk app.
Feel free to customize the project further by adding additional features, such as sensor integration or advanced scheduling options. Enjoy the convenience and flexibility of a smart power outlet!