如何在使用 ESP8266 进行 HTML 重定向之前添加延迟?

How do I add a delay before an HTML redirect with an ESP8266?

我设置了一个简单的 ESP8266 网络服务器,允许我远程控制 LED。它使用四个按钮:一个用于每个 LED,一个用于关闭所有 LED。当您单击一个按钮时,它会将您发送到 /LED 并发送一个值。根据该值,它发回纯文本,说明 LED 亮起的颜色。到目前为止,它工作正常,LED 按预期打开或关闭。 但是,我希望它在让您知道哪个 LED 已更改后重定向到根页面。我知道您可以发送 "Location" header 然后发送 303 代码来执行此操作,但会立即重定向并且不允许我显示任何文本。在处理打开 LED 的请求时,我已经尝试过 运行 类似的操作:

server.send(200, "text/plain", "led color");
delay(2000);
server.sendHeader("Location","/");
server.send(303);

这会按预期显示文本,但不会重定向回根页面。 有没有一种直接显示文本然后在短暂延迟后重定向的方法?

完整代码如下:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h> 
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>   // Include the WebServer library

ESP8266WiFiMulti wifiMulti;     // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'

ESP8266WebServer server(80);    // Create a webserver object that listens for HTTP request on port 80

void handleRoot();              // function prototypes for HTTP handlers
void handleNotFound();

void setup(void){
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  digitalWrite(0, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);

  //Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(10);
  //Serial.println('\n');

  wifiMulti.addAP("*****", "*****");   // add Wi-Fi networks you want to connect to

  //Serial.println("Connecting ...");
  //int i = 0;
  while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    //Serial.print('.');
  }
  //Serial.println('\n');
  //Serial.print("Connected to ");
  //Serial.println(WiFi.SSID());              // Tell us what network we're connected to
  //Serial.print("IP address:\t");
  //Serial.println(WiFi.localIP());           // Send the IP address of the ESP8266 to the computer

  MDNS.begin("ledtower");
  //if (MDNS.begin("ledtower")) {              // Start the mDNS responder for ledtower.local
    //Serial.println("mDNS responder started");
  //} else {
    //Serial.println("Error setting up MDNS responder!");
  //}

  server.on("/", handleRoot);               // Call the 'handleRoot' function when a client requests URI "/"
  server.on("/LED", handleLED);             // Call the 'handleLED' function
  server.onNotFound(handleNotFound);        // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"

  server.begin();                           // Actually start the server
  //Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();                    // Listen for HTTP requests from clients
}

void handleRoot() {
  server.send(200, "text/html", "<form action='/LED' method='post'><button type='submit' name='color' value='red'>Red LED</button><br><button type='submit' name='color' value='yellow'>Yellow LED</button><br><button type='submit' name='color' value='green'>Green LED</button><br><button type='submit' name='color' value='none'>Turn off LEDs</button></form>");   // Send HTTP status 200 (Ok) and send some text to the browser/client
}

void handleLED(){
  if(server.arg("color") == "red"){
    digitalWrite(0, LOW);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    server.send(200, "text/plain", "Red!"); 
  }
  if(server.arg("color") == "yellow"){
    digitalWrite(0, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    server.send(200, "text/plain", "Yellow!"); 
  }
  if(server.arg("color") == "green"){
    digitalWrite(0, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    server.send(200, "text/plain", "Green!"); 
  }
  if(server.arg("color") == "none"){
    digitalWrite(0, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    server.sendHeader("Location","/");
    server.send(303);
  }
  else{
    server.send(400, "text/plain", "400: Invalid Request");
  }
}

void handleNotFound(){
  server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}

这不是 HTTP 重定向的工作方式。您可以发送重定向 您可以发送内容。你不能两者都做。此外,由于 HTTP 重定向是一个 HTTP header 字段,因此必须在发送任何内容之前发送它。

您可以使用 HTML 重定向。您需要 return 一个格式正确的 HTML,看起来像这样:

<html>
  <head>
    <meta http-equiv="refresh" content="2; URL=/" />
  </head>
  <body>
    led color
  </body>
</html>

通常显示内容然后重定向是一个糟糕的设计。调试没问题,但与真实用户交互是一种糟糕的方式,因为如果他们将目光移开片刻,他们就会错过消息。最好重定向到所需页面并让该页面在状态或 "flash" 消息中显示内容。