ESP8266 wifi light switch

Saturday, July 8, 2017

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 (220VAC to 5VDC)
(3) Power module (5VDC to 3.3VDC)
(4) Electronics: relay 5VDC, transistor, resistor

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

No comments:

Post a Comment


Search This Blog

Most Reading

Google+ Followers