#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <Dhcp.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <Dns.h>

//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
#include <SPI.h>
#include <Ethernet.h>

#include <Servo.h>
//Servo myservo;  // create servo object to control a serv
#define led 13
byte mac[] = { 0x90, 0xA1, 0xDA, 0x00, 0x51, 0x09 };
//byte ip[] = { 192, 168, 0, 102 }; // ip in lan
EthernetServer server(80); //server port

String readString;

//////////////////////

void setup(){

  pinMode(led, OUTPUT); //pin selected to control
  //start Ethernet
    Ethernet.hostName("TUNNEL");

  Ethernet.begin(mac);
  server.begin();
  Ethernet.hostName("TUNNEL");

 Serial.begin(19200);
  Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
  printIPAddress();
    sensors.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  Serial.println(Ethernet.getHostName());
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging



          ///////////////////// control arduino pin
          if(readString.indexOf("temp") >0)//checks for on
          {
          sensors.requestTemperatures(); // Send the command to get temperatures
          Serial.println("gotrequest");
          client.print("temp is ");
          client.print(sensors.getTempCByIndex(0));
          client.println("<br />");
         }
          if(readString.indexOf("off") >0)//checks for off
          {
            //myservo.write(140);
            digitalWrite(led, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
void printIPAddress()
{
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }

  Serial.println();
}
