//Ethernet Switch // //Intro: //This will swich on and off outputs trough your mobile device. //No images or links to images. CSS3 and HTML5 use. //Though it work with other web browser, we suggest Safari for best experiance. // //Version: Web Server Ethernet Switching Version 3.05 //Author: Claudio Vella - Malta //Initial code from: http://bildr.org/2011/06/arduino-ethernet-pin-control/ //Made lot of comments for beginners. //ARDUINO 1.0+ ONLY # Include # Include //////////////////////////////////////////////////////////////////////// //CONFIGURE //////////////////////////////////////////////////////////////////////// //IP manual settings byte ip[] = { 192, 168, 0, 177 }; //Manual setup only byte gateway[] = { 192, 168, 0, 254 }; //Manual setup only byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only // if need to change the MAC address (Very Rare) 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); //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); } digitalWrite(9, HIGH);//allume la sortie 9 au depart //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(){ // 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("Telecommande Ethernet"); 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(""); // //-------- debut de la page HTML ------- client.println("
");//
client.print("[[Fichier:),client.print(]]") ; // " > client.println(" ");// //-------- fin de la page HTML ------- // //-------- debut de la page HTML ------- // client.println(" ");// // client.print("[[Fichier:),client.print(]]") ; // " > //client.println(" ");// //-------- fin de la page HTML ------- //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(" Telecommandes Ethernet"); client.println("
"); client.println("
"); client.println(" Allumez ou Eteignez la sortie ."); 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© Author - Claudio Vella
Malta - October - 2012"); 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]); }
}