Arduino Home automation with 8 channel relay

Hello world! Today i made a Arduino Home automation system with a 8 channel relay
check out the video below!

Schematic:
Arduino Home automation

You need to include the SPI and Ethernet Libraries.
You probably need to change the IP Address and gateway. Subnet will be as it is in most cases.
You can find this below on the top of the code to change IP and gateway.

byte mac[] = { 0xAE, 0xFE, 0xDE, 0xEF, 0xFD, 0xFD }; //You can change the mac address to!
byte gateway[] = { 192, 168, *, *** }; //
byte subnet[] = { 255, 255, 255, 0 }; //
ip(192,168,*,**);

To find out what your gateway
Select Start > Run. Type command into the dialog box, then click OK.In the command line window, type ipconfig /all.
Here you will find your gateway.

To find the control panel. Just go to the IP adress you assigned to the Ethernet Shield in your browser, phone or tablets browser. From here you can Control each relay. Good luck with your Arduino Home automation system.

Parts list:
Arduino Mega
8 channel relay
w5100 ethernet shield

The code:

/*
  Author: Danny van den Brande, arduinosensors.nl. BlueCore tech.
  This Arduino Home automation with 8 channel relay is one of my first projects. The code needs some work but the     basics to build from are there.Good luck building your Arduino Home automation.
 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xAE, 0xFE, 0xDE, 0xEF, 0xFD, 0xFD };


byte gateway[] = { 192, 168, *, *** }; //               <------- PUT YOUR ROUTERS IP Address to which your shield is connected Here
byte subnet[] = { 255, 255, 255, 0 }; //                <------- It will be as it is in most of the cases
IPAddress ip(192,168,*,**);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);


#define RELAY_CH1  2
#define RELAY_CH2  3
#define RELAY_CH3  4
#define RELAY_CH4  5
#define RELAY_CH5  6
#define RELAY_CH6  7
#define RELAY_CH7  8
#define RELAY_CH8  9

String readString;

void setup() {
  
  
        pinMode(RELAY_CH1, OUTPUT); 
   digitalWrite(RELAY_CH1, HIGH);  // switch on LED1
 
        pinMode(RELAY_CH2, OUTPUT); 
  digitalWrite(RELAY_CH2, HIGH);  // switch on LED2
 
        pinMode(RELAY_CH3, OUTPUT); 
  digitalWrite(RELAY_CH3, HIGH);  // switch on LED3
 
        pinMode(RELAY_CH4, OUTPUT); 
  digitalWrite(RELAY_CH4, HIGH);  // switch on LED4
 
        pinMode(RELAY_CH5, OUTPUT); 
  digitalWrite(RELAY_CH5, HIGH);  // switch on LED5
 
        pinMode(RELAY_CH6, OUTPUT); 
  digitalWrite(RELAY_CH6, HIGH);  // switch on LED6
 
        pinMode(RELAY_CH7, OUTPUT); 
  digitalWrite(RELAY_CH7, HIGH);  // switch on LED7
 
        pinMode(RELAY_CH8, OUTPUT); 
  digitalWrite(RELAY_CH8, HIGH);  // switch on LED8
  
  
  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP()); 
  
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
           
      if (client.available()) {
        
        char c = client.read();
        
        if (readString.length() < 100) {
          //store characters to string
          readString += c;      //Serial.print(c);
        }
        
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          //client.println("Connection: close");  // the connection will be closed after completion of the response
          //client.println("Refresh: 20");  // refresh the page automatically every 5 sec
          client.println();
          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("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://vandenbrande3ddesign.com/homeautomation.css\" />");
          client.println("<meta http-equiv=\"refresh\" content=\"5;url=http://192.168.2.10/\"/>" );
          client.println("</HEAD>");
          
          client.println("<body bgcolor=\"#333333\">");
          //client.println("<hr/>");
          //client.println("<hr/>");
          
          client.println("<h4><center><img border=\"2\" src=\"http://i809.photobucket.com/albums/zz12/pocolocoaboma/Logo.jpg\" /></center></h4>");
          //client.println("<hr/>");
          //client.println("<hr/>");
                   
          client.print("<center> <p> <h1></h1></p> ");
          //client.println("<br />");
          //client.println("<br />");
          
          
          // Relay Status Display
          //client.println("<center>");
          
              //client.println("<table border=\"5\">");
              
                  //client.println("<tr>"); 
                      //if (digitalRead(RELAY_CH1))
                      { 
                           //client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 1.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p> </td>");
                      }
                      //else
                      {
                           //client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 1.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                      
                                  
                      //if (digitalRead(RELAY_CH2))
                      {   
                           //client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 2.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
                      }
                      //else
                      {  
                          //client.print("<td> <p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 2.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                     
                      
                     //if (digitalRead(RELAY_CH3))
                      { 
                           //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 3.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
                      }
                      //else
                      {
                          //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 3.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                     
                     
                      //if (digitalRead(RELAY_CH4))
                      { 
                         //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 4.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
                      }
                      //else
                      {
                          //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 4.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                     
                     
                      //if (digitalRead(RELAY_CH5))
                      { 
                           //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 5.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
                      }
                      //else
                      {
                          //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 5.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                     
                             
                      //if (digitalRead(RELAY_CH6))
                      {   
                           //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 6.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
                      }
                      //else
                      {  
                          //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 6.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                      
                          
                      //if (digitalRead(RELAY_CH7))
                      { 
                           //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 7.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
                      }
                      //else
                      {
                          //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 7.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                     
                     
                      //if (digitalRead(RELAY_CH8))
                      { 
                         //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 8.</p><p style=\"font-family:arial;color:green;font-size:35px;\">ON</p></td>");
                      }
                      //else
                      {
                          //client.print("<td><p style=\"font-family:arial;color:black;font-size:26px;\">Lamp 8.</p><p style=\"font-family:arial;color:red;font-size:35px;\">OFF</p></td>");
                      }
                                         
                 //client.println("</tr>");                   
              //client.println("</table>"); 
              
          //client.println("</center>");
          //client.println("<br />");
//End relay status display
          
          
         /* Relay Control Code  */                
        
          client.println("<a href=\"/?relay1off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\"; color:red ;>Lamp 1 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay1on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 1 ON </font> </button> </a> <br />"); 
          client.println("<br />");
          

          client.println("<a href=\"/?relay2off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 2 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay2on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 2 ON </font> </button> </a> <br />"); 
          client.println("<br />");
          
          
          client.println("<a href=\"/?relay3off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 3 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay3on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 3 ON </font> </button> </a> <br />"); 
          client.println("<br />");
          

          client.println("<a href=\"/?relay4off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 4 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay4on\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 4 ON </font> </button> </a> <br />"); 
          client.println("<br />");
         
          
          client.println("<a href=\"/?relay5off\"\"> <button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 5 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay5on\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 5 ON </font> </button> </a> <br />"); 
          client.println("<br />");        


          client.println("<a href=\"/?relay6off\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 6 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay6on\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 6 ON </font> </button> </a> <br />"); 
          client.println("<br />");
         
          
          client.println("<a href=\"/?relay7off\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 7 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay7on\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 7 ON </font> </button> </a> <br />");  
          client.println("<br />");
          

          client.println("<a href=\"/?relay8off\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 8 OFF </font> </button> </a> ");
          client.println("<a href=\"/?relay8on\"\"><button style=\"width:360px;height:120px\"> <font size=\"7\">Lamp 8 ON </font> </button> </a> <br />"); 
          client.println("<br />");
         
          
          // control arduino pin via ethernet Start //

        if(readString.indexOf("?relay1off") >0)//checks for on
        {
            digitalWrite(RELAY_CH1, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay1on") >0)//checks for off
          {
            digitalWrite(RELAY_CH1, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
          
          
         if(readString.indexOf("?relay2off") >0)//checks for on
        {
            digitalWrite(RELAY_CH2, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay2on") >0)//checks for off
          {
            digitalWrite(RELAY_CH2, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
          
          
          if(readString.indexOf("?relay3off") >0)//checks for on
        {
            digitalWrite(RELAY_CH3, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay3on") >0)//checks for off
          {
            digitalWrite(RELAY_CH3, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
        
        if(readString.indexOf("?relay4off") >0)//checks for on
        {
            digitalWrite(RELAY_CH4, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay4on") >0)//checks for off
          {
            digitalWrite(RELAY_CH4, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
        
         if(readString.indexOf("?relay5off") >0)//checks for on
        {
            digitalWrite(RELAY_CH5, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay5on") >0)//checks for off
          {
            digitalWrite(RELAY_CH5, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
          
          
         if(readString.indexOf("?relay6off") >0)//checks for on
        {
            digitalWrite(RELAY_CH6, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay6on") >0)//checks for off
          {
            digitalWrite(RELAY_CH6, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
          
          
          if(readString.indexOf("?relay7off") >0)//checks for on
        {
            digitalWrite(RELAY_CH7, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay7on") >0)//checks for off
          {
            digitalWrite(RELAY_CH7, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
        
        if(readString.indexOf("?relay8off") >0)//checks for on
        {
            digitalWrite(RELAY_CH8, HIGH);    // set pin 4 high
            //Serial.println("Led On");
      
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
            //client.println("Light 1 Is On");
            client.println("<br />");
  }
        else{
          if(readString.indexOf("?relay8on") >0)//checks for off
          {
            digitalWrite(RELAY_CH8, LOW);    // set pin 4 low
            //Serial.println("Led Off");
            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

      //client.println("Light 1 Is Off");
            client.println("<br />");
          }
        }
                    
        // INPUT
          
          
          // output the value of each analog input pin
          //for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
          //  int sensorReading = analogRead(analogChannel);
          //  client.print("analog input ");
          //  client.print(analogChannel);
          //  client.print(" is ");
          //  client.print(sensorReading);
          //  client.println("<br />");       
          //}
                   
          client.println("<hr> <p> By <a href=\"https://www.facebook.com/dendan.vandenbrande.9?fref=ts\"></p><p style=\"font-family:arial;color:black;font-size:20px;\">Danny van den Brande</p></a>");
                  
           readString="";
           client.println("</body>");         
           client.println("</html>");
         
          break;
        }// if ( c == \n )
        
        
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
                
      }
    }
    
    // give the web browser time to receive the data   
    delay(1);
    //client.println("<meta http-equiv=\"refresh\" content=\"10;url=http://192.168.0.199/\"/>" );
    
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
    
  }//if(Client)
  
}//Loop

Buy sensors here: http://arduinosensors.nl

Danny van den Brande

http://www.arduinosensors.nl

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

Your email address will not be published. Required fields are marked *