Reed Switch + Ethernet ENC...

Friends, I am using the following code to call a web page and wanted when my reedsiwtch closed contact, it triggers a relay and when the contact was open it disables the relay. How can I fix this?

#include <EtherCard.h>

#define LED1PIN  3 //definisce il pin del LED 1
#define LED2PIN  4 //definisce il pin del LED 2

int contato = 7;
int ledvermelho = 6;
int ledverde = 5;


//settaggio dei valori statici
static byte mymac[] = {0x00,0x19,0xCB,0xF4,0x03,0x01};
static byte myip[] = {192,168,14,99};
static byte gwip[] = {192,168,14,3};
static byte netmask[] = {255,255,255,0};
static byte dnsip[] = {192,168,0,22};
byte Ethernet::buffer[700];

boolean led1Status;
boolean led2Status;

void setup () {

  Serial.begin(57600);
  Serial.println("2 WebRele' Ip Statico");

  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Accesso fallito all'Ethernet Shield");
 else
   Serial.println("Ethernet Shield initializzato");

  if (!ether.staticSetup(myip, gwip, dnsip, netmask ))
    Serial.println("Impossibile assegnare l'indirizzo");
  else
    Serial.println("Indirizo statico configurato");
    
  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.netmask);
  ether.printIp("Gateway:\t", ether.gwip);
  ether.printIp("DNS:\t\t", ether.dnsip);
   Serial.println();
  
  pinMode(contato,INPUT);
  pinMode(ledverde,OUTPUT);
  pinMode(ledvermelho,OUTPUT);
  
  pinMode(LED1PIN, OUTPUT);
  pinMode(LED2PIN, OUTPUT);
  
  
  digitalWrite(LED1PIN, HIGH);
  digitalWrite(LED2PIN, HIGH);
  
  led1Status = false;
  led2Status = false;  
}
  
void loop() {

  
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if(pos) {
    
      if(digitalRead(contato)==LOW && strstr((char *)Ethernet::buffer + pos, "GET /?RELAY1") != 0) {
        led1Status = !led1Status;
        digitalWrite(LED1PIN, led1Status);
        digitalWrite(ledverde,HIGH);
        digitalWrite(ledvermelho,LOW);
        Serial.println("Porta Fechada");  
      }
      
      if(strstr((char *)Ethernet::buffer + pos, "GET /?RELAY2") != 0) {
        led2Status = !led2Status;
        digitalWrite(LED2PIN, led2Status);
        digitalWrite(ledverde,LOW);
        digitalWrite(ledvermelho,HIGH);
        Serial.println("Porta Fechada!");        
      }
     
      
      BufferFiller bfill = ether.tcpOffset();
      bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
                        "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
                        "<html><head><meta name='viewport' content='width=200px'/></head><body>"
                        "<div style='position:absolute;width:200px;height:200px;top:1%;left:50%;margin:5px 0 0 -100px'>"
                        "<div style='font:bold 18px verdana;text-align:center'>Web Rele'</div>"
                        "
<div style='text-align:center'>"));

      if(led1Status) bfill.emit_p(PSTR("<a href=\"/?RELAY1\">LIGA</a>
Status Porta Fechada
"));
      else bfill.emit_p(PSTR("<a href=\"/?RELAY1\">DESLIGA</a>
Status Porta Aberta
"));
      
      if(led2Status) bfill.emit_p(PSTR("
<a href=\"/?RELAY2\">LIGA</a>
Status Rele' 2 OFF"));
      else bfill.emit_p(PSTR("
<a href=\"/?RELAY2\">DESLIGA</a>
Status Porta Fechada"));      

      bfill.emit_p(PSTR("
<a href=\"/\">Controle de status do Rele'</a></div></div></body></html>"));
      ether.httpServerReply(bfill.position());
   }

  }

It makes it very difficult to understand your code when you attach a relay to a pin named LED1PIN.

I suppose you plan to attach some LEDs to pins named RELAY1PIN, RELAY2PIN, etc.

If writing HIGH to the pin turns the relay off, and writing LOW turns it on, you know the values that you need to write to the pin to do what you want the relay to do. So, write them.

Hello.

Resolve as follows:

But now when I call refresh the page using:

if(pos) {
      BufferFiller bfill = ether.tcpOffset();
      
      if(digitalRead(contato)==HIGH) {
        bfill.emit_p(PSTR("
Status Porta Fechada
<img src=\"http://thumbs.dreamstime.com/x/porta-alaranjada-fechada-com-sinal-da-porta-do-manequim-12926250.jpg\">"));

        Serial.println("Porta Fechada");  
      }else {
         bfill.emit_p(PSTR("
Status Porta Aberta
<img src=\"http://sistemas.abrapp.org.br/apoio/porta/portal.png\">"));
        Serial.println("Porta Fechada");  
      }

He drops my connection, but only tips if I close contact with the Reed Switch, but it works normal, but only after I put a refresh tag.

<META HTTP-EQUIV=Refresh CONTENT='5; URL=http://192.168.14.99/'>