Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By edash-
#46348 I have a simple application that I am trying to figure out. I have a web server that basically has two "pages" (start and stop). When I browse to /start a process starts in a loop. For example, an LED starts blinking on and off. I would like this to be able to run indefinitely until you browse to /stop. The problem I am seeing is that while the loop is running in /start, the application does not respond to any HTTP requests. Can anyone help me figure out a solution?

Example:

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal

const char *ssid =  "myssid";
const char *pass =  "mypassword";

boolean proceed = false;

// Start WiFi Server
std::unique_ptr<ESP8266WebServer> server;

void setup() {
 
  // Setup console
  Serial1.begin(115200);
  delay(10);
  Serial1.println();
  Serial1.println();
 
  WiFi.begin(ssid, pass);

  server.reset(new ESP8266WebServer(WiFi.localIP(), 80));

  server->on("/", []() {
    server->send(200, "text/plain", "this works as well");
  });

  server->on("/inline", []() {
    server->send(200, "text/plain", "this works as well");
  });

  server->on("/start", []() {
    server->send(200, "text/plain", "Program has started");
    proceed = false;
    unsigned long previousMillis = millis();
    unsigned long currentMillis = millis();
    while (proceed == false) {
      //repeating task will go here
      currentMillis = millis();
      //break out of the loop after 600 seconds
      if (currentMillis - previousMillis >= 600000) {
        proceed = true;
      }
      delay(1);
    }
  });

  server->on("/stop", []() {
    proceed = true;
    server->send(200, "text/plain", "Program has stopped");
  });

  server->onNotFound( []() {
    server->send(404, "text/plain", "Not Found");
  });

  server->begin();

}

void loop() {
  server->handleClient();
}
User avatar
By eduperez
#46399 Use a global flag to indicate whether the task should run or not, then activate the flag at "/start", and disable it at "/stop".
In your loop(), perform one iteration of your task (if the flag is enabled), and then handle requests.
User avatar
By edash-
#46500
eduperez wrote:Use a global flag to indicate whether the task should run or not, then activate the flag at "/start", and disable it at "/stop".
In your loop(), perform one iteration of your task (if the flag is enabled), and then handle requests.


Perfect, thanks. I moved my loop to the main loop and created a global flag to "enable" it as suggested. Things are working as they should. I really appreciate it.