Adjustable delay

Hello, I'm new to coding and wanted to make a web page through an esp32 that can remotely change the delay through my phone. For example, I have a simple program that has a delay for 2000ms, is it possible to remotely change the delay anywhere from 2000ms to 5000ms through the webpage?
digitalWrite(relay_pin, HIGH);
delay(2000);

unsigned long x;

//set x with phone..

digitalWrite(relay_pin, HIGH);
delay(x);

Like that.

-jim lee

also google for "esp32 web server"
You use the web browser on your smart phone to talk to the web server.

How can I set x with my phone? Sorry, I'm new to this. Here's what I've been working on.

#include <WiFi.h>

WiFiServer server(80);
WiFiClient client;

const char* ssid = "esp32"; // Your Wifi network name here
const char* password = "12345678"; // Password

int relay_pin = 15;

unsigned long x;

void setup()
{

Serial.begin (115200);
pinMode (relay_pin, OUTPUT);
digitalWrite(relay_pin, LOW);

Serial.print ("Connecting to ");
Serial.print(ssid);

//CONNECT TO WIFI
WiFi.softAP(ssid, password);

Serial.println( "" );
Serial.println( "WiFi AP is now running" );
Serial.println( "IP address: " );
Serial.println( WiFi.softAPIP() );

server.begin(); // Starting the server
}

void loop()
{
WiFiClient client = server.available(); //Checking if any client request is available or not
if (client)
{
boolean currentLineIsBlank = true;
String buffer = "";
while (client.connected())
{
if (client.available()) // if there is some client data available
{
char c = client.read();
buffer+=c; // read a byte
if (c == '\n' && currentLineIsBlank) // check for newline character,
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("Relayesp32");
client.print("

Relay

");
client.print("

Relay Control

");
client.print("<a href="/?relayon"">ON");
client.print("<a href="/?pms"">Increase"); //pms== positive ms
client.print("");

      break;        // break out of the while loop:
    }
    if (c == '\n') { 
      currentLineIsBlank = true;
      buffer="";       
    } 
    else 
      if (c == '\r') {     
      if(buffer.indexOf("GET /?relayon")>=0)
        digitalWrite(relay_pin, HIGH);
        delay(x);
        digitalWrite(relay_pin, LOW);

      if(buffer.indexOf("GET /?pms")>=0)
        //controll delay here
    }
      
    else {
      currentLineIsBlank = false;
    }  
  }
}
client.stop();

}
}

Break it down into separate parts.
There is a standard web server which serves a page with a parameter the user can adjust. You should be able to find examples, one of which you seem to have already found.
There is a state problem where you issue a command to switch a relay on and then, after a preset time, defined by your parameter, it switches itself off.

Format the code in the IDE and use code tags when posting code on this forum.

Repeat that last bit to yourself every day.

1 Like