如何在ESP8266wifiserver中添加去认证

时间:2019-06-04 12:24:51

标签: arduino esp8266 arduino-ide arduino-esp8266

我正在esp8266WiFiServer中设置身份验证,并设法添加了身份验证过程。但是问题是我无法在代码中添加注销/取消身份验证。


#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>

#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK  "your-password"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServer server(80);

const char* www_username = "admin";
const char* www_password = "esp8266";

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Connect Failed! Rebooting...");
    delay(1000);
    ESP.restart();
  }
  ArduinoOTA.begin();

  server.on("/", []() {
    if (!server.authenticate(www_username, www_password)) {
      return server.requestAuthentication();
    }
    server.send(200, "text/plain", "Login OK");
  });
  server.begin();

  Serial.print("Open http://");
  Serial.print(WiFi.localIP());
  Serial.println("/ in your browser to see it working");
}

void loop() {
  ArduinoOTA.handle();
  server.handleClient();
}

我正在尝试代码并且运行良好。但是我需要一个注销按钮。任何想法我该怎么做?

谢谢

1 个答案:

答案 0 :(得分:0)

只需添加以下内容即可注销:

server.on(“ / logout”,{ server.send(401,“ text / html”,“注销!”); });