0

我对搞乱 esp 很陌生,我已经设法 - 使用 ARDUINO IDE - 创建一个带有按钮的本地页面,一个引导我到我的本地服务器,另一个是我的问题的主题,我需要它将我重定向到不断变化的 URI,URI 是通过串行连接从 arduino 获取的,任何帮助将不胜感激,在此先感谢

这是我的代码:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid = "SSID";
const char* password = "PASSWORD";

ESP8266WebServer server(80);

//Check if header is present and correct
bool is_authentified(){
  Serial.println("Enter is_authentified");
  if (server.hasHeader("Cookie")){
    Serial.print("Found cookie: ");
    String cookie = server.header("Cookie");
    Serial.println(cookie);
    if (cookie.indexOf("ESPSESSIONID=1") != -1) {
      Serial.println("Authentification Successful");
      return true;
    }
  }
  Serial.println("Authentification Failed");
  return false;
}

//login page, also called for disconnect
void handleLogin(){
  String msg;
  if (server.hasHeader("Cookie")){
    Serial.print("Found cookie: ");
    String cookie = server.header("Cookie");
    Serial.println(cookie);
  }
  if (server.hasArg("DISCONNECT")){
    Serial.println("Disconnection");
    server.sendHeader("Location","/login");
    server.sendHeader("Cache-Control","no-cache");
    server.sendHeader("Set-Cookie","ESPSESSIONID=0");
    server.send(301);
    return;
  }
  if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){
    if (server.arg("USERNAME") == "98710" &&  server.arg("PASSWORD") ==     "hussein" ){
      server.sendHeader("Location","/");
      server.sendHeader("Cache-Control","no-cache");
      server.sendHeader("Set-Cookie","ESPSESSIONID=1");
      server.send(301);
      Serial.println("Log in Successful");
      return;
    }
  msg = "invalid username/password please try again";
  Serial.println("Log in Failed");
  }
  String content = "<center><div id=\"ucPageViewer_ctl00_TopHead\"><div     id=\"ucPageViewer_ctl00_ctl03_dvBody\" class=\"\"><div     id=\"ucPageViewer_ctl00_ctl03_ctl00_containerid\"><a     href=\"http://www.ungift.org/images/knowledgehub/logos/usaid-logo.jpeg\"><img     border=\"0\" alt=\"main page\"     src=\"http://www.ungift.org/images/knowledgehub/logos/usaid-logo.jpeg\"     width=\"930\" height=\"110\"></a></div></div></div></center>";
  content += "<center></center> ";
  content += "<center></center>";
  content += "<center><html><body><form action='/login' method='POST'>please     enter your info to be able to login<br></center>";
  content += "<center>Username:<input type='text' name='USERNAME'     placeholder='Username'><br></center>";
  content += "<center>Password:<input type='password' name='PASSWORD'     placeholder='Password'><br></center>";
  content += "<center><input type='submit' name='SUBMIT' value='sign in'>    </form>" + msg + "<br></center>";
  content += "<center>for more info<a href='/inline'>press here</a></body>    </html></center>";
  server.send(200, "text/html", content);
}

//root page can be accessed only if authentification is ok
void handleRoot(){
  Serial.println("Enter handleRoot");
  String header;
  if (!is_authentified()){
    server.sendHeader("Location","/login");
    server.sendHeader("Cache-Control","no-cache");
    server.send(301);
    return;
  }
  String content = "<center><div id=\"ucPageViewer_ctl00_TopHead\"><div     id=\"ucPageViewer_ctl00_ctl03_dvBody\" class=\"\"><div     id=\"ucPageViewer_ctl00_ctl03_ctl00_containerid\"><a     href=\"http://www.ungift.org\"><img border=\"0\" alt=\"main page\"     src=\"http://www.ungift.org/images/knowledgehub/logos/usaid-logo.jpeg\"     width=\"930\" height=\"110\"></a></div></div></div></center>";
  if (server.hasHeader("User-Agent")){
  }
    content += "<center><img crossorigin=\"anonymous\"     src=\"https://upload.wikimedia.org/wikipedia/ar/b/bf/Yarmouk_University_Logo.png    \"width=\"168\" height=\"250\"</center>";
    content += "<h1><center>hi there</center></h1>";
    content += "<center><p><a href=\"socket2On\"><button>go to link from     serial </button></a></p></center>";
    content += "<center><p>browse server   <a     href=\"192.168.1.240/main.html"><button>ON</button></a></p></center>";
    content += "please sign out before leaving <a href=\"/login?    DISCONNECT=YES\">sign out</a></body></html>";  

  server.send(200, "text/html", content);
}

//no need authentification
void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup(void){
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());


  server.on("/", handleRoot);
  server.on("/login", handleLogin);
  server.on("/inline", [](){
    String MGS = "<h1><center>please enter valid username and     password</center></h1>";
    MGS += "to go back to login page please press `enter code here`<a     href=\"/login?DISCONNECT=YES\">here</a></body></html>"; 
    server.send(200, "text/html", MGS );
  });
   server.on("/search", [](){
    String T = "";
     T = Serial.readString();
     Serial.print(T);
    server.send(200, "text/html", T );
  });

  server.onNotFound(handleNotFound);
  //here the list of headers to be recorded
  const char * headerkeys[] = {"User-Agent","Cookie"} ;
  size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
  //ask server to track these headers
  server.collectHeaders(headerkeys, headerkeyssize );
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
      }
4

0 回答 0