Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Luis Álvarez
  2. // Incluimos las librerías:
  3. #include <LiquidCrystal.h>
  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6. #define max_he 255
  7.  
  8. // Inicializamos la librería con los pines que utilizaremos
  9. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
  10.  
  11. byte mac[] = {
  12.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  13. IPAddress ip(192,168,1,14); //direccion ip configurada para funcionar con el servidor local.
  14.  
  15. // Inicializar la librería de servidor Ethernet
  16. // con la dirección IP y el puerto que desee utilizar
  17. // (puerto 80 es el predeterminado para HTTP):
  18. EthernetServer server(80);
  19. String Http = String(max_he);
  20.  
  21. void setup() {
  22.   // Inicializamos el LCD
  23.   lcd.begin(16, 2);
  24.   Http = "";
  25.  
  26.   // Iniciamos la conexión con el servidor
  27.   Ethernet.begin(mac, ip);
  28.   server.begin();
  29.   Serial.begin(9600);
  30.   Serial.print("server is at ");
  31.   Serial.println(Ethernet.localIP());
  32. }
  33.  
  34. void loop() {
  35.   // Apagamos el LCD
  36.   lcd.noDisplay();
  37.   delay(500);
  38.   // Encendemos el LCD
  39.   lcd.display();
  40.   delay(500);
  41.  
  42.   EthernetClient client = server.available();
  43.   if (client) {
  44.     Serial.println("new client");
  45.     boolean currentLineIsBlank = true;
  46.     while (client.connected()) {
  47.       if (client.available()) {
  48.         char c = client.read();
  49.         if (Http.length() < max_he){
  50.           Http += c;
  51.         }
  52.         if (c == '\n' && currentLineIsBlank) {
  53.           client.println("HTTP/1.1 200 OK");
  54.           client.println("Content-Type: text/html");
  55.           client.println("Connection: close");  // la conexión se cierra después de la finalización de la respuesta
  56.           client.println("Refresh: 30");  // Refescamos cada 30 segundos
  57.           client.println();
  58.           client.println("<!DOCTYPE HTML>");
  59.           client.println("<html>");
  60.  
  61.           client.println("<form method=\"GET\">");
  62.           client.println("<input type=\"text\" name=\"msg\" size=\"10\" />");
  63.           client.println("<input type=\"submit\">");
  64.           client.println("</form>");
  65.           client.println("</html>");
  66.           break;
  67.         }
  68.         if (c == '\n') {
  69.           currentLineIsBlank = true;
  70.         }
  71.         else if (c != '\r') {
  72.          
  73.           currentLineIsBlank = false;
  74.         }
  75.       }
  76.     }
  77.     // Le damos tiempo al navegador para recibir los datos
  78.     delay(1);
  79.  
  80.     int firstPos = Http.indexOf("?");
  81.     if(firstPos > -1){
  82.       int lastPos = Http.indexOf(" ", firstPos+5);
  83.       String text = Http.substring(firstPos+5, lastPos);
  84.       lcd.print(text);
  85.       Http = "";
  86.     }
  87.    
  88.     // Cerramos la conexión:
  89.     client.stop();
  90.     Serial.println("client disonnected");
  91.   }
  92.  
  93. }