Full width home advertisement

Arduino project

Raspberry project

Post Page Advertisement [Top]

This IoT project will guide making ESP8266 wifi light switch on wall, which uses relay 5VDC to control lamp remotely.
The lamp can be turned on/off by phone (via wifi) or by local switch. In case no phone, we still control it by phone. In case of ...lazy, we can control lamp via local wifi network.
Result video:

Hardware need:
(1) ESP8266
(2) Power module 220V to 5VDC
(3) Power module 5VDC to 3.3VDC
(4) Electronics: relay 5VDC, transistor, resistor
1. ESP8266
3. Power module 5VDC to 3.3V
2. Power module 220V to 5VDC

Software need:
(1) Arduino IDE
(2) Firmware for ESP8266

Step 1. Make circuit

Step 2. Programming ESP8266
Using Arduino IDE to program firmware for ESP. To know pramming by Arduino IDE, please see our article link here
After first download, ESP8266 will be downloaded by OTA (over the air), instruction to do it link here
Download OTA helps us update firmware ESP8266 without touching the board. It's easy for next modify when the board is already installed to the wall.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//------------------------------for local web
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

MDNSResponder mdns;
ESP8266WebServer server(80);
String webPage;
//----------------------------for local web

const char* ssid = "NguyenTuong";
const char* password = "064897435";
int pinout=14;  //for relay
int pinsw=12;  //for switch
boolean light_change;
boolean sw_status;

void setup() {
  pinMode(pinout, OUTPUT);
  pinMode(pinsw, INPUT);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");

  // No authentication by default
  ArduinoOTA.setPassword((const char *)"123");

  ArduinoOTA.onStart([]() {
  ArduinoOTA.onEnd([]() {
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  Serial.print("IP address: ");

  //-----------------------------------------add your code here
  webPage += "<center><font size='27'>ESP8266 Web Server<p>Light #1 ";
  webPage += "<a href=\"light1_change_status\"><mark>TOGGLE it";
  webPage += "</mark></a></p></font></center>";

  if (mdns.begin("esp8266", WiFi.localIP())) 
    Serial.println("MDNS responder started");
  server.on("/", [](){
    server.send(200, "text/html", webPage);
  server.on("/light1_change_status", [](){
    server.send(200, "text/html", webPage);
    // Change status
    light_change = true;

  //-------------------------------------------add your code here

void loop() {
  //-------------------------------------------add your code here
    light_change = false;
    if(digitalRead(pinout)) digitalWrite(pinout,LOW);
    else digitalWrite(pinout,HIGH);

  if(sw_status != digitalRead(pinsw))
    sw_status = digitalRead(pinsw);
    light_change = true;

Step 3. Test it
Here is our real model, looks small:

Overall connection:

This module has IP address
After power on, ESP will connect to local wifi network. Type IP address into web browser, a local web server will show, relay can be controlled from here


  1. Very nice project. Good documentation. You should be teaching thus stuff.

    1. i am not able to control the relay from the browser. i have done all the above mentioned steps but still no success instead the relay is closing and opening in a loop. please help.

    2. Check if GPIO14 can be ON/OFF. If yes, check transistor, or power supply for transistor not enough for relay operation.

  2. Near the lamp, on the relay, should be 5v instead of 3.3v, right?

    1. yes, you're right. If you use 5VDC relay, it should be 5V.

    2. I do not know how to you feeback ?

  3. useful project. Thanks

  4. hi.i was wondering if i can connect a esp8266 12E directly to an arduino uno and program them so i could give order to esp8266(for example turn GPIO14 high) over wifi and then arduino read the esp 14 pins that is connected to on of its digital pins(8) as input and alter one of another digital pins(like 10 or 11). is this possible?

    1. Hi, i think it is possible. Please search on Google or Youtube about the way program ESP12E by UNO.
      ESP12E is also possible connecting with other Arduino board (UNO, Pro Mini) to control GPIO -> You can use UART, or SPI communication between ESP12 and other Arduino board.

    2. I am using the same code on nodemcu to control switch directly from GPIO pins,I think using transistor you can get more reliable results , maybe !! :P

      @engineer2you Thankyou , found exactly what i was looking for :)....

  5. Good little project. Some ideas for improvement in the circuit. GPIO pins could control opto-isolation LED, and on the other side, power the Relay with a different voltage source. That way, relay back EMF won't enter ESP8266 circuit. Also, a discussion about HomeAssistant integration would be fantastic.

  6. One way to test if your relay will glitch your ESP8266 is to click the relay off and on with a high duty-cycle -- say 80Hz.

  7. Fantastic and useful we blog thanks for publishing this.it's useful and informative.keep up the great.
    2 Way 2gang Wireless Light Touch Switch

  8. hi nice project.
    can u pls tell me if i have to use "two way" switch or ordinary switch?

    1. use "two way" if you need control light remotely

  9. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. Unique wifi names

  10. what's extra information you need?


Bottom Ad [Post Page]