LusoRobótica - Robótica em Português
Sistemas específicos => Arduino / AVR => Tópico iniciado por: poliveira1978 em 19 de Setembro de 2011, 22:49
-
Alterações ao código, deixa de haver entradas estáticas no código, é tudo gerado com um ciclo de 2 a maxrelays +2 (o relay 0 corresponde ao pino 2, para não interferir com o tx e rx do arduino):
#include "etherShield.h"
#include "ETHER_28J60.h"
#include <stdio.h>
static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};
static uint8_t ip[4] = {xxx, xxx, xxx, xxx}; //alterar para um ip válido na LAN
static uint16_t port = 80;
ETHER_28J60 e;
#define maxrelays 8
void setup()
{
e.setup(mac, ip, port);
for(int(relay)=2;relay<(maxrelays+2);relay++)
{
pinMode(relay,OUTPUT);
digitalWrite(relay,HIGH); //o relay desliga com o pino HIGH
}
}
void loop()
{
char* params;
while (params = e.serviceRequest())
{
e.print("<html><head><title>Web Controlled Relays</title></head><body><table border=\"1\" width=\"20%\"><tr><td>Relay</td><td>Status</td>");
for(int(relay)=2;relay<(maxrelays+2);relay++)
{
char resp[3];
sprintf(resp, "%s%d","?", relay); // "?" -> string; relay -> inteiro
if (strcmp(params,resp) == 0) digitalWrite(relay,!digitalRead(relay));
if (strcmp(params,"?allon") == 0) for (int(relay)=2;relay<maxrelays+2;relay++) digitalWrite(relay,LOW);
if (strcmp(params,"?alloff") == 0) for (int(relay)=2;relay<maxrelays+2;relay++) digitalWrite(relay,HIGH);
e.print("<tr><td>");
e.print(relay);
e.print("</td><td><a href=?");
e.print(relay);
e.print(">");
if (digitalRead(relay)==LOW) e.print("ON");
if (digitalRead(relay)==HIGH) e.print("OFF");
e.print("</a></td></tr>");
}
e.print("</table><br><a href=\"?allon\">All On</a><br><a href=\"?alloff\">All Off</a></body></html>");
e.respond();
}
}