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