Helly guys I'm doing a project with the ethernet shield and basically what I want to do is to make a local webpage where I have 4 buttons and 2 arduino inputs displays (text indicating the state of an input pin) and I can read the pressing of the buttons and then according to the pressed button I activate an output (a led). I've tried several thing but using client.print was the easiest thing to make but so far I can't manage a way to publish a buton and read that action in order to toggle an exit (I'm uding digitaltoogle library so make this easier but so far i can't read the button being pressed).
Currently I have the following code:
//Carga de librerias para la comunicacion Ethernet y la conmutacion digital de estados
#include <DigitalToggle.h>
#include <SPI.h>
#include <Ethernet.h>
//Declaracion de direccion MAC del Shield Ethernet e IP deseada para el servidor
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x32, 0xA5 };
byte ip[] = { 192,168,1,10 };
//Se utiliza el puerto 80 ya que es el default de http
Server server(80);
//Se crean las variables necesarias para la declaracion de pines del microcontrolador y estados de los elementos de entrada
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int pin9 = 9;
int boton=0;
int sensor=0;
//Inicializacion del servidor
void setup(){
Ethernet.begin(mac, ip);
server.begin();
//Configuracion de entrada o salida los pines
//Entradas, pin2=boton, pin3=sensor
pinMode(pin2,INPUT);
pinMode(pin3,INPUT);
//LEDS
pinMode(pin4,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);
pinMode(pin7,OUTPUT);
//Motores
pinMode(pin8,OUTPUT);
pinMode(pin9,OUTPUT);
}
void loop()
{
Client client = server.available();
//Se detecta si current es la primer linea
boolean current_line_is_first = true;
if (client) {
//La peticion http termina con una linea en blanco
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Si se ha llegado al final de la linea y al recibir una nueva en blanco, se termina la peticion http y se puede enviar una respuesta
if (c == '\n' && current_line_is_blank) {
//Se envia un encabezado http estandar
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//Meta tag html para generar un refresh de la pagina cada 4 segundos
client.println("<head><meta http-equiv='refresh' content='4;url=http://192.168.1.10'> </head>");
//Titulo de la pagina
client.println("<center><TITLE>Proyecto Redes Industriales 2012</TITLE></center>");
//Color del fondo de la pagina
client.println("<body><body bgcolor='419ed4'>");
//Encabezado de bienvenida e info general
client.println("<center><H1>Bienvenido</H1></center>");
client.println("<center><H1>Control remoto de hogar =Primer Piso=</H1></center>");
client.println("<center>De click en el siguiente link para ir al Control del Piso 2</center>");
client.println("<CENTER><P><a href='http://192.168.1.20'>Control Piso 2</a></CENTER>");
//Lectura y despliegue de pines de entrada
boton = digitalRead(pin2);
if (boton == HIGH) { // Alarma activada
client.println("<center><h2><font color=green>Alarma Activada</font></h2></center>");
}
else { // Alarma desactivada
client.println("<center><h2><font color=red>Alarma Desactivada</font></h2></center>");
}
sensor = digitalRead(pin3);
if (sensor == HIGH) { // Sensor presionado, puerta cerrada
client.println("<center><h2><font color=green>Puerta Cerrada</font></h2></center>");
}
else { // Sensor libre, puerta abierta
client.println("<center><h2><font color=red>Puerta Abierta!</font></h2></center>");
}
//Programacion de botones
//Botones de iluminacion
client.println("<center>Control de iluminacion</center>");
client.println("<center><form method=get><INPUT TYPE='Submit'VALUE='pin4'NAME='4'STYLE='width:150px;height:100px;background-color:#47e5e7'>");
client.println("<INPUT TYPE='Submit'VALUE='pin5'NAME='5'STYLE='width:150px;height:100px;background-color:#47e5e7'></form></CENTER>");
client.println("<center><form method=get><INPUT TYPE='Submit'VALUE='pin6'NAME='6'STYLE='width:150px;height:100px;background-color:#47e5e7'>");
client.println("<INPUT TYPE='Submit'VALUE='pin7'NAME='7'STYLE='width:150px;height:100px;background-color:#47e5e7'></form></CENTER>");
//Botones del control de puertas
client.println("<center>Control de apertura y cierre de garage</center>");
client.println("<CENTER><P><INPUT TYPE='Submit'VALUE='Abrir'NAME='8'STYLE='width:150px;height:100px;background-color:#47e5e7'>");
client.println("<INPUT TYPE='Submit'VALUE='Cerrar'NAME='9'STYLE='width:150px;height:100px;background-color:#47e5e7'></CENTER>");
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_first = false;
current_line_is_blank = true;
}
else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
// get the first http request
if (current_line_is_first && c == '=') {
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
And I want to use the following structure for toggling the pins when each button is pressed.
if(c == '5'){
digitalToggle(pin5);
break;
Anyone got any idea for me D:?