Controlling Appliances over WiFi and Ethernet

details

Description:

This a simple tutorial on how to setup your Arduino board with Ethernet shield to control your home appliances like Lights or Fans using any device (Mobile/Tablet/Laptop/Desktop) connected to your WiFi network.

Components:

1. Arduino Ethernet Shield
2. Arduino Uno (or any other Arduino board compatible with the shield)
3. Relay Board (Number of Relays depend on how many appliances you want to control)
4. WiFi router
5. Ethernet cables
6. Wires.
Block Diagram:
 47

 

We basically create an HTML page from Arduino which can be accessed by the devices connected to the network from their respective internet browsers. So we don’t need to install any specific application in each device you want to control your appliance with.
You just need to know some basics of HTML (which I will cover here) but the better you know, the cooler your web interface would look!
#include <SPI.h>
#include <Ethernet.h>

int relay = 3;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 0, 108 };                      // ip in lan (that's what you need to use in your browser. ("192.168.0.108")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port    
String readString;

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(relay, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {  
      if (client.available()) {
        char c = client.read();
    
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {         
           Serial.println(readString); //print to serial monitor for debuging
    
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();    
           client.println("<HTML>");
           client.println("<HEAD>");

           client.println("<TITLE>WEB INTERFACE</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<center>");
           client.println("WELCOME TO THE WEB INTERFACE OF APPLIANCE CONTROLLER OVER WIFI");
           client.println("<hr />");
           client.println("<br />"); 
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("<br />"); 
           client.println("<a href=\"/?relay1on\">Switch ON Relay 1</a>");
           client.println("<a href=\"/?relay1off\">Switch OFF Relay 1</a><br />");  
           client.println("<br />");    
           client.println("<br />");
           
           client.println("<p>Created by GANESH SELVARAJ for EngineersGarage.com</p>"); 
           client.println("<br />");
           client.println("</center>");
           client.println("</BODY>");
           client.println("</HTML>");
    
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?relay1on") >0){
               digitalWrite(relay, HIGH);
           }
           if (readString.indexOf("?relay1off") >0){
               digitalWrite(relay, LOW);
           }
         
            //clearing string for next read
            readString=""; 
          
         }
       }
    }
}
}

 

Demo:
https://www.youtube.com/watch?v=pNOaZu7SSi0&feature=youtu.be
Tags:201802,Ethernet Shield W5100,Relay Board, WiFi router
Author:
Ganesh Selvaraj

Description:

This a simple tutorial on how to setup your Arduino board with Ethernet shield to control your home appliances like Lights or Fans using any device (Mobile/Tablet/Laptop/Desktop) connected to your WiFi network.

Components:

1. Arduino Ethernet Shield
2. Arduino Uno (or any other Arduino board compatible with the shield)
3. Relay Board (Number of Relays depend on how many appliances you want to control)
4. WiFi router
5. Ethernet cables
6. Wires.
Block Diagram:
 47

 

We basically create an HTML page from Arduino which can be accessed by the devices connected to the network from their respective internet browsers. So we don’t need to install any specific application in each device you want to control your appliance with.
You just need to know some basics of HTML (which I will cover here) but the better you know, the cooler your web interface would look!
#include <SPI.h>
#include <Ethernet.h>

int relay = 3;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 0, 108 };                      // ip in lan (that's what you need to use in your browser. ("192.168.0.108")
byte gateway[] = { 192, 168, 1, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port    
String readString;

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(relay, OUTPUT);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {  
      if (client.available()) {
        char c = client.read();
    
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {         
           Serial.println(readString); //print to serial monitor for debuging
    
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();    
           client.println("<HTML>");
           client.println("<HEAD>");

           client.println("<TITLE>WEB INTERFACE</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<center>");
           client.println("WELCOME TO THE WEB INTERFACE OF APPLIANCE CONTROLLER OVER WIFI");
           client.println("<hr />");
           client.println("<br />"); 
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("<br />"); 
           client.println("<a href=\"/?relay1on\">Switch ON Relay 1</a>");
           client.println("<a href=\"/?relay1off\">Switch OFF Relay 1</a><br />");  
           client.println("<br />");    
           client.println("<br />");
           
           client.println("<p>Created by GANESH SELVARAJ for EngineersGarage.com</p>"); 
           client.println("<br />");
           client.println("</center>");
           client.println("</BODY>");
           client.println("</HTML>");
    
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?relay1on") >0){
               digitalWrite(relay, HIGH);
           }
           if (readString.indexOf("?relay1off") >0){
               digitalWrite(relay, LOW);
           }
         
            //clearing string for next read
            readString=""; 
          
         }
       }
    }
}
}

 

Demo:
https://www.youtube.com/watch?v=pNOaZu7SSi0&feature=youtu.be
Tags:201802,Ethernet Shield W5100,Relay Board, WiFi router
Author:
Ganesh Selvaraj

COMMENTS

Please Login to comment
  Subscribe  
Notify of