Thursday, April 30, 2015

Esp8266 Control Electrical Devices From your browser.


WiFi web server:

This program connected to the wifi home server that is specified by the user, Once it connected to the homeserver, you can check the IP address assigned to the esp8266 by logging into your WIFI modem and check for the DHCP Clients List. Open a broswer and type in the IP address that your esp8266 has associated with, you will see the webpage returned by the module and wait for the command from the user. 


check the video below to know how to do this project











Make sure you are connecting to the correct pin, whatever the pin number you are assigned in your lua program , the same pin has to be connected with the relay module.check the GPIO table.


Download the nodemcu flasher from here

Download the latest lua firmware from here

Download ESPlorer from here

Download the Lua program here (Make sure you are uploading the file in the name init.lua )


Hi, in this tutorial we will see how to control electrical devices like fan, light, etc., to turn on and off using esp8266 from a web browser. if you are bored with a dedicated device controller like an app or an remote which will be available for only one particular device but using this method all the device which support web browsing will be act as a  controller for us.

Make sure all the devices are connected to the same router, this example doesn't include a port forwarding function which will not allow us to control the device from outside the home network. 

Components that you need for completing this project are very simple, you need to have an esp8266 wifi module and a relay, make sure you buying a 5v relay which very easy to use with esp chips doesn't require external supply too. we can make use of the Vin pin of the nodemcu or if you are using a generic chip, you need to supply an external 5v to the relay.


For this example project I have used only 2 relay circuit, but the actual program wrote for connecting four relay module. 


You can check the above video on how this thing works and how to connect your browser to the ip address returned from esp and all the details are included in this video.





Copy the below arduino code and paste into your Arduino IDE and upload the program to your nodemcu or any other esp devices that you are using, make sure to choose the correct port and device name from the board. also don't forget to change the SSID and password to your Wi-fi settings.

This program for the esp8266 wrote to return the status of the device , which will in turn notify us with the device state in the browser which will also make the user to know which device has currently turned on or off. 

----------------------------------------------------------------------------------------------------------------------------



#include <ESP8266WiFi.h>
 
const char* ssid = "Magesh";
const char* password = "jayakumar";
 
; // 
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  digitalWrite(0, LOW);
  digitalWrite(13, LOW);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  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.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
 
  if (request.indexOf("/light1on") > 0)  {
    digitalWrite(5, HIGH);
   
  }
  if (request.indexOf("/light1off") >0)  {
    digitalWrite(5, LOW);
   
  }

   if (request.indexOf("/light2on") > 0)  {
    digitalWrite(4, HIGH);
   
  }
  if (request.indexOf("/light2off") >0)  {
    digitalWrite(4, LOW);
   
  }
    if (request.indexOf("/light3on") >0)  {
    digitalWrite(0, HIGH);
   
  }
  if (request.indexOf("/light3off") > 0)  {
    digitalWrite(0, LOW);
   
  }
   if (request.indexOf("/light4on") > 0)  {
    digitalWrite(13, HIGH);
   
  }
  if (request.indexOf("/light4off") > 0)  {
    digitalWrite(13, LOW);
   
  }
