Translate

Membuat Alat Kendali Peralatan Rumah Tangga / RELAY Menggunakan WIFI dengan INPUT Jadwal Jam ON / OFF WEBSERVER ARDUINO

Membuat Alat Kendali Peralatan Rumah Tangga / RELAY Menggunakan WIFI dan  INPUT Jadwal  /Jam ON - OFF WEBSERVER ARDUINO RTC


       Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang menggunakan wifi sebagai media alat kendali ON/OFF device dengan input jadwalnya menggunakan browser, jadi sistemnya yaitu webserver yang mana user bisa memasukkan jam dan tanggal kapan alat hidup dan kapan alat mati secara otomatis dengan menggunakan browser, koneksi yang digunakan yaitu wifi sehingga memudahkan user untuk instalasinya, alat ini menggunakan Arduino sebagai kontrollernya dan ethernet shield sebagai jembatan menuju ke WIFI. alat ini juga dilengkapi dengan RTC DS1302 dan 4 buah relay sebagai kendali device, jadi jika jam sekarang sudah sama dengan jam yang diinputkan maka device akan on/off sesuai jadwal yang dimasukkan. untuk lebih jelasnya berikut adalah skema dan programnya.




a. Arduino Mega





b. RTC DS1302





c. Ethernet Shield





d. Modul Relay





e.  Router






f. Program Arduino IDE

-PROGRAM INPUT JADWAL

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

// Init the DS1302
DS1302 rtc(2, 3, 4);

int onkip;
int offkip;
int onkul;
int offkul ;
int onlt;
int offlt;
int onls;
int offls;


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 102 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80);; //server port

String readString;
Time t;


