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


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,............... };
byte gateway[] = { 192, ............. };
byte subnet[] = { 255,............. };
EthernetServer server(..);

String readString;
int temp=0;
int statofari;
int dist;
int dist2;
int durata;
int durata2;
int TRIG1 = 34;
int ECHO1 = 35;
int VCC_PIN = 11;
int TRIG2 = 7;
int ECHO2 = 8;
int distance1;
int duration2;
int distance2;
int duration1;
int ostacolo;
int i;int j;

void setup(){
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(22,OUTPUT); //MOTORE 1 AVANTI
pinMode(23,OUTPUT); //MOTORE 1 INDIETRO
pinMode(24,OUTPUT); //MOTORE 2 AVANTI
pinMode(25,OUTPUT); //MOTORE 2 INDIETRO
pinMode(26,INPUT); //MOTORE 3 AVANTI
pinMode(27,INPUT); //MOTORE 3 INDIETRO
pinMode(28,INPUT); //MOTORE 4 AVANTI
pinMode(29,OUTPUT); //MOTORE 4 INDIETRO
pinMode(34,OUTPUT); //TRIG1
pinMode(7,OUTPUT); //TRIG2
pinMode(35,INPUT); //ECHO1
pinMode(8,INPUT); //ECHO2
pinMode(VCC_PIN,OUTPUT);
pinMode(30,OUTPUT); // FARI
pinMode(31,OUTPUT); // FARI
digitalWrite(VCC_PIN,HIGH);

// SENSORE TEMPERATURA A9
// SENSORE LUMINOSITA' A10
i=0;j=0;
}

