#include <SPI.h>
#include <WiFi.h>

char ssid[] = "Your network SSID";      //  your network SSID (name) 
char pass[] = "Your network Password";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);
#define LED 8

void setup(void){
  
	pinMode(LED, OUTPUT);

	Serial.begin(9600);

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    while(true);        // don't continue
  } 
  
  String fv = WiFi.firmwareVersion();
  if( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

	while ( status != WL_CONNECTED) { 
		Serial.print("Attempting to connect to Network named: ");
		Serial.println(ssid);                   // print the network name (SSID);

		// Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
		status = WiFi.begin(ssid, pass);
		// wait 10 seconds for connection:
		delay(10000);
	  } 
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status


}


void loop(void){
  
  
         WiFiClient client = server.available();
         if (client) {                             // if you get a client,
            Serial.println("new client");           // print a message out the serial port
                  
            while (client.connected()) {            // loop while the client's connected
            if (client.available()) {             // if there's bytes to read from the client,
                    char c = client.read();             // read a byte, then
                    Serial.write(c);                    // print it out the serial monitor
                    if(c == 't'){
                       digitalWrite(LED, HIGH);
                     }
                     else{
                       digitalWrite(LED, LOW);
                     }
                  }
             }         
        client.stop();
        Serial.println("client disonnected");
      
     }
    
     
	delay(200);

}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
