Shares
Control a Lamp Remotely Using the ESP8266 WiFi Chip

Control a Lamp Remotely Using the ESP8266 WiFi Chip

Last Update: / by Marco Schwartz

The ESP8266 is an amazing WiFi chip that can be used in several home automation applications. In this article, we are going to use it to control a lamp remotely via WiFi. However, we won’t do what you usually see on this website. Indeed, I usually use a server running on my computer to control devices remotely. Here, we are going to use the onboard processor of the ESP8266 to host a small web server, that will serve a simple interface page from which you will be able to control the lamp. And we will even make this interface responsive, so it can also be used with your phone or tablet!

Hardware & Software Requirements

For this project, you will of course need an ESP8266 chip. I used the Olimex ESP8266 module, but any ESP8266 module will work fine here.

You will also need some way to control your lamp or other devices. I originally used a simple relay for my tests, but I quickly moved to a PowerSwitch Tail Kit which allows to simply & safely plug high voltage devices to your projects.

You will also need a 3.3V FTDI USB module to program the ESP8266 chip. Finally, you will also need some jumper wires & a breadboard

This is a list of all the components that will be used in this guide:

Note that you will also need a device to control. I used a simple 30W desk lamp as a test device, but you can also use any other device in your home (if the power rating is lower than the maximum power accepted by the PowerSwitch Tail Kit). You can also just use a simple relay for test purposes.

On the software side, we will use the Arduino IDE to configure the ESP8266 chip. I will let you see my guide on how to set up the ESP8266 for a first use & how to upload a sketch:

https://www.openhomeautomation.net/getting-started-esp8266/

You will also need to install several libraries for this project:

Hardware Configuration

Again, for most of the hardware configuration I will redirect you to the article I wrote on setting up your ESP8266 chip:

https://www.openhomeautomation.net/getting-started-esp8266/

The only thing you need to add to this basic configuration is the PowerSwitch Tail Kit. Connect the two pins on the right (-in and Ground) on the ground of our project (blue power rail), and the +in pin to the GPIO5 pin. If your board doesn’t have this pin, you can plug it to the free GPIO pin of your choice, you will just need to modify your code accordingly.

Then, also connect a lamp or any electrical device to the PowerSwitch, and the other end of the PowerSwitch to the mains electricity.

This is the assembled project, without the FTDI module to configure the module:

Control a Lamp Remotely Using the ESP8266 WiFi Chip

And this is the project deployed close to the test lamp:

Control a Lamp Remotely Using the ESP8266 WiFi Chip

Controlling the Lamp Remotely

We are now going to write the code required to control our lamp remotely. Note that we want a completely autonomous operation of the device. The ESP8266 will have to handle requests coming from your browser, display a simple HTML page with two buttons (On & Off), and then control the PowerSwitch Tail Kit accordingly. To do all that, we will use the powerful aREST UI library that makes it really easy to build graphical interfaces.

As we are using the Arduino IDE for the ESP8266, we will simply write some Arduino code here. It starts by declaring which libraries we are going to use:

#include 
#include 
#include 

Then, we declare the aREST UI object:

aREST_UI rest = aREST_UI();

After that, you will need to include your WiFi name & password:

const char* ssid = "your_wifi_name";
const char* password = "your_wifi_password";

Now, in the setup() part of the sketch, we actually build the interface of the project, with a button linked to pin number 5 of the board:

rest.title("Relay Control");
rest.button(5);

After that, we connect the board to the WiFi network:

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
 
// Start the server
server.begin();
Serial.println("Server started");
 
// Print the IP address
Serial.println(WiFi.localIP());

Then, in the loop() part, we handle incoming connections with the aREST UI library:

WiFiClient client = server.available();
if (!client) {
 return;
}
while(!client.available()){
 delay(1);
}
rest.handle(client);

Note that you can find all the code for this project on the corresponding GitHub repository:

https://github.com/openhomeautomation/esp8266-relay

I used the Arduino IDE to upload the code to the board. First, select the ESP8266 board inside the Arduino IDE. Make sure that you choose the option that corresponds to your board, especially if you are using the Olimex board like I did for this tutorial. Also make sure that you changed your WiFi name & password inside the sketch.

Then, put the chip in bootloader mode by connecting GPIO0 to GND, and then power cycle the board by switching the power supply off & then on again. Then, upload the sketch to the board. Once it is done, disconnect GPIO0 from GND, and power cycle the board again.

