Wrong behavior of relay activation sketch

Hi Friend!

Thank you to be a new small part of this great community.

I may ask for help instantly because I cannot find a mistake in my sketch.

The goal: Arduino is controlling via Ethernet Shield a Relay shield, remotely operated by a simple web page.
The problem: On the web page, the push button who should trigger one of the relays, also is triggered by opening the page or by refreshing the page.

I have no idea what could be the problem: Syntax? Wrong placement of code segments? Missing or wrong code?

I like to show the complete code here, maybe somebody is able to find the bug? I am not, sorry :confused:

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

// MAC-Adresse des Ethernet-Shields
byte mac[] = {0xA8, 0x0A, 0xAE, 0xDE, 0xD9};

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

EthernetServer server = EthernetServer(80);

const int relayPin = 8;

void setup() {
  pinMode(relayPin, OUTPUT);
  
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
}

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);
}

Thank you for watching!

Jens

Hi, Jens. Welcome to the Arduino forum.

The age-old way of debugging a program is to use serial.Print() to display messages showing the steps your logic takes in your program. Then you can see where to also print the values of associated variables and you can use it to print the values coming from your web page. Good luck!

Okay, I will try that, thanks!

First attempt to get output from Serial Monitor failed.

I inserted both strings to the sketch setup section:

Serial.begin(9600);
Serial.println("Monitor okay");

but even did not get any response back.
Test sketches with simple setup / loop monitor actions are working...

programming is soo frustrating to me... (grrrr)

Any ideas?

Thanks!!!

True, until you actually turn on the serial in the IDE screen.

For sure I activated the monitor in the IDE screen...
The test scetches delivered replies...
I also tried switching the serial instruction to the start of the setup section, before all other instructions, no result. Screen remains blank...

Okay, so show us that most recent version, please. Verbal description sounds like it should work, so we need to see it to evaluate what's still wrong.

Thanks...

Thats the actual sketch version I tried:

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

// MAC-Adresse des Ethernet-Shields
byte mac[] = {0xA8, 0x0A, 0xAE, 0xDE, 0xD9};

// IP-Adresse, Subnetzmaske und Gateway
IPAddress ip(11, 9, 69, 222); 
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);
  Serial.println("Monitor okay");
  Ethernet.begin(mac, ip, gateway, 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);
}

Another person in a german forum told me it could be a problem with the (client.find("trigger") phrase...

  1. Which Arduino? Uno?
  2. After the Serial.println() in setup, before anything else, put in a delay(1000). That should allow the serial output to complete before anything else hangs/crashes. If you still don't see output in Serial Monitor, there's no point in debugging anything downstream of that.

Report back.

the delay string did not work... still no response from the serial monitor :frowning_face:

I use the following setup:

Arduino Uno R4 Minima,
Arduino Ethernet Shield
Arduino Relay Shield V2

Okay. I don't have an R4, and have no experience with them. My guess is, there's some form of startup synchronization(or something different about the serial usage) that you've run afoul of, so maybe look for guidance in any examples you can find that use it.

I'm thinking about buying a regular Uno R3 to possibly avoid such problems... It's also a different microprocessor...
Maybe that can help???

Don't know what you want to do, so can't advise. Certainly, if you want Ethernet then for an Uno R3 you're into an addon shield, etc. Which can also be painful.

There is an R4 subforum here, perhaps your question could be moved, now that we know an R4 is involved? It would then be seen by people with some experience with the R4.
I'll flag for a move.

okay...
I ordered also an R3, the Ethernet Shield and Relay Shile I have already..

As told earlier in this thread, there is a simple goal for this project: Trigger a relay for 1.5 secinds from a web page by pressing a button. The sketch is working but the trigger is realeased every time I open the web page or reload the page...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.