miércoles, 21 de mayo de 2014

Laboratorio 10

Enviar desde una página web un mensaje a una pantalla LCD 16x2 conectada al Arduino.

Lista de elementos utilizados:


-Una Protoboard
-Un LCD 16x2
-Un Potenciómetro
-Un Arduino UNO
-Cables
-Un Arduino Ethernet Board


Diagrama del montaje en la protoboard:













Diagrama Esquemático del Circuito 














Fotos del Montaje.






















Vídeo funcionamiento:


Código fuente arduino:


  1. // include the library code:
  2. #include <LiquidCrystal.h>
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5. #define max_he 255
  6. // initialize the library with the numbers of the interface pins
  7. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
  8. // Enter a MAC address and IP address for your controller below.
  9. // The IP address wixll be dependent on your local network:
  10. byte mac[] = {
  11.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  12. IPAddress ip(192,168,1,14); //direccion ip configurada para funcionar con el servidor.
  13. // Initialize the Ethernet server library
  14. // with the IP address and port you want to use
  15. // (port 80 is default for HTTP):
  16. EthernetServer server(80);
  17. String Http = String(max_he);
  18. void setup() {
  19.   // set up the LCD's number of columns and rows:
  20.   lcd.begin(16, 2);
  21.   // Print a message to the LCD.
  22.   lcd.print(">>");
  23.   Http = "";
  24.   // start the Ethernet connection and the server:
  25.   Ethernet.begin(mac, ip);
  26.   server.begin();
  27.   Serial.begin(9600);
  28.   Serial.print("server is at ");
  29.   Serial.println(Ethernet.localIP());
  30. }
  31. void loop() {
  32.   // set the cursor to column 0, line 1
  33.   // (note: line 1 is the second row, since counting begins with 0):
  34.   //lcd.setCursor(0, 1);
  35.   // print the number of seconds since reset:
  36.   //lcd.print(millis()/1000);
  37.   // Turn off the display:
  38.   lcd.noDisplay();
  39.   delay(500);
  40.   // Turn on the display:
  41.   lcd.display();
  42.   delay(500);
  43.   // listen for incoming clients
  44.   EthernetClient client = server.available();
  45.   if (client) {
  46.     Serial.println("new client");
  47.     // an http request ends with a blank line
  48.     boolean currentLineIsBlank = true;
  49.     while (client.connected()) {
  50.       if (client.available()) {
  51.         char c = client.read();
  52.         if (Http.length() < max_he){
  53.           Http += c;
  54.         }
  55.         // if you've gotten to the end of the line (received a newline
  56.         // character) and the line is blank, the http request has ended,
  57.         // so you can send a reply
  58.         if (c == '\n' && currentLineIsBlank) {
  59.           // send a standard http response header
  60.           client.println("HTTP/1.1 200 OK");
  61.           client.println("Content-Type: text/html");
  62.           client.println("Connection: close");  // the connection will be closed after completion of the response
  63.           client.println("Refresh: 30");  // refresh the page automatically every 5 sec
  64.           client.println();
  65.           client.println("<!DOCTYPE HTML>");
  66.           client.println("<html>");
  67.           client.println("<form method=\"GET\">");
  68.           client.println("<input type=\"text\" name=\"msg\" size=\"10\" />");
  69.           client.println("<input type=\"submit\">");
  70.           client.println("</form>");
  71.           client.println("</html>");
  72.           break;
  73.         }
  74.         if (c == '\n') {
  75.           // you're starting a new line
  76.           currentLineIsBlank = true;
  77.         }
  78.         else if (c != '\r') {
  79.           // you've gotten a character on the current line
  80.           currentLineIsBlank = false;
  81.         }
  82.       }
  83.     }
  84.     // give the web browser time to receive the data
  85.     delay(1);
  86.     int firstPos = Http.indexOf("?");
  87.     if(firstPos > -1){
  88.       int lastPos = Http.indexOf(" ", firstPos+5);
  89.       String text = Http.substring(firstPos+5, lastPos);
  90.       // Serial.print("[" + text + "]");
  91.       lcd.print(text);
  92.       Http = "";
  93.     }
  94.    
  95.     // close the connection:
  96.     client.stop();
  97.     Serial.println("client disonnected");
  98.   }
  99. }

No hay comentarios:

Publicar un comentario