/* Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Analog inputs attached to pins A0 through A5 (optional) created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe modified 02 Sept 2015 by Arturo Guadalupi */ #include #include #include #define BUFSIZ 100 // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); int isOn = 0; const int relay = 7; int pcIsOn = 0; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); pinMode(relay,OUTPUT); digitalWrite(relay, LOW); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port 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() { char clientline[BUFSIZ]; int index = 0; int sensorValue = analogRead(A1); //get voltage from molex to see if pc is running float voltage = sensorValue * (5.0 / 1023.0); // 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 it isn't a new line, add the character to the buffer if (c != '\n' && c != '\r') { clientline[index] = c; index++; // are we too big for the buffer? start tossing out data if (index >= BUFSIZ) index = BUFSIZ -1; // continue to read more data! continue; } // got a \n or \r new line, which means the string is done clientline[index] = 0; // Print it out for debugging Serial.println(clientline); // Look for substring such as a request to get the root file if (strncmp(clientline, "GET /1", 6) == 0) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("Starting PC."); client.println("\r\n"); //Set relay on isOn = 1; } else if (strncmp(clientline, "GET /2", 6) == 0) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("Shutting Down PC."); client.println("\r\n"); //Set relay off isOn = 2; } else if (strncmp(clientline, "GET /3", 6) == 0) { if (voltage > 2) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("Voltage"); client.println(voltage); } else { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(); client.println("PC Not Running."); client.println("\r\n"); } //Set relay off isOn = 3; } else { // everything else is a 404 client.println("HTTP/1.1 404 Not Found"); client.println("Content-Type: text/html"); client.println(); client.println("

File Not Found!

"); } break; } } // give the web browser time to receive the data delay(1); client.stop(); Serial.println("client disconnected"); } if (isOn == 1) { Serial.println(voltage); if (voltage < 1 ) { pcIsOn = 0; digitalWrite(relay, HIGH); // set the relay on delay (300); digitalWrite(relay, LOW); Serial.println("PC starting"); } else { pcIsOn = 1; Serial.println("PC already running"); } isOn = 0; } else if (isOn == 2) { Serial.println(voltage); if (voltage > 2 ) { pcIsOn = 1; digitalWrite(relay, HIGH); // set the relay on delay (300); digitalWrite(relay, LOW); Serial.println("PC shutting down"); } else { pcIsOn = 0; Serial.println("PC is not running"); } isOn = 0; } else if (isOn == 3) { isOn = 0; digitalWrite(relay, LOW); } else { isOn = 0; digitalWrite(relay, LOW); } }