#include #include #include "EmonLib.h" // Include Emon Library EnergyMonitor emon1; double Irms; byte ip[] = { 192, 168, 1, 177 }; //Manual setup only byte gateway[] = { 192, 168, 1, 1 }; //Manual setup only byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Ethernet Port EthernetServer server = EthernetServer(80); //default html port 80 //The number of outputs going to be switched. int outputQuantity = 8; //when added to outputLowest result should not exceed 10 //The lowest output pin we are starting from int outputLowest = 2; //Should be between 2 to 9 //////////////////////////////////////////////////////////////////////// // Variable declaration int outp = 0; boolean printLastCommandOnce = false; boolean printButtonMenuOnce = false; boolean initialPrint = true; String allOn = ""; String allOff = ""; boolean reading = false; boolean readInput[10]; //Create a boolean array for the maximum ammount. //Beginning of the program void setup(){ Serial.begin(9600); emon1.current(1, 60); //Pins 10,11,12 & 13 are used by the ethernet shield //Set pins as Outputs for (int var = outputLowest; var < outputLowest + outputQuantity; var++) { pinMode(var, OUTPUT); } //Setting up the IP address. Comment out the one you dont need. //Ethernet.begin(mac); //for DHCP address. (Address will be printed to serial.) Ethernet.begin(mac, ip, gateway, subnet); //for manual setup. (Address is the one configured above.) server.begin(); Serial.println(Ethernet.localIP()); } void loop(){ Irms = emon1.calcIrms(1480); // Calculate Irms only Serial.print(Irms*220.0); // A Serial.print(" "); Serial.println(Irms); // Irms // tambahan seminar hasil if (Irms*220 > 300) { // Cek Jika daya listrik mencapai 220 watt maka digitalWrite(4, LOW); // Matikan Alat Listrik } // listen for incoming clients, and process requests. checkForClient(); } void checkForClient(){ EthernetClient client = server.available(); if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; boolean sentHeader = false; while (client.connected()) { if (client.available()) { if(!sentHeader){ // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connnection: close"); client.println(); client.println(""); client.println(""); // add page title client.println("Sistem Monitoring & Kendali Alat Listrik Berbasis TCP/IP"); client.println(""); // add a meta refresh tag, so the browser pulls again every 5 seconds: client.println(""); // add other browser configuration client.println(""); client.println(""); client.println(""); //inserting the styles data, usually found in CSS files. client.println(""); client.println(""); //now printing the page itself client.println(""); client.println("
"); client.println("
"); client.println("

Monitoring & Kendali Alat Listrik

"); client.println("
"); client.println("
"); client.print("

Arus Listrik: "); client.print (Irms); client.print (" Ampere."); client.println("

"); client.print("

Daya Listrik : "); client.print (Irms * 220); client.print (" Watt." ); client.println("

"); client.println(); //This is for the arduino to construct the page on the fly. sentHeader = true; } char c = client.read(); if(reading && c == ' '){ reading = false; } // Serial.print(c); if(c == '?') { reading = true; //found the ?, begin reading the info } if(reading){ if(c == 'H') {outp = 1;} if(c == 'L') {outp = 0;} Serial.print(c); //print the value of c to serial communication //Serial.print(outp); //Serial.print('\n'); switch (c) { case '2': //add code here to trigger on 2 triggerPin(2, client, outp); break; case '3': //add code here to trigger on 3 triggerPin(3, client, outp); break; case '4': //add code here to trigger on 4 triggerPin(4, client, outp); break; case '5': //add code here to trigger on 5 triggerPin(5, client, outp); //printHtml(client); break; case '6': //add code here to trigger on 6 triggerPin(6, client, outp); break; case '7': //add code here to trigger on 7 triggerPin(7, client, outp); break; case '8': //add code here to trigger on 8 triggerPin(8, client, outp); break; case '9': //add code here to trigger on 9 triggerPin(9, client, outp); break; } } if (c == '\n' && currentLineIsBlank){ printLastCommandOnce = true; printButtonMenuOnce = true; triggerPin(777, client, outp); //Call to read input and print menu. 777 is used not to update any outputs break; } } } //Set Variables Before Exiting printLastCommandOnce = false; printButtonMenuOnce = false; allOn = ""; allOff = ""; client.println("
\n
\n\n"); delay(1); // give the web browser time to receive the data client.stop(); // close the connection: } } void triggerPin(int pin, EthernetClient client, int outp){ //Switching on or off outputs, reads the outputs and prints the buttons //Setting Outputs if (pin != 777){ if(outp == 1) { digitalWrite(pin, HIGH); } if(outp == 0){ digitalWrite(pin, LOW); } } //Refresh the reading of outputs readOutputStatuses(); //Prints the buttons if (printButtonMenuOnce == true){ printHtmlButtons(client); printButtonMenuOnce = false; } } //print the html buttons to switch on/off channels void printHtmlButtons(EthernetClient client){ //Start to create the html table client.println(""); //client.println("

"); client.println("

"); client.println(""); //Start printing button by button for (int var = outputLowest; var < outputLowest + outputQuantity; var++) { //set command for all on/off allOn += "H"; allOn += var; allOff += "L"; allOff += var; //Print begining of row client.print("\n"); //Prints the ON Buttons client.print(" \n"); //Prints the OFF Buttons client.print(" \n"); //Print first part of the Circles or the LEDs if (readInput[var] == true){ client.print(" \n"); }else { client.print(" \n"); } //Print end of row client.print("\n"); } //Prints the ON All Pins Button client.print("\n\n"); //Prints the OFF All Pins Button client.print("\n\n\n"); //Closing the table and form client.println("
"); client.println("
"); //client.println("

"); } //Reading the Output Statuses void readOutputStatuses(){ for (int var = outputLowest; var < outputLowest + outputQuantity; var++) { readInput[var] = digitalRead(var); //Serial.print(readInput[var]); } }