#include <SPI.h>
#include <Ethernet.h>

#include <avr/io.h>
#include <avr/wdt.h>

#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {}

byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 
  192,168,2,20 };
byte server[] = { 
  193,205,230,10 }; 

Client client(server, 80);

void setup(){
  Ethernet.begin(mac, ip); 
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  int campioni=20;
  int ii=0;
  float  tempc1=0; // temperature variables
  float  tempc2=0; // temperature variables
  float samples1[campioni]; // variables to make a better precision
  float samples2[campioni]; // variables to make a better precision
  long unsigned int light=0;
  int samples3[campioni];  

  short count=0;

  analogWrite(9, 1);   // set the LED on
  delay(500);              // wait for a second
  analogWrite(9, LOW);    // set the LED off
  delay(500);              // wait for a second
  //delay(1000);

  for(ii = 0; ii <= campioni-1 ; ii++)
  {
    samples1[ii] = (( 5.0 * analogRead(3) * 100.0) / 1024.0);
    tempc1 = tempc1 + samples1[ii];

    samples2[ii] = (( 5.0 * analogRead(4) * 100.0) / 1024.0);
    tempc2 = tempc2 + samples2[ii];

    samples3[ii] = analogRead(5);
    light=light + samples3[ii];
  }

  tempc1 = tempc1/campioni; // better precision
  tempc2 = tempc2/campioni; // better precision
  light = light /campioni;

  Serial.print(tempc1);
  Serial.println(" C");
  Serial.print(tempc2-273.15);
  Serial.println(" K to C");
  Serial.print(light);
  Serial.println(" light"); 

  while (!client.connected()) 
  { 
    Serial.println("*************************");
    if(count!=0)
    { 
      Serial.print("Try: "); 
      Serial.println(count);
    }
    Serial.println("Reconnect...");
    client.stop();
    Serial.println("connection stopped");
    client.connect();
    if (client.connected())    
      Serial.println("CONNECTED!!!");
    else
      Serial.println("...connection lost.");
  
    if (count>3)
    {
      digitalWrite(9, HIGH);   // set the LED on
      delay(1000);              // wait for a second
      digitalWrite(9, LOW);    // set the LED off
      
      Reset_AVR();
    }
    Serial.println("*************************");
    count++;
  }

  if (client.connected()) {
    Serial.println("Sending...");
    sendData("andrea", "esposito", 29, tempc1);   
    sendData("andrea", "esposito", 30, tempc2);
    sendData("andrea", "esposito", 31, (tempc2-273.15));
    sendData("andrea", "esposito", 32, light);
    //webResponse();
  }

  Serial.println("+-----------------------------+");
}

void sendData (String userName, String userPassword, int userDatastreamID, float userValue)
{
  //query string....
  client.print("GET http://simulator.uniparthenope.it/108001007/arduino.aspx?username=");
  client.print(userName);
  client.print("&password=");
  client.print(userPassword);
  client.print("&datastream_id=");
  client.print(userDatastreamID);
  client.print("&value=");
  client.print(userValue);
  client.println(" HTTP/1.1");
  client.println("Host: http://simulator.uniparthenope.it");  
  client.println();
}

void webResponse()
{
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}