Now, open the Serial monitor. You should see the IP address of the board being printed inside the Serial monitor. Finally, simply open your favorite web browser, and type in the board IP address. You should see the interface being displayed:

Control a Lamp Remotely Using the ESP8266 WiFi Chip

You can now try the buttons: you should notice that the state of the PowerSwitch or the relay is changing instantly. And note that this is only using the ESP8266 chip here: it’s completely independent from your computer!

The interface is also responsive, thanks to the aREST UI library. This means it will automatically adapt to the device on which it is used. This is for example the result on my phone:

Control a Lamp Remotely Using the ESP8266 WiFi Chip

How to Go Further

In this project, we build a completely autonomous remote lamp controller using the ESP8266 WiFi chip. We made this little chip control a lamp (or any other device) by serving a nice & responsive interface, allowing you to control the electrical from any device within your local WiFi network.

Of course, you can use what you learned in this project to build other home automation. You can for example use the same principles to build a completely autonomous sensor module, that measure data from several sensors and display them all in a single interface, also served by the ESP8266 module.

What about you, already built projects using the ESP8266 that are similar to this one? Please share in the comments!

Want to learn more? Get my free eBook about the ESP8266!
The ESP8266 is an amazing WiFi chip that you can use to build all sorts of projects. Download today my free eBook "Build a WiFi Weather Station with the ESP8266". Simply click on the button below!

Leave a Comment

Please enter your name.
Please enter your email.
Please enter your comment.
sravya a month ago
hii I want to interface four p6 led module to node MCU to send data using wifi and i have tried to Connect but the data was not displaying properly, and i used serial data and also used dmd library but i don't receive any data to p6led module ( indore )
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
sravya a month ago
hii I want to interface four p6 led module to node MCU to send data using wifi and i have tried to Connect but the data was not displaying properly, and i used serial data and also used dmd library but i don't receive any data to p6led module ( indore )
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Arjen Veninga a month ago
This is awesome. I want to have several of those. so how would I switch them all on and off with one button? then I should make a web interface sending the commands to all nodes, or so? I mean without making a complete home automation system first?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Jamie 2 years ago
Hi Marco, I would love to try this, but my country uses 220V. Is there a suitable relay that you can recommend?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
pankaj jain 2 years ago
how many relay switches can be controlled with single esp8266
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz pankaj jain 2 years ago
As many as the number of available pins on the ESP8266 :)
Harpreet Kaur 2 years ago
Is the anyway to integrate the existing dumb electrical wall switches that we have in home ?? Like I want the option to control the light via an app but also if someone uses the electrical switch to turn off and on. Unlike the push switch these electrical switches shorts the pins unless turned off and as a result the app fails to turn control the light.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Mick Harpreet Kaur a year ago
You could use a single pole changeover relay connected to the esp2866 o/p pin and use a two way (changeover) light switch?
Santhosh Kumar .R 2 years ago
I need to change the state for 4 bulbs and one fan, the fan speed will be controllable through the slider or 4 mode buttons.So i ned 4 more buttons and slider/button for speed control.....How do i create those!!!Can you please help me with this!!!!!
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Willi 2 years ago
I want 2 esp8266 12e modules should be connected as server client configuration and gpio's of both modules should be controlled with one android app
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Carlos del Gato 3 years ago
As of yesterday, it has become an off-the-shelf solution to use any of ITEAD's products - with the update of the eWeLink Smart Home app. And ALEXA discovers the devices now so it is as simple as asking to turn on or off by name. COOL. All of our SONOFF units are now working with the Echo perfectly.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Gregory Carlos del Gato a year ago
You are not telling the whole truth -you must be from Marketing. ITead and Alexa (et al) need a massive extra infrastructure to be able to switch a light on. Marco's admirable solution here does NOT use the internet. So it wins. FO Clouds.
Tony Knors 3 years ago
Hello Marco, thanks for this great example ! I am a newbie on ESP8266 and REST but have some experience with Python, Raspberry ... I was trying to make a few simple changes like adding additional buttons to control additional ports. Simply done by adding a rest.button(6), rest.button(7) ...but .. How can you put "text" in between them so that you could have for example :Garden ON OFFKitchen ON OFF...All help is greatly appreciated !!!Kind Regards,Tony
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Hasan 3 years ago
i was work as this project but with current sensor(relay + current sensor ) , can you help me pls i have code but i dont know why dont read real value any one can you help me pls < my gmail. is : [email protected]
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Channa Ranatunga Hasan 3 years ago
Change the Baud rate to 115200.