void loop(){

ostacolo=false;

//TEMPERATURA
int val=analogRead(A0);
temp = (5.0 * val * 100.0)/1024.0;

//LUCE
int luce=analogRead(A1);

EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {


if (client.available()) {
char c = client.read();
readString.concat(c);

if (c == '\n' && currentLineIsBlank) {

if(readString.indexOf("Avanti") > 0) {
i=140;
analogWrite(23,i);
analogWrite(25,i);
analogWrite(27,i);
analogWrite(29,i);
digitalWrite(22,LOW);
digitalWrite(24,LOW);
digitalWrite(26,LOW);
digitalWrite(28,LOW);

while(i>0){
//Distanza FRONT
digitalWrite(TRIG1, HIGH);
delayMicroseconds(1000);
digitalWrite(TRIG1, LOW);
duration1 = pulseIn(ECHO1, HIGH);
distance1 = (duration1/2) / 29.1;
if(distance1 < 20){
digitalWrite(22,LOW);
digitalWrite(23,LOW);
digitalWrite(24,LOW);
digitalWrite(25,LOW);
digitalWrite(26,LOW);
digitalWrite(27,LOW);
digitalWrite(28,LOW);
digitalWrite(29,LOW);
i=0;}
}

}

if(readString.indexOf("Indietro") > 0) {
j=140;
analogWrite(22,j);
analogWrite(24,j);
analogWrite(26,j);
analogWrite(28,j);
digitalWrite(23,LOW);
digitalWrite(25,LOW);
digitalWrite(27,LOW);
digitalWrite(29,LOW);

while(j>0){
//Distanza REAR
digitalWrite(TRIG2, HIGH);
delayMicroseconds(1000);
digitalWrite(TRIG2, LOW);
duration1 = pulseIn(ECHO2, HIGH);
distance2 = (duration2/2) / 29.1;
if(distance2 < 10){
digitalWrite(22,LOW);
digitalWrite(23,LOW);
digitalWrite(24,LOW);
digitalWrite(25,LOW);
digitalWrite(26,LOW);
digitalWrite(27,LOW);
digitalWrite(28,LOW);
digitalWrite(29,LOW);
j=0;}}
}

if(readString.indexOf("Destra") > 0) {
analogWrite(23,130);
analogWrite(25,130);
analogWrite(26,130);
analogWrite(28,130);
digitalWrite(22,LOW);
digitalWrite(24,LOW);
digitalWrite(27,LOW);
digitalWrite(29,LOW);
delay(200);
digitalWrite(22,LOW);
digitalWrite(23,LOW);
digitalWrite(24,LOW);
digitalWrite(25,LOW);
digitalWrite(26,LOW);
digitalWrite(27,LOW);
digitalWrite(28,LOW);
digitalWrite(29,LOW);
}

if(readString.indexOf("Sinistra") > 0) {
analogWrite(22,130);
analogWrite(24,130);
analogWrite(27,130);
analogWrite(29,130);
digitalWrite(23,LOW);
digitalWrite(25,LOW);
digitalWrite(26,LOW);
digitalWrite(28,LOW);
delay(200);
digitalWrite(22,LOW);
digitalWrite(23,LOW);
digitalWrite(24,LOW);
digitalWrite(25,LOW);
digitalWrite(26,LOW);
digitalWrite(27,LOW);
digitalWrite(28,LOW);
digitalWrite(29,LOW);
}

if(readString.indexOf("Stop") > 0) {
digitalWrite(22,LOW);
digitalWrite(23,LOW);
digitalWrite(24,LOW);
digitalWrite(25,LOW);
digitalWrite(26,LOW);
digitalWrite(27,LOW);
digitalWrite(28,LOW);
digitalWrite(29,LOW);
i=0;j=0;
}
if(readString.indexOf("Fari1") > 0) {
digitalWrite(30,HIGH);
digitalWrite(31,HIGH);
statofari=true;
}
if(readString.indexOf("Fari0") > 0) {
digitalWrite(30,LOW);
digitalWrite(31,LOW);
statofari=false;
}

//HTML

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><title>MGE Robot Wi-Fi</title><body bgcolor=black>");
client.print("<style>");
client.print(" html { height: 100%; margin: 0px; padding: 0px; }");
client.print(" /*BODY Rule*/");
client.print("body { height: 100%; margin: 0px; padding: 0px; font-size: 100%; font-family: Verdana, Arial, Helvetica, Geneva, sans-serif; text-align: center; }");
client.print(" /*___RESET RULE FOR BASIC TAG_______________________________________________ ______*/");
client.print(" /* Image */");
client.print(" img { border: 0px; margin: 0px; padding: 0px; }");
client.print(" /* Anchor Link */");
client.print(" a { border: 0px; margin: 0px; padding: 0px; text-decoration: none; outline: 0; color:blue;}");
client.print(" a:visited { text-decoration: none; }");
client.print(" a:hover, a:active { text-decoration: none; }");
client.print(" /*Form input field*/");
client.print("input, select, button, textarea { }");
client.print("/*Table*/");
client.print(" table, td, tr { border: 0px; margin: 0px; padding: 0px; }");
client.print("/*Descriptio List*/");
client.print(" dl, dt, dd { border: 0px; margin: 0px; padding: 0px; }");
client.print("/*Unordered list*/");
client.print(" /*ul, li { border: 0px; margin: 0px; padding: 0px; list-style: none; }*/");
client.print(" /*Ordered list*/");
client.print(" li { margin: 0px; list-style: none; font-size: 0.5625em; border:1px solid black; width:40px; margin-bottom:50px; margin-right:5px; align:left;}");
client.print(".left{float:left;}");
client.print(".right{float:right;}");
client.print(".right-r { clear: right; display: inline; float: right; }");
client.print(" /************************************************** ********************************/");
client.print("/*___BASIC PAGE RULE______________________________________________ ________________*/");
client.print(" /*container of page*/");
client.print(" #container { height: auto !important; height: 100%; width: 100%px; min-height: 100%; margin: auto; padding: 0px; }");
client.print(" #content {width:990px; margin:auto;}");
client.print(" #content #bottonisx {width:150px; height:400px; border:1px solid black; float:left;padding:0px; }");
client.print("#content #telecamera { width:670px; height:600px; border: 1px solid black; float:left;}");
client.print(" #content #bottonidx { width:150px; height:390px; border:1px solid black; float:right; padding-top:10px; padding-left:3px; padding-right:3px;}");
client.print(" #content #bottonidx #up { width:140px; height:50px; border:1px solid black; margin:auto;}");
client.print(" #content #bottonidx #rt { width:65px; height:50px; border:1px solid black;margin-bottom:10px; margin-top:10px;}");
client.print(" #content #bottonidx #lt { width:65px; height:50px; border:1px solid black;margin-bottom:10px;margin-top:10px;}");
client.print("#content #bottonidx #dw { width:140px; height:50px; border:1px solid black; margin:auto; margin-top:70px;}");
client.print(" /************************************************** ********************************/");

client.print("</style>");
client.print("<div id=container>");
client.print("<div id=content>");
client.print("<div id=bottonisx>");
client.print("<ul>");

client.print("<a href=Fari1><li class=left>Luci ON</li></a>");
client.print("<a href=Fari0><li class=left>Luci OFF</li></a>");

client.print("<a href=#><li class=left>");
client.print("<font color=white>T: </font>");
client.print(temp);
client.print("</li></a>");

client.print("<a href=#><li class=left>");
client.print("<font color=white>D: </font>");
client.print(dist);
client.print("</li></a>");

client.print("<a href=#><li class=left>");
client.print("<font color=white>L: </font>");
client.print(luce);
client.print("</li></a>");

if(ostacolo){
client.print("<a href=#><li class=left>");
client.print("<font color=red>OSTACOLO!</font>");
client.print("</li></a>");
}

client.print("</ul>");
client.print("</div> ");

client.print("<div id=telecamera>");
client.print("<iframe width=640 height=480 src=http://utente:utente@192.........../live.htm></iframe>");
client.print("</div> ");

client.print("<div id=bottonidx>");
client.print("<a href=Avanti><div id=up>Avanti</div></a>");
client.print("<a href=Sinistra><div id=lt class=left>Sinistra</div></a>");
client.print("<a href=Destra><div id=rt class=right-r>Destra</div></a>");
client.print(" <a href=Stop><div id=dw class=both>Stop</div></a>");
client.print(" <a href=Indietro><div id=dw class=both>Indietro</div></a>");
client.print("</div>");

client.print("</div>");

client.print("</body>");
client.print(" </html>");
readString="";
client.stop();

}}
}
}
}