Issue with LAN shield using web interface

Hi friends!

Since some months I do try to get fixed a (maybe small) problem, using my LAN shield in combination with a relay shield via web access.
It should be a small project with a very simple web site, controlling a relay with a simple button...

My problem: The sketch is working, if I press the button, the relay reacts as needed. BUT every time I refresh the web page, the relay again closes. This is a fatal behavior because the the controlled computer again gets an impulse and is shutting down.

I think the problem might be the syntax of the sketch. I am not a specialist and therefore I cannot determine the error. Most parts are from free documents and some parts are written by a mate.

I could paste some parts or the whole sketch here for inspection, maybe the error is found quickly?

Thank you for the attention and suggestion!

Jens

Post your full sketch so it can be compiled by anyone.
If you use several libraries clearly indicate what library you are using (how it is called in the library manager) including the used version.

If you don't want to post your full sketch, here is my webserver example which will work:

Thanks for the response!

I also found out that the relays is triggered even if I start the browser with the page, so it seems this is a problem with the http protocol handling?

#include <SPI.h>
#include <Ethernet.h>

// MAC-Adresse des Ethernet-Shields
byte mac[] = {, , , , , }; // you may use your hardware mac adress for testing

// IP-Adresse, Subnetzmaske und Gateway
IPAddress ip(11, 9, 69, 222); // intrernal IP, subnet and gateway
IPAddress subnet(255, 0, 0, 0);
IPAddress gateway(11, 9, 69, 1); 

EthernetServer server = EthernetServer(80);

const int relayPin = 8;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  pinMode(relayPin, OUTPUT);
  
}

void loop() {
  EthernetClient client = server.available();

  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
        if (c == '\n' && currentLineIsBlank) {
          // HTTP-Header senden
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          
          // HTML-Seite senden
          client.println(" <!DOCTYPE html><html>");
          client.println(" <head><title>GM2000 Bootbutton</title></head>");
          client.println(" <body bgcolor=\"#6E6E6E\">");
          client.println(" <div style=\"text-align: center; font: Arial; size: 16pt\">");
          client.println(" <br><br><br><br>");
          client.println(" <br><br><br><br>");
          client.println(" <br><br><br><br>");
          client.println(" <h1>10Micron GM2000-HPSII</h1>");
          client.println(" <br>");
          client.println(" <button style=\"height: 70px; width: 190px; font-size: 14pt; background-color: #d57800; border-radius: 10px; cursor: pointer; \" onclick=\"activateRelay()\">Boot / Shutdown</button>");
          client.println(" <br>");
          client.println(" <br>");
          client.println(" <span style=\"color: #393939;\">Feste IP: 11.9.69.222</span>");
          client.println(" </div>");
          client.println(" <script>");
          client.println(" function activateRelay() {");
          client.println("  var xhttp = new XMLHttpRequest();");
          client.println("  xhttp.open(\"GET\", \"/trigger\", true);");
          client.println("  xhttp.send();");
          client.println("}");
          client.println(" </script>");
          client.println(" </body></html>");
          
          // Wenn der Button auf der Webseite gedrückt wurde, Relais für 1500 ms aktivieren
          if (client.find("trigger") != -1) {
            activateRelay();
          }
          
         break;
        }
        
        if (c == '\n') {
          currentLineIsBlank = true;
        } else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    
    delay(1);
    client.stop();
  }
}

void activateRelay() {
  digitalWrite(relayPin, HIGH);
  delay(1500); // Relais für 1500 ms aktivieren
  digitalWrite(relayPin, LOW);
}

Thanks!

Jens

no, it's your code.

have you tried my example? What did you experience with my code?

Not yet, at home from work right now...