// Set ledPin according to the request
//digitalWrite(ledPin, value);
 
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head>");
  client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
 client.println("</head>");
  client.println("<body bgcolor = \"#f7e6ec\">"); 
  client.println("<hr/><hr>");
  client.println("<h4><center> Esp8266 Electrical Device Control </center></h4>");
  client.println("<hr/><hr>");
  client.println("<br><br>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("Device 1");
  client.println("<a href=\"/light1on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light1off\"\"><button>Turn Off </button></a><br />");  
  client.println("</center>");   
  client.println("<br><br>");
   client.println("<center>");
   client.println("Device 2");
  client.println("<a href=\"/light2on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light2off\"\"><button>Turn Off </button></a><br />");  
client.println("</center>"); 
  client.println("<br><br>");
    client.println("<center>");
   client.println("Device 3");
  client.println("<a href=\"/light3on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light3off\"\"><button>Turn Off </button></a><br />");  
client.println("</center>"); 
  client.println("<br><br>");
   client.println("<center>");
   client.println("Device 4");
  client.println("<a href=\"/light4on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light4off\"\"><button>Turn Off </button></a><br />");  
client.println("</center>"); 
  client.println("<br><br>");
  client.println("<center>");
  client.println("<table border=\"5\">");
 client.println("<tr>");
  if (digitalRead(5))
         { 
           client.print("<td>Light 1 is ON</td>");
        
         }
          else
          {
            client.print("<td>Light 1 is OFF</td>");
      
        }
     
        client.println("<br />");
             
         if (digitalRead(4))
          { 
           client.print("<td>Light 2 is ON</td>");

         }
          else
          {

            client.print("<td>Light 2 is OFF</td>");

          }
          client.println("</tr>");


          client.println("<tr>");

          if (digitalRead(0))

          { 
           client.print("<td>Light 3 is ON</td>");

          }

          else

          {
            client.print("<td>Light 3 is OFF</td>");
          }


          if (digitalRead(13))


          { 


           client.print("<td>Light 4 is ON</td>");

          }


          else


          {


            client.print("<td>Light 4 is OFF</td>");


          }

          client.println("</tr>");


          client.println("</table>");

          client.println("</center>");
  client.println("</html>"); 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}
---------------------------------------------------------------------------------------------




Copy the above code and complete the process. Share and let others know about this tiny chip which can do dozens of magic.





if you like the above tutorial and if you want try out with cool projects you can also check this link here , that's the amazon book link where you can use that book to make IoT with Esp8266 or Nodemcu, that books gives you basic coverage on how to do simple things and get yourself started with arduino and goes on developing projects like sending data to webserver and creating a webserver, uploading and controlling data from a webpage, how to interface TFT LCD and I2C devices and many more things can find on the link.

10 comments :

  1. Hi hope you are fine, i have a problem with me code when i try to turn the LED on its going to OFF, when i try to turn the LED OFF its going to ON what should i do many thanks for your help:

    wifi.setmode(wifi.STATION)
    wifi.sta.config("SSID ","Pass")
    print(wifi.sta.getip())
    gpio.mode(3, gpio.OUTPUT)
    srv=net.createServer(net.TCP, 1)
    srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
    local buf = "";
    local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
    if(method == nil)then
    _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
    end
    local _GET = {}
    if (vars ~= nil)then
    for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
    _GET[k] = v
    end
    end
    html ---------------------------------

    local _on,_off = "",""

    if(_GET.pin == "ON1")then
    gpio.write(3, gpio.HIGH);
    elseif(_GET.pin == "OFF1")then
    gpio.write(3, gpio.LOW);

    end
    client:send(buf);
    client:close();
    collectgarbage();
    end)
    end)

    ReplyDelete
  2. i have watched your video ......
    i appreciate a lot ......especially on the use of more gpio output pins......
    i am working on the project which ....has an esp8266 wiith only 2 gpios ,gpio 0 and gpio 2......,i have succeded flashing the firmware using nodemcu flasher.....problem comes when uploading the lua code..it takes tooo long no error but just showing busy......i need to know the precise connections ...i have removed gpio 0 from ground and put gpio it to VCC.....wat about rx and tx and ch_pd....and reset..and gpio 2.....

    ReplyDelete
  3. Hello,
    I have a small problem the web interface does not work on iphone
    do you have a solution
    Thank you

    ReplyDelete
  4. how can i get the ESP8266WiFi.h library? can i have a download link sir :) thank you :)

    ReplyDelete
  5. Hi i want to make a home automation for my home ,
    Could you pls help me out in this since i am not an electonics guy.
    Want to talk to you regarding this.
    You can contact me in my mail santoshranjandas@gmail.com
    or call me on 9022083993

    Thanks

    ReplyDelete
  6. HI,

    Can the same project be done to control relays from android phone and coding in Arduino IDE .

    ReplyDelete
  7. Can we do this project with esp8266-01.

    ReplyDelete
  8. Hi all,

    I have interfaced Arduino Uno with ESP 8266 wifi module and LCD . Now i want to send text from text box in webpage and when i click on submit button ,the etxt should appear on LCD through wireless communciation. Can you suugest the circuit diagram and code.

    ReplyDelete
  9. Can you make a project which is a combination of input and output both

    Great project

    ReplyDelete