martes, 9 de junio de 2015

/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
byte gateway[]= {192,168,1,1};
byte subnet[]= {255,255,255,0};



// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
float a;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip,gateway,subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Nuevo cliente");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        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");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
       
          // Aquí empieza la página
          //client.println("<img src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_djoUUyFaxjIUPt12eqN6dUUWOXgnOfeJV8hFrCMMaM0n3IAKuJkIMTvqnOHm65m-9h_yRJzuPkkB1ZjCGMDOGYZFMUgYqPLKudZU_30M_kgVLPkKMp4xcufIZ3l3ltP75PdI8CdpeUo/s1600/ArduinoUno_R3_thumb.jpg'  width='100px' height='100px'></a>"); //Muestra la imagen con la url de la imagen
           client.print("<h2>");
            client.print("Arduino con Ethernet Shield");
            client.print("</h2>");
          
          client.println("<img src='http://www.ntpro.nl/blog/uploads/Arduino.png'  width='400px' height='400px'></a>"); //Muestra la imagen con la url de la imagen
          
          client.println("<br />"); // salta de línea
          
          // Devuelve el valor de cada pin de entrada analógica
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            double sensorReading = analogRead(analogChannel);  // uso double para poder tener los decimales
            client.print("<HEAD><TITLE>Prueba Webserver lectura entradas analogicas</TITLE></HEAD>");
            client.print("<body bgcolor='#FFFFFF'>"); //pongo el color del fondo
            
            client.print("<i>");
            client.print("Entrada analogica ");
            client.print("</i>");
            client.print("<b>"); //inicio negrita
            client.print(analogChannel);
            client.print("</b>"); //fin negrita
            client.print(" = ");
            client.print(sensorReading);
            client.print("   son   ");
            double b=sensorReading*5/1023;
            client.print("<b>"); //inicio negrita
            client.print(b);
            client.print("Voltios ");
            client.print("</b>"); //fin negrita
             client.println("<br />");
             client.print("<b>--------------------------------------------------------------</b>");
            client.println("<br />");
            
            client.print("</body>");
          }
            client.println("<br />");
            client.println("<br />");
         
            client.print("<h2>");
            client.print("Llamada a Blog desde Arduino Ethernet Shield");
            client.print("</h2>");
            
          client.println("<iframe src='http://www.rjuarez7.blogspot.com.es/' frameborder='5' scrolling='yes' width='350px' height='350px'></iframe>"); // Hago un link a mi blogspot debajo de los valores de las entradas
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(2);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}