#include <SPI.h> 
#include <Ethernet2.h> // if you have ethernet shield V1: # include <Ethernet.h>
#include <Twitter.h> 

byte mac[] =  
 {  0x90, 0xA2, 0xDA, 0x10, 0xC5, 0xDD  };  // change the mac @ with the @ mac of your shield
Twitter twitter("923975243101818882-LdnxHIwOU0hgacaB0Fi8PzNlswCotkk"); // put your secret key here
char msg[] = "There are someone waiting for you ^^^^^^"; // change the msg if you want

EthernetServer server(80);
EthernetClient client;
void TwitterSend () ;
void ServerWeb();

const int trigPin = A1; // Arduino UNO Pin A1 to trig on Ultrasonic Board
const int echoPin = A2;  // Arduino UNO Pin A2 to echo on Ultrasonic Board
long duration;
int objectDistance = 15;//distance in cm

int pingSonar() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
}


void setup() 
{ 
 delay(1000); 
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); 
 if (Ethernet.begin(mac)) {
  Serial.print("Server at : ");
  Serial.println(Ethernet.localIP());
  Serial.println("------------------------------------------------------\n");
 }
 Serial.println("Waitting for the Sensor  ..."); 
 server.begin(); 
} 

void loop() 
{ 
   ServerWeb();
     if (pingSonar()< objectDistance) {
       TwitterSend(); 
          }
   delay(300);
   
}

void TwitterSend() {
 if (twitter.post(msg)) { 
 int status = twitter.wait(&Serial); 
 if (status == 200) { 
 Serial.println("OK."); 
 } else { 
 Serial.print("failed : code "); 
 //Serial.println(status); 
 } 
 } else { 
 Serial.println("connection failed."); 
 }
}
void ServerWeb () {
   delay(500);
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        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("Connection: close");  
          client.println("Refresh: 3");  
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<body bgcolor = #000 style=font-family:verdana>"); 
          client.println("<center>");
          client.print("<h3 style=color:#0099ff> Sensor distance : ");
          client.print(pingSonar());
          client.println(" cm</h3>");
          if (pingSonar()< objectDistance) {
             client.print("<h3 style=color:red> >>>> Go check your <a href=https://twitter.com/AhmNouira>Twitter</a> you have a <u>Notification</u><<<<</h3>"); // change to your account
             TwitterSend(); 
          }
          client.println("</center>");
          client.println("</body>");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}


