Stop a running process with HTTP GET
Posted: Wed Apr 27, 2016 3:52 pm
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:
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();
}