bangnaga

MONITORING KENDALI DAYA AJAX WEB SERVER

Apr 7th, 2014 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.30 KB | None | 0 0
  1. #include <Ethernet.h>
  2. #include <SPI.h>
  3. #include "EmonLib.h"                   // Include Emon Library
  4. EnergyMonitor emon1;    
  5. double Irms;
  6.   byte ip[] = { 192, 168, 1, 177 };   //Manual setup only
  7.   byte gateway[] = { 192, 168, 1, 1 }; //Manual setup only
  8.   byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
  9.  
  10.  
  11.   byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  12.  
  13.   //Ethernet Port
  14.   EthernetServer server = EthernetServer(80); //default html port 80
  15.  
  16.   //The number of outputs going to be switched.
  17.   int outputQuantity = 8;  //when added to outputLowest result should not exceed 10
  18.  
  19.   //The lowest output pin we are starting from
  20.   int outputLowest = 2;    //Should be between 2 to 9
  21. ////////////////////////////////////////////////////////////////////////
  22.  
  23.   // Variable declaration
  24.   int outp = 0;
  25.   boolean printLastCommandOnce = false;
  26.   boolean printButtonMenuOnce = false;
  27.   boolean initialPrint = true;
  28.   String allOn = "";
  29.   String allOff = "";
  30.   boolean reading = false;
  31.   boolean readInput[10]; //Create a boolean array for the maximum ammount.
  32.  
  33. //Beginning of the program
  34. void setup(){
  35.   Serial.begin(9600);
  36.   emon1.current(1, 60);
  37.   //Pins 10,11,12 & 13 are used by the ethernet shield
  38.   //Set pins as Outputs
  39.   for (int var = outputLowest; var < outputLowest + outputQuantity; var++)  {
  40.             pinMode(var, OUTPUT);
  41.         }
  42.  
  43.   //Setting up the IP address. Comment out the one you dont need.
  44.   //Ethernet.begin(mac); //for DHCP address. (Address will be printed to serial.)
  45.   Ethernet.begin(mac, ip, gateway, subnet); //for manual setup. (Address is the one configured above.)
  46.  
  47.  
  48.   server.begin();
  49.   Serial.println(Ethernet.localIP());
  50. }
  51.  
  52.  
  53. void loop(){
  54.  Irms = emon1.calcIrms(1480);  // Calculate Irms only
  55.    Serial.print(Irms*220.0);           // A
  56.   Serial.print(" ");
  57.   Serial.println(Irms);            // Irms
  58. // tambahan seminar hasil
  59.     if (Irms*220 > 300) { // Cek Jika daya listrik mencapai 220 watt maka
  60.       digitalWrite(4, LOW); // Matikan Alat Listrik
  61.     }
  62.      
  63.  
  64.   // listen for incoming clients, and process requests.
  65.   checkForClient();
  66. }
  67.  
  68.  
  69. void checkForClient(){
  70.  
  71.   EthernetClient client = server.available();
  72.  
  73.   if (client) {
  74.  
  75.     // an http request ends with a blank line
  76.     boolean currentLineIsBlank = true;
  77.     boolean sentHeader = false;
  78.  
  79.     while (client.connected()) {
  80.       if (client.available()) {
  81.  
  82.         if(!sentHeader){
  83.          // send a standard http response header
  84.           client.println("HTTP/1.1 200 OK");
  85.           client.println("Content-Type: text/html");
  86.           client.println("Connnection: close");
  87.           client.println();
  88.           client.println("<!DOCTYPE HTML>");
  89.           client.println("<head>");
  90.          
  91.           // add page title
  92.           client.println("<title>Sistem Monitoring & Kendali Alat Listrik Berbasis TCP/IP</title>");
  93.           client.println("<meta name=\"description\" content=\"Sistem Monitoring & Kendali Alat Listrik Berbasis TCP/IP\"/>");
  94.  
  95.           // add a meta refresh tag, so the browser pulls again every 5 seconds:
  96.           client.println("<meta http-equiv=\"refresh\" content=\"5; url=/\">");
  97.          
  98.           // add other browser configuration
  99.           client.println("<meta name=\"apple-mobile-web-app-capable\" content=\"yes\">");
  100.           client.println("<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"default\">");
  101.           client.println("<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\"/>");        
  102.          
  103.           //inserting the styles data, usually found in CSS files.
  104.           client.println("<style type=\"text/css\">");
  105.           client.println("");
  106.          
  107.           //This will set how the page will look graphically
  108.           client.println("html { height:100%; }");
  109.  
  110.           client.println("  body {");
  111.           client.println("    height: 100%;");
  112.           client.println("    margin: 0;");
  113.           client.println("    font-family: helvetica, sans-serif;");
  114.           client.println("    -webkit-text-size-adjust: none;");
  115.           client.println("   }");
  116.           client.println("");
  117.           client.println("body {");
  118.           client.println("    -webkit-background-size: 100% 21px;");
  119.           client.println("    background-color: #c5ccd3;");
  120.           client.println("    background-image:");
  121.           client.println("    -webkit-gradient(linear, left top, right top,");
  122.           client.println("    color-stop(.75, transparent),");
  123.           client.println("    color-stop(.75, rgba(255,255,255,.1)) );");
  124.           client.println("    -webkit-background-size: 7px;");
  125.           client.println("   }");
  126.           client.println("");
  127.           client.println(".view {");
  128.           client.println("    min-height: 100%;");
  129.           client.println("    overflow: auto;");
  130.           client.println("   }");
  131.           client.println("");
  132.           client.println(".header-wrapper {");
  133.           client.println("    height: 44px;");
  134.           client.println("    font-weight: bold;");
  135.           client.println("    text-shadow: rgba(0,0,0,0.7) 0 -1px 0;");
  136.           client.println("    border-top: solid 1px rgba(255,255,255,0.6);");
  137.           client.println("    border-bottom: solid 1px rgba(0,0,0,0.6);");
  138.           client.println("    color: #fff;");
  139.           client.println("    background-color: #8195af;");
  140.           client.println("    background-image:");
  141.           client.println("    -webkit-gradient(linear, left top, left bottom,");
  142.           client.println("    from(rgba(255,255,255,.4)),");
  143.           client.println("    to(rgba(255,255,255,.05)) ),");
  144.           client.println("    -webkit-gradient(linear, left top, left bottom,");
  145.           client.println("    from(transparent),");
  146.           client.println("    to(rgba(0,0,64,.1)) );");
  147.           client.println("    background-repeat: no-repeat;");
  148.           client.println("    background-position: top left, bottom left;");
  149.           client.println("    -webkit-background-size: 100% 21px, 100% 22px;");
  150.           client.println("    -webkit-box-sizing: border-box;");
  151.           client.println("   }");
  152.           client.println("");
  153.           client.println(".header-wrapper h1 {");
  154.           client.println("    text-align: center;");
  155.           client.println("    font-size: 20px;");
  156.           client.println("    line-height: 44px;");
  157.           client.println("    margin: 0;");
  158.           client.println("   }");
  159.           client.println("");
  160.           client.println(".group-wrapper {");
  161.           client.println("    margin: 9px;");
  162.           client.println("    }");
  163.           client.println("");
  164.           client.println(".group-wrapper h2 {");
  165.           client.println("    color: #4c566c;");
  166.           client.println("    font-size: 17px;");
  167.           client.println("    line-height: 0.8;");
  168.           client.println("    font-weight: bold;");
  169.           client.println("    text-shadow: #fff 0 1px 0;");
  170.           client.println("    margin: 20px 10px 12px;");
  171.           client.println("   }");
  172.           client.println("");
  173.           client.println(".group-wrapper h3 {");
  174.           client.println("    color: #4c566c;");
  175.           client.println("    font-size: 12px;");
  176.           client.println("    line-height: 1;");
  177.           client.println("    font-weight: bold;");
  178.           client.println("    text-shadow: #fff 0 1px 0;");
  179.           client.println("    margin: 20px 10px 12px;");
  180.           client.println("   }");
  181.           client.println("");
  182.           client.println(".group-wrapper table {");
  183.           client.println("    background-color: #fff;");
  184.           client.println("    -webkit-border-radius: 10px;");
  185.          
  186.           client.println("    -moz-border-radius: 10px;");
  187.           client.println("    -khtml-border-radius: 10px;");
  188.           client.println("    border-radius: 10px;");
  189.          
  190.           client.println("    font-size: 17px;");
  191.           client.println("    line-height: 20px;");
  192.           client.println("    margin: 9px 0 20px;");
  193.           client.println("    border: solid 1px #a9abae;");
  194.           client.println("    padding: 11px 3px 12px 3px;");
  195.           client.println("    margin-left:auto;");
  196.           client.println("    margin-right:auto;");
  197.          
  198.           client.println("    -moz-transform :scale(1);"); //Code for Mozilla Firefox
  199.           client.println("    -moz-transform-origin: 0 0;");
  200.  
  201.  
  202.          
  203.           client.println("   }");
  204.           client.println("");
  205.  
  206.  
  207.           //how the green (ON) LED will look
  208.           client.println(".green-circle {");
  209.           client.println("    display: block;");
  210.           client.println("    height: 23px;");
  211.           client.println("    width: 23px;");
  212.           client.println("    background-color: #0f0;");
  213.         //client.println("    background-color: rgba(60, 132, 198, 0.8);");
  214.           client.println("    -moz-border-radius: 11px;");
  215.           client.println("    -webkit-border-radius: 11px;");
  216.           client.println("    -khtml-border-radius: 11px;");
  217.           client.println("    border-radius: 11px;");
  218.           client.println("    margin-left: 1px;");
  219.  
  220.           client.println("    background-image: -webkit-gradient(linear, 0% 0%, 0% 90%, from(rgba(46, 184, 0, 0.8)), to(rgba(148, 255, 112, .9)));@");
  221.           client.println("    border: 2px solid #ccc;");
  222.           client.println("    -webkit-box-shadow: rgba(11, 140, 27, 0.5) 0px 10px 16px;");
  223.           client.println("    -moz-box-shadow: rgba(11, 140, 27, 0.5) 0px 10px 16px; /* FF 3.5+ */");
  224.           client.println("    box-shadow: rgba(11, 140, 27, 0.5) 0px 10px 16px; /* FF 3.5+ */");
  225.          
  226.           client.println("    }");
  227.           client.println("");
  228.          
  229.           //how the black (off)LED will look
  230.           client.println(".black-circle {");
  231.           client.println("    display: block;");
  232.           client.println("    height: 23px;");
  233.           client.println("    width: 23px;");
  234.           client.println("    background-color: #040;");
  235.           client.println("    -moz-border-radius: 11px;");
  236.           client.println("    -webkit-border-radius: 11px;");
  237.           client.println("    -khtml-border-radius: 11px;");
  238.           client.println("    border-radius: 11px;");
  239.           client.println("    margin-left: 1px;");
  240.           client.println("    -webkit-box-shadow: rgba(11, 140, 27, 0.5) 0px 10px 16px;");
  241.           client.println("    -moz-box-shadow: rgba(11, 140, 27, 0.5) 0px 10px 16px; /* FF 3.5+ */");
  242.           client.println("    box-shadow: rgba(11, 140, 27, 0.5) 0px 10px 16px; /* FF 3.5+ */");
  243.           client.println("    }");
  244.           client.println("");
  245.          
  246.           //this will add the glare to both of the LEDs
  247.           client.println("   .glare {");
  248.           client.println("      position: relative;");
  249.           client.println("      top: 1;");
  250.           client.println("      left: 5px;");
  251.           client.println("      -webkit-border-radius: 10px;");
  252.           client.println("      -moz-border-radius: 10px;");
  253.           client.println("      -khtml-border-radius: 10px;");
  254.           client.println("      border-radius: 10px;");
  255.           client.println("      height: 1px;");
  256.           client.println("      width: 13px;");
  257.           client.println("      padding: 5px 0;");
  258.           client.println("      background-color: rgba(200, 200, 200, 0.25);");
  259.           client.println("      background-image: -webkit-gradient(linear, 0% 0%, 0% 95%, from(rgba(255, 255, 255, 0.7)), to(rgba(255, 255, 255, 0)));");
  260.           client.println("    }");
  261.           client.println("");
  262.          
  263.          
  264.           //and finally this is the end of the style data and header
  265.           client.println("</style>");
  266.           client.println("</head>");
  267.          
  268.           //now printing the page itself
  269.           client.println("<body>");
  270.           client.println("<div class=\"view\">");
  271.           client.println("    <div class=\"header-wrapper\">");
  272.           client.println("      <h3 align=\"center\">Monitoring & Kendali Alat Listrik</h3>");
  273.           client.println("    </div>");
  274.           client.println("<div  class=\"group-wrapper\">");
  275.           client.print("    <h3 align=\"center\"> Arus Listrik: ");
  276.           client.print (Irms);
  277.           client.print (" Ampere.");
  278.           client.println(" </h3>");
  279.           client.print("    <h3 align=\"center\"> Daya Listrik : ");
  280.           client.print (Irms * 220);
  281.           client.print (" Watt." );
  282.           client.println(" </h3>");
  283.           client.println();
  284.          
  285.           //This is for the arduino to construct the page on the fly.
  286.           sentHeader = true;
  287.         }
  288.        
  289.         char c = client.read();
  290.  
  291.         if(reading && c == ' '){
  292.           reading = false;
  293.           }
  294.      
  295. //       Serial.print(c);
  296.        
  297.        
  298.         if(c == '?') {
  299.           reading = true; //found the ?, begin reading the info
  300.         }
  301.    
  302.  
  303.         if(reading){
  304.             if(c == 'H') {outp = 1;}
  305.             if(c == 'L') {outp = 0;}
  306.           Serial.print(c);   //print the value of c to serial communication
  307.           //Serial.print(outp);
  308.           //Serial.print('\n');
  309.        
  310.            switch (c) {
  311.             case '2':
  312.               //add code here to trigger on 2
  313.               triggerPin(2, client, outp);
  314.               break;
  315.             case '3':
  316.             //add code here to trigger on 3
  317.               triggerPin(3, client, outp);
  318.               break;
  319.             case '4':
  320.             //add code here to trigger on 4
  321.               triggerPin(4, client, outp);
  322.               break;
  323.             case '5':
  324.             //add code here to trigger on 5
  325.               triggerPin(5, client, outp);
  326.               //printHtml(client);
  327.               break;
  328.             case '6':
  329.             //add code here to trigger on 6
  330.               triggerPin(6, client, outp);
  331.               break;
  332.             case '7':
  333.             //add code here to trigger on 7
  334.               triggerPin(7, client, outp);
  335.               break;
  336.             case '8':
  337.             //add code here to trigger on 8
  338.               triggerPin(8, client, outp);
  339.               break;
  340.             case '9':
  341.             //add code here to trigger on 9
  342.               triggerPin(9, client, outp);
  343.               break;
  344.           }
  345.  
  346.         }
  347.  
  348.         if (c == '\n' && currentLineIsBlank){
  349.           printLastCommandOnce = true;
  350.           printButtonMenuOnce = true;
  351.           triggerPin(777, client, outp); //Call to read input and print menu. 777 is used not to update any outputs
  352.           break;
  353.         }
  354.       }
  355.     }
  356.    
  357.     //Set Variables Before Exiting
  358.     printLastCommandOnce = false;
  359.     printButtonMenuOnce = false;
  360.    
  361.     allOn = "";
  362.     allOff = "";
  363.  
  364.     client.println("</div>\n</div>\n</body>\n</html>");
  365.    
  366.     delay(1); // give the web browser time to receive the data
  367.     client.stop(); // close the connection:
  368.  
  369.   }
  370.  
  371. }
  372.  
  373.  
  374.  
  375. void triggerPin(int pin, EthernetClient client, int outp){
  376. //Switching on or off outputs, reads the outputs and prints the buttons  
  377.  
  378.   //Setting Outputs
  379.     if (pin != 777){
  380.         if(outp == 1) {
  381.           digitalWrite(pin, HIGH);
  382.          }
  383.         if(outp == 0){
  384.           digitalWrite(pin, LOW);
  385.          }
  386.     }
  387.   //Refresh the reading of outputs
  388.   readOutputStatuses();
  389.  
  390.  
  391.   //Prints the buttons
  392.           if (printButtonMenuOnce == true){
  393.               printHtmlButtons(client);
  394.                printButtonMenuOnce = false;
  395.           }
  396.        
  397. }
  398.  
  399.  
  400. //print the html buttons to switch on/off channels
  401. void printHtmlButtons(EthernetClient client){
  402.    
  403.      //Start to create the html table
  404.      client.println("");
  405.      //client.println("<p>");
  406.      client.println("<FORM>");
  407.      client.println("<table border=\"0\" align=\"center\">");
  408.    
  409.      //Start printing button by button
  410.      for (int var = outputLowest; var < outputLowest + outputQuantity; var++)  {    
  411.              
  412.               //set command for all on/off
  413.               allOn += "H";
  414.               allOn += var;
  415.               allOff += "L";
  416.               allOff += var;
  417.              
  418.              
  419.               //Print begining of row
  420.               client.print("<tr>\n");      
  421.              
  422.               //Prints the ON Buttons
  423.               client.print(" <td><INPUT TYPE=\"button\" VALUE=\"Switch ON - Pin  ");
  424.               client.print(var);
  425.               client.print("\" onClick=\"parent.location='/?H");
  426.               client.print(var);
  427.               client.print("'\"></td>\n");
  428.              
  429.               //Prints the OFF Buttons
  430.               client.print(" <td><INPUT TYPE=\"button\" VALUE=\"Switch OFF - Pin  ");
  431.               client.print(var);
  432.               client.print("\" onClick=\"parent.location='/?L");
  433.               client.print(var);
  434.               client.print("'\"></td>\n");
  435.              
  436.              
  437.               //Print first part of the Circles or the LEDs
  438.               if (readInput[var] == true){
  439.                 client.print(" <td><div class='green-circle'><div class='glare'></div></div></td>\n");
  440.               }else
  441.               {
  442.                 client.print(" <td><div class='black-circle'><div class='glare'></div></div></td>\n");
  443.               }
  444.              
  445.              
  446.               //Print end of row
  447.               client.print("</tr>\n");
  448.          }
  449.        
  450.  
  451.               //Prints the ON All Pins Button
  452.               client.print("<tr>\n<td><INPUT TYPE=\"button\" VALUE=\"Switch ON All Pins");
  453.               client.print("\" onClick=\"parent.location='/?");
  454.               client.print(allOn);
  455.               client.print("'\"></td>\n");
  456.            
  457.               //Prints the OFF All Pins Button          
  458.               client.print("<td><INPUT TYPE=\"button\" VALUE=\"Switch OFF All Pins");
  459.               client.print("\" onClick=\"parent.location='/?");
  460.               client.print(allOff);
  461.               client.print("'\"></td>\n<td></td>\n</tr>\n");
  462.              
  463.               //Closing the table and form
  464.               client.println("</table>");
  465.               client.println("</FORM>");
  466.               //client.println("</p>");
  467.    
  468.     }
  469.  
  470. //Reading the Output Statuses
  471. void readOutputStatuses(){
  472.   for (int var = outputLowest; var < outputLowest + outputQuantity; var++)  {
  473.             readInput[var] = digitalRead(var);
  474.             //Serial.print(readInput[var]);
  475.        }
  476.  
  477. }
Advertisement
Add Comment
Please, Sign In to add comment