void setup(void){

  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
   
  rtc.halt(false);
  rtc.writeProtect(false);

  // The following lines can be commented out to use the values already stored in the DS1302
  rtc.setDOW(WEDNESDAY);        // Set Day-of-Week to FRIDAY
  rtc.setTime(20, 59, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(4, 5, 2017);   // Set the date to August 6th, 2010
 
  rtc.writeProtect(true);
 
  Wire.begin();

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("servertest1"); // so I can keep track of what is loaded
}



void loop(void){

t = rtc.getTime();

  // 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;

        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString);

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println("Refresh: 1");        
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>INPUT JADWAL</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H3>kontrol ON / OFF dan delay lampu");
          client.println("<BR>");
          client.print(rtc.getTimeStr());
          client.print(" ");
          client.println(rtc.getDOWStr(FORMAT_LONG));
          client.print(" ");
          client.print(rtc.getDateStr(FORMAT_LONG,FORMAT_LITTLEENDIAN, '/'));

          client.println("<BR>");
         
          client.println("<BR></H3>");
          client.println("<FORM ACTION=\"http://192.168.0.102:80\" method=get >");

          client.println("JAM ON KIPAS  : <INPUT TYPE=TEXT NAME=\"onkip\" VALUE=\"A\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
          client.println("JAM OFF KIPAS: <INPUT TYPE=TEXT NAME=\"offkip\" VALUE=\"B\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
         
          client.println("JAM ON KULKAS : <INPUT TYPE=TEXT NAME=\"onkul\" VALUE=\"C\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>"); 
          client.println("JAM OFF KULKAS : <INPUT TYPE=TEXT NAME=\"offkul\" VALUE=\"D\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
         
          client.println("JAM ON Lampu Tengah : <INPUT TYPE=TEXT NAME=\"onlt\" VALUE=\"E\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
          client.println("JAM OFF Lampu Tengah : <INPUT TYPE=TEXT NAME=\"offlt\" VALUE=\"F\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");

          client.println("JAM ON Lampu Teras : <INPUT TYPE=TEXT NAME=\"onls\" VALUE=\"G\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
          client.println("JAM OFF Lampu Teras : <INPUT TYPE=TEXT NAME=\"offls\" VALUE=\"H\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");

          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\" SUBMIT\">");
        
          client.println("</FORM>");
          client.println("<BR>");
          client.println("<h5>");   
          client.println("SET ON KIPAS = ");
          client.println(onkip);
            client.println("<BR>");
          client.println("SET OFF KIPAS = ");
          client.println(offkip);
            client.println("<BR>");
          client.println("SET ON KULKAS = ");
          client.println(onkul);
            client.println("<BR>");
          client.println("SET OFF KULKAS = ");
          client.println(offkul);
            client.println("<BR>");
          client.println("SET ON LAMPU TENGAH = ");
          client.println(onlt);
            client.println("<BR>");
          client.println("SET OFF LAMPU TENGAH =");
          client.println(offlt);
            client.println("<BR>");
          client.println("SET ON LAMPU TERAS = ");
          client.println(onls);
            client.println("<BR>");
          client.println("SET OFF LAMPU TERAS =");
          client.println(offls);
            client.println("<BR>");
           
          client.println("</h5>");
          client.println("<BR>");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client

          client.stop();
        
        

        
          //==============================================
          if(readString.indexOf("A7") >0)//checks for on
          {
             onkip = 21;          
          }

                 
          if(readString.indexOf("B8") >0)//checks for on
          {
             offkip = 8;
          }
        
              
          if(readString.indexOf("C10") >0)//checks for on
          {
             onkul = 1000;
           
          }

                
          if(readString.indexOf("D2") >0)//checks for off
          {
             offkul = 2;
          }
        
        
          if(readString.indexOf("E2") >0)//checks for off
          {
            onlt = 2;
          }
         
          if(readString.indexOf("F2") >0)//checks for off
          {
            offlt = 2;
          }
         
          if(readString.indexOf("G2") >0)//checks for off
          {
            onls = 2;
          }
         
          if(readString.indexOf("H2") >0)//checks for off
          {
            offls = 2;
          }
         
          //===================================
        //   Serial.print(rtc.getDOWStr());
        //   Serial.print(" ");
        //   Serial.print(rtc.getDOWStr(FORMAT_LONG));
        //   Serial.print(" -- ");
        //   Serial.println(rtc.getTimeStr());
 
          if((t.hour == onkip)&&(t.hour > 0)){
          digitalWrite(A0,HIGH);
          }

        
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}





- PROGRAM INPUT JADWAL + KENDALI + MONITORING ADC

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

// Init the DS1302
DS1302 rtc(2, 3, 4);

int onkip;
int offkip;
int onkul;
int offkul ;
int onlt;
int offlt;
int onls;
int offls;


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 102 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80);; //server port

String readString;
Time t;


void setup(void){

  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
   
  rtc.halt(false);
  rtc.writeProtect(false);

  // The following lines can be commented out to use the values already stored in the DS1302
  //rtc.setDOW(WEDNESDAY);        // Set Day-of-Week to FRIDAY
  //rtc.setTime(10, 20, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(12, 4, 2017);  

  rtc.writeProtect(true);
 
  Wire.begin();

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("servertest1"); // so I can keep track of what is loaded
}



void loop(void){
 
int dataadc = analogRead(A7);

t = rtc.getTime();

  // 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;

        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString);

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println("Refresh: 1");        
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>INPUT JADWAL</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H3>kontrol ON / OFF dan delay lampu");
          client.println("<BR>");
          client.print(rtc.getTimeStr());
          client.print(" ");
          client.println(rtc.getDOWStr(FORMAT_LONG));
          client.print(" ");
          client.print(rtc.getDateStr(FORMAT_LONG,FORMAT_LITTLEENDIAN, '/'));

          client.println("<BR>");
           client.println("ADC = ");
           client.println(dataadc);
          client.println("<BR></H3>");
         
          client.println("<FORM ACTION=\"http://192.168.0.102:80\" method=get >");

          client.println("JAM ON KIPAS  : <INPUT TYPE=TEXT NAME=\"onkip\" VALUE=\"A\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
          client.println("JAM OFF KIPAS: <INPUT TYPE=TEXT NAME=\"offkip\" VALUE=\"B\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
         
          client.println("JAM ON KULKAS : <INPUT TYPE=TEXT NAME=\"onkul\" VALUE=\"C\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>"); 
          client.println("JAM OFF KULKAS : <INPUT TYPE=TEXT NAME=\"offkul\" VALUE=\"D\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
         
          client.println("JAM ON Lampu Tengah : <INPUT TYPE=TEXT NAME=\"onlt\" VALUE=\"E\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
          client.println("JAM OFF Lampu Tengah : <INPUT TYPE=TEXT NAME=\"offlt\" VALUE=\"F\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");

          client.println("JAM ON Lampu Teras : <INPUT TYPE=TEXT NAME=\"onls\" VALUE=\"G\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");
          client.println("JAM OFF Lampu Teras : <INPUT TYPE=TEXT NAME=\"offls\" VALUE=\"H\" SIZE=\"25\" MAXLENGTH=\"50\"><BR><BR>");

          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\" SUBMIT\">");
        
          client.println("</FORM>");
          client.println("<BR>");
                  
          client.println("<a href = \"/?lighton1\"\"><input type = 'button' value = 'ON KIPAS' ></a>");
          client.println("<a href = \"/?lightoff1\"\"><input type = 'button' value = 'OFF KIPAS' ></a> ");
          client.println("<a href = \"/?lighton2\"\"><input type = 'button' value = 'ON KULKAS' ></a>");
          client.println("<a href = \"/?lightoff2\"\"><input type = 'button' value = 'OFF KULKAS' ></a> ");
          client.println("<a href = \"/?lighton3\"\"><input type = 'button' value = 'ON LAMPU TENGAH' ></a>");
          client.println("<a href = \"/?lightoff3\"\"><input type = 'button' value = 'OFF LAMPU TENGAH' ></a> ");
          client.println("<a href = \"/?lighton4\"\"><input type = 'button' value = 'ON LAMPU TERAS' ></a>");
          client.println("<a href = \"/?lightoff4\"\"><input type = 'button' value = 'OFF LAMPU TERAS' ></a> ");
          client.println("<BR>");
          client.println("<BR>");       
          client.println("<h5>");   
          client.println("SET ON KIPAS = ");
          client.println(onkip);
            client.println("<BR>");
          client.println("SET OFF KIPAS = ");
          client.println(offkip);
            client.println("<BR>");
          client.println("SET ON KULKAS = ");
          client.println(onkul);
            client.println("<BR>");
          client.println("SET OFF KULKAS = ");
          client.println(offkul);
            client.println("<BR>");
          client.println("SET ON LAMPU TENGAH = ");
          client.println(onlt);
            client.println("<BR>");
          client.println("SET OFF LAMPU TENGAH =");
          client.println(offlt);
            client.println("<BR>");
          client.println("SET ON LAMPU TERAS = ");
          client.println(onls);
            client.println("<BR>");
          client.println("SET OFF LAMPU TERAS =");
          client.println(offls);
            client.println("<BR>");
           
          client.println("</h5>");
          client.println("<BR>");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client

          client.stop();
        
        
          //==========================================================
         
          if(readString.indexOf("?lighton1") >0)
          { 
            digitalWrite(A0, HIGH);    
          }
         
          if(readString.indexOf("?lightoff1") >0)
          { 
            digitalWrite(A0, LOW);    
          }
         
          if(readString.indexOf("?lighton2") >0)
          { 
            digitalWrite(A1, HIGH);    
          }
         
          if(readString.indexOf("?lightoff2") >0)
          { 
            digitalWrite(A1, LOW);    
          }
         
          if(readString.indexOf("?lighton3") >0)
          { 
            digitalWrite(A2, HIGH);    
          }
         
          if(readString.indexOf("?lightoff3") >0)
          { 
            digitalWrite(A2, LOW);    
          }
         
          if(readString.indexOf("?lighton4") >0)
          { 
            digitalWrite(A3, HIGH);    
          }
         
          if(readString.indexOf("?lightoff4") >0)
          { 
            digitalWrite(A3, LOW);    
          }
        
          //==========================================================
          if(readString.indexOf("A7") >0)//checks for on
          {
             onkip = 7;          
          }
         
             if(readString.indexOf("A8") >0)//checks for on
          {
             onkip = 7;          
          }
         
             if(readString.indexOf("A9") >0)//checks for on
          {
             onkip = 7;          
          }
         
          //============================================================
                 
          if(readString.indexOf("B20") >0)//checks for on
          {
             offkip = 20;
          }
         
            if(readString.indexOf("B21") >0)//checks for on
          {
             offkip = 21;
          }
         
            if(readString.indexOf("B22") >0)//checks for on
          {
             offkip = 22;
          }
        
          //====================================================
         
          if(readString.indexOf("C7") >0)//checks for on
          {
             onkul = 7;
           
          }
         
           if(readString.indexOf("C8") >0)//checks for on
          {
             onkul = 8;
           
          }
         
           if(readString.indexOf("C9") >0)//checks for on
          {
             onkul = 9;
           
          }
         
          //=========================================================
                
          if(readString.indexOf("D20") >0)//checks for off
          {
             offkul = 20;
          }
         
          if(readString.indexOf("D21") >0)//checks for off
          {
             offkul = 21;
          }
         
          if(readString.indexOf("D22") >0)//checks for off
          {
             offkul = 22;
          }
        
          //=========================================================
        
          if(readString.indexOf("E7") >0)//checks for off
          {
            onlt = 7;
          }
         
           if(readString.indexOf("E8") >0)//checks for off
          {
            onlt = 8;
          }
         
           if(readString.indexOf("E9") >0)//checks for off
          {
            onlt = 9;
          }
         
          //==========================================================
         
          if(readString.indexOf("F20") >0)//checks for off
          {
            offlt = 20;
          }
         
          if(readString.indexOf("F21") >0)//checks for off
          {
            offlt = 21;
          }
         
          if(readString.indexOf("F22") >0)//checks for off
          {
            offlt = 22;
          }
         
          //=========================================================
         
          if(readString.indexOf("G7") >0)//checks for off
          {
            onls = 7;
          }
         
           if(readString.indexOf("G8") >0)//checks for off
          {
            onls = 8;
          }
         
           if(readString.indexOf("G9") >0)//checks for off
          {
            onls = 9;
          }
         
          //=========================================================
         
          if(readString.indexOf("H20") >0)//checks for off
          {
            offls = 20;
          }
         
           if(readString.indexOf("H21") >0)//checks for off
          {
            offls = 21;
          }
         
           if(readString.indexOf("H22") >0)//checks for off
          {
            offls = 22;
          }
         
          //==============================================================================
        //   Serial.print(rtc.getDOWStr());
        //   Serial.print(" ");
        //   Serial.print(rtc.getDOWStr(FORMAT_LONG));
        //   Serial.print(" -- ");
        //   Serial.println(rtc.getTimeStr());
 
          if((t.hour == onkip)&&(t.hour > 0)){
          digitalWrite(A0,HIGH);
          }
         
          if((t.hour == offkip)&&(t.hour > 0)){
          digitalWrite(A0,LOW);
          }
         
          if((t.hour == onkul)&&(t.hour > 0)){
          digitalWrite(A1,HIGH);
          }
         
          if((t.hour == offkul)&&(t.hour > 0)){
          digitalWrite(A1,LOW);
          }
         
          if((t.hour == onlt)&&(t.hour > 0)){
          digitalWrite(A2,HIGH);
          }
         
          if((t.hour == offlt)&&(t.hour > 0)){
          digitalWrite(A2,LOW);
          }

          if((t.hour == onls)&&(t.hour > 0)){
          digitalWrite(A3,HIGH);
          }
         
          if((t.hour == offls)&&(t.hour > 0)){
          digitalWrite(A3,LOW);
          }
        
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}





g. Gambar WEB







h. VIDEO HASILNYA













No comments:

Post a Comment