Control a Lamp Remotely Using the ESP8266 WiFi Chip
The ESP8266 is an amazing WiFi chip that can be used in several home automation applications. In this tutorial, we are going to use it to control a lamp remotely via WiFi. We will 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. The interface is fully responsive, so it can also be used with your phone or tablet!
What You'll Need
| Component | Qty | |
|---|---|---|
| ESP8266 Olimex module | 1 | |
| Breadboard 3.3V power supply | 1 | |
| 3.3V FTDI USB module | 1 | |
| PowerSwitch Tail Kit | 1 | |
| Breadboard | 1 | |
| Jumper wires | Multiple | |
| Desk lamp or electrical device | 1 |
Step by Step
Hardware Configuration
<p>First, set up your ESP8266 chip following the getting started guide at https://www.openhomeautomation.net/getting-started-esp8266/. The only additional component you need to add is the PowerSwitch Tail Kit. Connect the two pins on the right (-in and Ground) to the ground of your 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, but you will need to modify your code accordingly. Finally, connect a lamp or any electrical device to the PowerSwitch, and the other end of the PowerSwitch to the mains electricity.</p>
Install Required Libraries
<p>You will need to install the following libraries for this project using the Arduino IDE Library Manager:</p><ul><li><a target="_blank" rel="noopener noreferrer nofollow" href="https://github.com/marcoschwartz/aREST">aREST</a></li><li><a target="_blank" rel="noopener noreferrer nofollow" href="https://github.com/marcoschwartz/aREST_UI">aREST UI</a></li></ul><p>These libraries will handle the web server and UI generation for controlling the relay.</p>
Write and Upload the Code
<p>We will use the Arduino IDE to configure the ESP8266 chip. The code starts by declaring the libraries and creating an aREST UI object:</p><p>First, declare the libraries:</p><pre><code>#include <ESP8266WiFi.h> #include <aREST.h> #include <aREST_UI.h></code></pre><p>Then, create the aREST UI object:</p><pre><code>aREST_UI rest = aREST_UI();</code></pre><p>Add your WiFi credentials:</p><pre><code>const char* ssid = "your_wifi_name"; const char* password = "your_wifi_password";</code></pre><p>In the setup() function, build the interface with a button linked to pin 5:</p><pre><code>rest.title("Relay Control"); rest.button(5);</code></pre><p>Connect to WiFi:</p><pre><code>WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); server.begin(); Serial.println("Server started"); Serial.println(WiFi.localIP());</code></pre><p>In the loop() function, handle incoming connections:</p><pre><code>WiFiClient client = server.available(); if (!client) { return; } while(!client.available()){ delay(1); } rest.handle(client);</code></pre><p>You can find the complete code at: <a target="_blank" rel="noopener noreferrer nofollow" href="https://github.com/openhomeautomation/esp8266-relay">https://github.com/openhomeautomation/esp8266-relay</a></p><p>Select the ESP8266 board in the Arduino IDE. Make sure you choose the option that corresponds to your board, especially if you are using the Olimex board. Verify that you have changed your WiFi name and password in the sketch.</p><p>Put the chip in bootloader mode by connecting GPIO0 to GND, then power cycle the board. Upload the sketch, disconnect GPIO0 from GND, and power cycle again.</p>
Access the Web Interface
<p>Open the Serial monitor in the Arduino IDE to view the IP address assigned to your ESP8266 board. Open your favorite web browser and type in the board's IP address. You should see the control interface with On/Off buttons for the relay.</p><p>Click the buttons to test the functionality - you should notice that the state of the PowerSwitch Tail Kit or relay changes instantly. The interface is fully responsive thanks to the aREST UI library, so it will automatically adapt to the device you're using, whether it's a desktop browser, tablet, or smartphone.</p>
Wrapping Up
In this project, we built 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 and responsive interface, allowing you to control the electrical device from any device within your local WiFi network. The beauty of this approach is that it's completely independent from your computer - the ESP8266 handles everything on its own. You can use what you learned in this project to build other home automation applications, such as a completely autonomous sensor module that measures data from several sensors and displays them all in a single interface served by the ESP8266 module.