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:





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





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:





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:





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!