Server delay

Hello,

from my android application I control the garage door relay.

On the arduino running the web server on the port, the application comes a request to run the relay. Timing is set to 30s of function delay. The problem is that if I wanted to stop the door earlier, then the arduino would not let me go until the 30s had been released. Is there any way I can make another request from the application "cancel" delay?

The problem is probably in the code that we cannot see.

Thank you for your help in making the correction, although I suppose that there is the unlikely possibility that the problem could be in the power supply or elsewhere.

#include <ESP8266WiFi.h>

const char* ssid = "x";
const char* password = "#";


WiFiServer server(9000);

void setup()
{

 Serial.begin(115200);

 WiFi.begin(ssid, password);
 //  WiFi.config(ip, dns, gateway, subnet);
 WiFi.hostname("Ovladac xxx");

 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
 }

 server.begin();
}

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

 if (client) {
   while (client.connected()) {
     if (client.available()) {

       String requestBody;

       while (client.available()) {
         requestBody += (char)client.read();
       }

       String read_rele = dejHodnotu(requestBody, '&', 0);
       String read_stav = dejHodnotu(requestBody, '&', 1);
       String read_interval = dejHodnotu(requestBody, '&', 2);
       String read_h = dejHodnotu(requestBody, '&', 3);
       String read_token = dejHodnotu(requestBody, '&', 4);

       int rele = dejHodnotu(read_rele, '=', 1).toInt();
       int stav = dejHodnotu(read_stav, '=', 1).toInt();
       int interval = dejHodnotu(read_interval, '=', 1).toInt() * 1000;
       int h = dejHodnotu(read_h, '=', 1).toInt();
       String token = dejHodnotu(read_token, '=', 1);

       /* pokud prijata data obsahuji token, pokracujeme dal */

       if (token != NULL) {
         client.write("HTTP/1.1 200 OK\r\n");
         client.write("Content-Type: text/html\r\n");
         client.write("Connection: close\r\n");
         client.stop();

         pinMode(4, OUTPUT); //brana

         /* sepneme rele brana, sirena */

         digitalWrite(4, LOW); //spustime impuls do brany


         /* impuls 0.5s vypneme rele pro branu */

         delay(500);
         digitalWrite(4, HIGH);


         if (stav == 1 && h == 1) { //sirena

           pinMode(5, OUTPUT);
           digitalWrite(5, LOW);

           /* po dobe x vypneme rele pro sirenu */

           delay(interval);
           digitalWrite(5, HIGH);
         }
       }
       else {
         client.write("HTTP/1.1 403 Forbidden\r\n");
         client.write("Content-Type: text/html\r\n");
         client.write("Connection: close\r\n");
         client.stop();
       }
     }
     
   }
 }
}

String dejHodnotu(String data, char separator, int index)
{
 int found = 0;
 int strIndex[] = {0, -1};
 int maxIndex = data.length() - 1;

 for (int i = 0; i <= maxIndex && found <= index; i++) {
   if (data.charAt(i) == separator || i == maxIndex) {
     found++;
     strIndex[0] = strIndex[1] + 1;
     strIndex[1] = (i == maxIndex) ? i + 1 : i;
   }
 }
 return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
            delay(interval);

NOTHING will happen while this function is running.

You KNOW that the (client) user wanted to close the door. So, start the door closing.

Completely independently of what the user wanted to do, the door might be closing. If it is, it might be time to stop that from happening, or it might not.

The blink without delay example shows how to determine that, without using delay().

delay on siren blinking is set to 30s, if I want to stop the drive gate (open / close) for any reason, it is not a drive until the delay 30s. Web client Is not response.

Web client Is not response.

It never will be until you make the client just give directions, like "start the gate opening", "start the gate closing", and let the Arduino handle EVERYTHING else. There will be NO delay()s anywhere in your code.

As an unrelated aside, I'd change this:

const char* ssid = "x";
const char* password = "#";

to either this:

const char* const ssid = "x";
const char* const password = "#";

or this:

const char ssid[] = "x";
const char password[] = "#";

As currently written, the pointer variable can be changed at run time thus "stranding" the string literal in memory never to be heard from again.

open / close the gate works, it only triggers the pulse. but at the same time I need to have a second relay with a horn and this is a problem.

I need to have a second relay with a horn and this is a problem.

Why? What IS the problem?

The opening / closing of the garage works fine, it is a pulse. But the second relay is needed for a siren that runs for example 30s and if I want to stop the gate before it can not until the second relay is delayed.

Use millis() for timing and NOT delay(...). There is a tutorial at the top of the forum that will help you with this. Some people find millis() hard to learn but learning to not use delay(...) is worthwhile in the long run.