NodeMcu Web_Server не будет перезагружаться при подключении нового клиента - PullRequest
0 голосов
/ 26 января 2020

Ну, я заинтересовался NodeMcu пару дней go, и я попытался сделать Wifi-сервер, который может контролировать четыре Gp ios, подключенных к плате. Я смог создать код, и он работает безупречно, но проблема, с которой я сталкиваюсь, может быть поведением доски или тем, чего я не знаю. В основном, когда плата включена, вновь подключенный клиент может контролировать gpio (s), но когда он отключается и пытается подключиться снова, он не перезагружает страницу, которую он просто застрял, и не будет отображать веб-страницу. Если я сбросил (ВЫКЛЮЧЕНА, а затем ВКЛЮЧЕНА) плату, то первый клиент сможет получить доступ к веб-странице. В чем может быть проблема, если она связана с кодом, тогда я опубликую код, но сначала скажу, что это потому, что я подозреваю что это доска природы, она работает следующим образом.

  #include <ESP8266WiFi.h>
    const char* ssid = "Anshumaan";  // Enter SSID here
    const char* password = "zxcvbnm123";  //Enter Password here
// Variable to store the HTTP request
String header;
String LED1State = "off";
String LED2State = "off";
String LED3State = "off";
String LED4State = "off";
const int LED1 = D0;
const int LED2 = D1;
const int LED3 = D2;
const int LED4 = D3;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
 Serial.print("Connecting to ");
 Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
   Serial.print(".");
 }
// Print local IP address and start web server
 Serial.println("");
  Serial.println("WiFi connected-->");
  Serial.println("Copy this IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   
  if (client) {                             // If a new client connects,
    Serial.println("new client connected");          
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected())
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             
        Serial.write(c);                    
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            //LED _1 and its change of state
            if (header.indexOf("GET /LED1/on") >= 0) {
              Serial.println("LED1 on");
              LED1State = "on";
              digitalWrite(LED1, HIGH);
            } else if (header.indexOf("GET /LED1/off") >= 0) {
              Serial.println("LED1 off");
              LED1State = "off";
              digitalWrite(LED1, LOW);
            } 
            //LED_ 2 and its change of state
            if (header.indexOf("GET /LED2/on") >= 0) {
              Serial.println("LED2 on");
              LED2State = "on";
              digitalWrite(LED2, HIGH);
            } else if (header.indexOf("GET /LED2/off") >= 0) {
              Serial.println("LED2 off");
              LED2State = "off";
              digitalWrite(LED2, LOW);
            } 
            //LED _3 and its change of state
            if (header.indexOf("GET /LED3/on") >= 0) {
              Serial.println("LED3 on");
              LED3State = "on";
              digitalWrite(LED3, HIGH);
            } else if (header.indexOf("GET /LED3/off") >= 0) {
              Serial.println("LED3 off");
              LED3State = "off";
              digitalWrite(LED3, LOW);
            } 
            //LED_4 and its change of state
            if (header.indexOf("GET /LED4/on") >= 0) {
              Serial.println("LED4 on");
              LED4State = "on";
              digitalWrite(LED4, HIGH);
            } else if (header.indexOf("GET /LED4/off") >= 0) {
              Serial.println("LED4 off");
              LED4State = "off";
              digitalWrite(LED4, LOW);
            } 

            // Display the HTML web page
            //displaying what is above the name below
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<style>html { font-family: Cairo; display: inline; margin: 0px auto; text-align: center; background-color: #ccffb3;}");
            client.println(".button { background-color: #006699; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 35px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #555555;}</style></head>");

            //to print the name
            client.println("<svg width=\"300\" height=\"80\"><text fill=\"#00bfbf\" font-family=\"serif\" font-size=\"24\" id=\"svg_1\" stroke=\"#000000\" text-anchor=\"middle\" transform=\"matrix(1.35388 0 0 1.42308 -6.66283 -8.67308)\" x=\"100.5\" xml:space=\"preserve\" y=\"41.5\">Anshumaan Kumar</text></svg>");

            // Web Page Heading
            client.println("<body><h1>Welcome Webserver version 1.0</h1>");
            client.println("<p>Output_1 is: " + LED1State + "</p>");
            // If the LED1State is off, it displays the ON button       
            if (LED1State=="off") {
              client.println("<p><a href=\"/LED1/on\"><button class=\"button\">ON</button></a></p>");
             // client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ffffff\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            } else {
              client.println("<p><a href=\"/LED1/off\"><button class=\"button button2\">OFF</button></a></p>");
              //client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ff7f00\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            }     
             client.println("<p>Output_2 is: " + LED2State + "</p>");
            // If the LED1State is off, it displays the ON button       
            if (LED2State=="off") {
              client.println("<p><a href=\"/LED2/on\"><button class=\"button\">ON</button></a></p>");
             // client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ffffff\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            } else {
              client.println("<p><a href=\"/LED2/off\"><button class=\"button button2\">OFF</button></a></p>");
              //client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ff7f00\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            }     
             client.println("<p>Output_3 is: " + LED3State + "</p>");
            // If the LED1State is off, it displays the ON button       
            if (LED3State=="off") {
              client.println("<p><a href=\"/LED3/on\"><button class=\"button\">ON</button></a></p>");
             // client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ffffff\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            } else {
              client.println("<p><a href=\"/LED3/off\"><button class=\"button button2\">OFF</button></a></p>");
              //client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ff7f00\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            }     
             client.println("<p>Output_4 is: " + LED4State + "</p>");
            // If the LED1State is off, it displays the ON button       
            if (LED4State=="off") {
              client.println("<p><a href=\"/LED4/on\"><button class=\"button\">ON</button></a></p>");
             // client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ffffff\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            } else {
              client.println("<p><a href=\"/LED4/off\"><button class=\"button button2\">OFF</button></a></p>");
              //client.println("<svg width=\"500\" height=\"300\"><ellipse cx=\"258.5\" cy=\"125.5\" fill=\"#ff7f00\" rx=\"47\" ry=\"52\" stroke=\"#ffffaa\" stroke-width=\"5\"/><rect fill=\"#cccccc\" height=\"40\" stroke=\"#ffffaa\" stroke-width=\"5\" transform=\"rotate(-0.485546 261 187.5)\" width=\"39\" x=\"241.5\" y=\"167.5\"/></svg>");

            }     
            client.println("</body></html>");
            client.println();
            break;
          } else { 
            currentLine = "";
          }
        } else if (c != '\r') {  
          currentLine += c;      
        }
      }
    }
    header = "";
    client.stop();
    Serial.println("Client disconnected");
    Serial.println("");
  }

что я делаю не так.

...