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