Académique Documents
Professionnel Documents
Culture Documents
ACTIVIDADES DESARROLLADAS
1. Configuración del Arduino
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,50);
EthernetServer server(80);
int led=7;
String estado="OFF";
void setup() {
Ethernet.begin(mac, ip);
server.begin();
pinMode(led,OUTPUT);
}
void loop() {
EthernetClient cliente = server.available();
if (cliente) {
boolean currentLineIsBlank = true;
String cadena="";
while (cliente.connected()) {
if (cliente.available()) {
char c = cliente.read();
cadena.concat(c);
int posicion=cadena.indexOf("LED=");
if(cadena.substring(posicion)=="LED=ON")
{
digitalWrite(led,HIGH);
estado="ON";
}
if(cadena.substring(posicion)=="LED=OFF")
{
digitalWrite(led,LOW);
estado="OFF";
}
if (c == '\n' && currentLineIsBlank) {
cliente.println("HTTP/1.1 200 OK");
cliente.println("Content-Type: text/html");
cliente.println("Connection: close");
//cliente.println("Refresh: 3");
cliente.println();
cliente.println("<!DOCTYPE HTML>");
cliente.println("<html>");
cliente.println("<HEAD>");
cliente.println("<TITLE>Ethernet Monitor</TITLE>");
cliente.println("</HEAD>");
cliente.println("<BODY>");
cliente.println("<hr />");
cliente.println("<H1>Lab. Comunicaciones (SI)</H1>");
cliente.println("<br />");
cliente.println("<H2>AUTOR: Luis Zhingri</H2>");
cliente.println("<br />");
cliente.println("Practica 10: Ethernet");
cliente.println("<br />");
cliente.println("<br />");
cliente.println("<div style='text-align:center;'>");
cliente.println("<button onClick=location.href='./?LED=ON\' style='margin:auto;background-color: #FE2E2E;color:
snow;padding: 10px;border: 1px solid ##FE2E2E;width:65px;'>");
cliente.println("<H2>ON<H2>");
cliente.println("</button>");
cliente.println("<button onClick=location.href='./?LED=OFF\' style='margin:auto;background-color: #84B1FF;color:
snow;padding: 10px;border: 1px solid #3F7CFF;width:65px;'>");
cliente.println("<H2>OFF<H2>");
cliente.println("</button>");
cliente.println("<br /><br />");
cliente.println("<H2> <b>LED = <H2>");
cliente.print(estado);
cliente.println("</b><br />");
int lecturaSensor = analogRead(0);
cliente.print("<H2>Valor Analogico es: <H2>");
cliente.print(lecturaSensor);
cliente.println("<br />");
cliente.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(15);
cliente.stop();
}
}
2. Código HTML
- EthernetServer server(80);
Creamos un servidor Web con el Puerto 80 que es el puerto HTTP por defecto
- EthernetClient client = server.available();
Creamos un cliente Web el mismo que lo detectara a través de una petición HTTP
- cliente.println("<button onClick=location.href='./?LED=ON\' style='margin:auto;background-color: #FE2E2E;color:
snow;padding: 10px;border: 1px solid ##FE2E2E;width:65px;'>");
Crea los botones. Para enviar parámetros a través de HTML se utiliza el método URL encode. Los parámetros se envían a
través del símbolo ‘?’.
RESULTADO(S) OBTENIDO(S):
- Interfaz de la página web
Firma: _________________________