/*
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)
by David A. Mellis

Modyfied by Nicolaj Lindhard to flip an outpud when loadet
*/
#include <SPI.h>
#include <Ethernet.h>

// 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 };
byte ip[] = { 192,168,0, 177 };

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);

boolean ledPinStatus = LOW;
int visitors = 0; //add a visitor caunter
char Message[100];
char *RecievedWord = NULL;
int i = 0;
int wordnr = 0;

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(9,OUTPUT);
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //The string functions begin!!
        Message[i++] = c;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //analyse the return string
          i = 0;
          wordnr = 0;
          char *result = NULL;
          result = strtok( Message, " ");
          while( result != NULL ) {
            if(wordnr == 1)
             {RecievedWord = result;}
              result = strtok( NULL, " " );
              wordnr++;
          }      
           client.println("<br>");
          //Enough messing around. Here we make diferent cases dependin on what has been sent   
          
          if( strcmp(RecievedWord , "/ON") == 0){                    
             client.print("LED ON <br /><br />");
             digitalWrite(9,HIGH);
             client.print("<a href='/OFF' ><img src='http://www.skramloteket.dk/elektronik/webserver/switchON.PNG' border=0></a><br/>");
          }
          if( strcmp(RecievedWord , "/OFF") == 0){                    
             client.print("LED OFF <br /><br />");
             digitalWrite(9,LOW);
             client.print("<a href='/ON' ><img src='http://www.skramloteket.dk/elektronik/webserver/switchOFF.PNG' border=0></a><br/>");
          }
          if( strcmp(RecievedWord , "/HELP") == 0){                    
             client.print("Ha! No help here <br> <br />");
          }
          
          visitors++; //increment and display the visitors
          client.print("visitors: ");
          client.print(visitors);
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
         }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);  // give the web browser time to receive the data
    client.stop();  // close the connection:
  }
}