3 input (on/off/off with delay) to control 1device using ESP8266 as Web Server

HELLO Guys;
First of all , i apologize for my poor English .
I want to control one device in three ways : turn on / turn off / turn off with delay
To do that i use an ESP8266 web server + android phone
The problem is that turn off with delay wont work.
I would be grateful if you could help me.

#include <ESP8266WiFi.h>        
const int device1 = 14;
unsigned long previousMillis = 0;
const long interval = 5000;
const char* ssid     = "****";         
const char* password = "****";     
#include <Arduino.h>
WiFiServer server(80);
String data = "";

void setup() {
 pinMode(device1, OUTPUT);
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
WiFi.begin(ssid, password);
IPAddress ip(*,*,*,*);   
IPAddress gateway(*,*,*,*);   
IPAddress subnet(*,*,*,*);   
WiFi.config(ip, gateway, subnet);
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
 
 server.begin();

 Serial.println(WiFi.localIP());
 }

void loop() {
    unsigned long currentMillis = millis();

  WiFiClient client = server.available();
 if (!client) {
   return;
 }
 
 while(!client.available()){
 }
 
 String req = client.readStringUntil('\r');
 client.flush();
 if (req.indexOf("/on") != -1)
    digitalWrite(device1, HIGH);
 if (req.indexOf("/off") != -1)
    digitalWrite(device1, LOW);

 if (req.indexOf("/delay") != -1)
  unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

   
    digitalWrite(device1, LOW);

}
}

You need to set previousMillis when you receive the delay command.

thank you for your response
i did that but nothing changed

if (req.indexOf("/delay") != -1)
  unsigned long currentMillis = millis();
  unsigned long previousMillis = 0;
if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

   
    digitalWrite(device1, LOW);

Set it to millis, not zero. Also, you need braces so it's controlled by the if.

:slightly_frowning_face: nothing happened!!

if (req.indexOf("/delay") != -1)
 {
  unsigned long currentMillis = millis();
  unsigned long previousMillis = millis();
if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

   
    digitalWrite(device1, LOW);

}
}

Print the code. Work through loop line by line and mentally "run" the code. See if you can see what's happening - debugging is as important (if not more so) than being able to write code.

its not working :slightly_frowning_face: it works if i change my input from server commands to physical push button command
. im confused.could you write the whole sketch in correct way for me please.