乐鑫技术交流
直播中

laisvl

9年用户 1144经验值
私信 关注
[问答]

如何使用HTTP GET停止正在运行的进程?

我有一个我想弄清楚的简单应用程序。我有一个基本上有两个“页面”(开始和停止)的网络服务器。当我浏览到 /start 时,一个进程开始循环。例如,LED 开始闪烁。我希望它能够无限期地运行,直到您浏览到 /stop。我看到的问题是当循环在 /start 中运行时,应用程序不响应任何 HTTP 请求。任何人都可以帮我找出解决方案吗?
例子:
代码:全选#include
#include      //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 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();
}

                                      

更多回帖

发帖
×
20
完善资料,
赚取积分