-->
Page 1 of 1

Stop a running process with HTTP GET

PostPosted: Wed Apr 27, 2016 3:52 pm
by edash-
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();
}

Re: Stop a running process with HTTP GET

PostPosted: Thu Apr 28, 2016 9:44 am
by eduperez
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.

Re: Stop a running process with HTTP GET

PostPosted: Fri Apr 29, 2016 1:45 pm
by edash-
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.

Re: Stop a running process with HTTP GET

PostPosted: Sun Nov 25, 2018 12:26 am
by victorvital100
Excuse me, did you solve that prolem? I have the same problem and I don't know how to solve it, did you have an example?