- 12V Akku mit 280 Ah bauen         
Ergebnis 1 bis 10 von 97

Thema: esp8266, Arduino IDE: neuer Versuch: warum wird website sofort beendet?

Hybrid-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    18.03.2018
    Beiträge
    2.650
    nimm mal den Quelltext:

    Code:
    #include <ESP8266WiFi.h>
    //#include <ESP8266WebServer.h>
    //#include <FS.h> // muss vor <detail\RequestHandlersImpl.h> stehen
    //#include <detail\RequestHandlersImpl.h>
    
    
    String ssid = "...";
    String password = "...";
    
    
    //ESP8266WebServer webserver(80); // Webserver-Instanz f�r Port 80 erstellen
    WiFiServer server(80);
    
    
    
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println();
    
    
      Serial.println("Connecting to " + ssid);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid.c_str(), password.c_str());
      WiFi.config(IPAddress(xxx,xxx,xxx,xx), IPAddress(xxx,xxx,xxx,xxx), IPAddress(xxx,xxx,xxx,xxx), IPAddress(xxx,xxx,xxx,xxx));
      
      //WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED)
      {
        delay(500);
        Serial.print(".");
      }
      Serial.println(" connected");
    
    
      server.begin();
      Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
    }
    
    
    
    
    // prepare a web page to be send to a client (web browser)
    String prepareHtmlPage()
    {
      String htmlPage =
         String("HTTP/1.1 200 OK\r\n") +
                "Content-Length: 44\r\n"+
                "Content-Type: text/html\r\n" +
                "Connection: close\r\n" +  // the connection will be closed after completion of the response
                "Refresh: 5\r\n" +  // refresh the page automatically every 5 sec
                "\r\n" +
                "<!DOCTYPE html>" +
                "<html>" +
                "Analog input:  " + String(analogRead(A0)) +
                "</html>" +
                "\r\n";
      return htmlPage;
    }
    
    
    void loop()
    {
      WiFiClient client = server.available();
      // wait for a client (web browser) to connect
      if (client)
      {
        Serial.println("\n[Client connected]");
        while (client.connected())
        {
          // read line by line what the client (web browser) is requesting
          if (client.available())
          {
            String line = client.readStringUntil('\r');
    
    
            Serial.print(line);
            // wait for end of client's request, that is marked with an empty line
            if (line.length() == 1 && line[0] == '\n')
            {
              client.println(prepareHtmlPage());
              delay(100); // give the web browser time to receive the data
              // close the connection:
              client.stop();
              Serial.println("[Client disonnected]");
              break;
            }
          }
        }
      }
    }

  2. #2
    HaWe
    Gast
    ja, funktioniert jetzt mit 2.5.2 (habe WiFi.config() momentan auskommentiert)
    was ist passiert?

  3. #3
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    18.03.2018
    Beiträge
    2.650
    weiter oben lesen: https://www.roboternetz.de/community...l=1#post655595

    - - - Aktualisiert - - -

    @hawe
    habe Deinen ersten Quelltext, nach den neuen Erkenntnissen, nochmals modifiziert:

    Code:
    bool authorized=false;
    
    
    void handleNotAuthorized() {
      String readString = "";
      char   strinput[MAXLEN], strupwd[TOKLEN], struname[TOKLEN] ;
      
      WiFiClient client = wifiserver.available();
    
    
      //---------------------------------------
      // debug
      // authorized=true;
    
    
      strcpy(strinput, "");
      strcpy(strupwd, "");
      strcpy(struname, "");
    
    
      while ( client.connected() ) {
        if (authorized) return;
    
    
        if ( client.available() ) {
          char c = client.read();
    
    
          //read char by request
          readString = "";
          while ( (readString.length() < TOKLEN) && (c != '\n') ) {
              readString += c;
              c = client.read();
          }
    
    
          readString.toCharArray(strinput, MAXLEN);
         // cstringarg(strinput, "uname", struname); // uname
         // cstringarg(strinput, "upwd", strupwd);  // upwd
     
          if(strstr(website_upwd,strupwd)!=NULL & strstr(website_uname,struname)!=NULL)
          {
            authorized = true;
            readString = "";
            return;           
            // return;  // <<< selbes Ergebnis, egal ob kommentiert oder auskommentiert
          }    
          
          //if HTTP request has ended
          if (c == '\n') {
             client.flush();
    
    
            //now output html data header
    
    
            String script="";
            script += ("<!DOCTYPE html> \n");
            script += ("<html> \n");
            
            script += ("<head> \n");
    
    
            // utf-8 für "°" Zeichen
            script +=  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" ;
            script +=  "<title>" ;        
            script +=  "</title> \n" ;
            script +=  "</head> \n" ;
            script +=  "<body> \n" ;
            
            script += ("<h2><p style=\"color:rgb(255,0,191);\"> log in to proceed: </p> </h2> \n");
    
    
            script += ("<FORM ACTION='/' method=GET > \n");
            script += ("<h2>user name:  <INPUT TYPE=text NAME='uname' VALUE=''  MAXLENGTH='50'> </h2> \n");
            script += ("<h2>password :  <INPUT TYPE=PASSWORD NAME='upwd' VALUE='' MAXLENGTH='50'> </h2> \n");
    
    
            script += ("<h2><INPUT TYPE=SUBMIT></h2> \n");
    
    
            script += ("</FORM> \n");
            script += ("<BR> \n");
            script += ("</body> \n");
            script += ("</html> \n");
    
    
            //----------------------------------
    
    
            String script1 = "";
            script1 += ("HTTP/1.1 200 OK \n");
            script1 += ("Content-Type: text/html \n");
            script1 += ("Content-Length: ") + script.length() + ("\n");
            script1 += ("\n");  //  do not forget this one //????
    
    
            script = script1+script;
    
    
            client.print(script);
            delay(100);
    
    
            //stopping client
            client.stop();
            //clearing string for next read
            //readString = "";
          }
        }
        delay(1);
      }
    }

  4. #4
    HaWe
    Gast
    äh, sorry, vor
    // close the connection:
    client.stop();

    der "echte" ungekürzte Code ist dieser hier:

    Code:
    
    void handleNotAuthorized() {
      String readString = "";
      char   strinput[MAXLEN], strupwd[TOKLEN], struname[TOKLEN] ;
      
      WiFiClient client = wifiserver.available();
    
      strcpy(strinput, "");
      strcpy(strupwd, "");
      strcpy(struname, "");
    
    
      while ( client.connected() ) {
        if (authorized) return;
    
    
        if ( client.available() ) {
          char c = client.read();
    
    
          //read char by request
          readString = "";
          while ( (readString.length() < TOKLEN) && (c != '\n') ) {
              readString += c;
              c = client.read();
          }
     
          if(strstr(website_upwd,strupwd)!=NULL & strstr(website_uname,struname)!=NULL)
          
          readString.toCharArray(strinput, MAXLEN);
          // cstringarg( char* haystack, char* vname, char* sarg )
          // haystack pattern: &varname=1234abc,  delimiters &, \n, \0, SPACE, EOF
          cstringarg(strinput, "uname", struname);  // uname
          cstringarg(strinput, "upwd", strupwd);   // upwd
          
          // debug
          Serial.print("strupwd     >>>"); Serial.print(strupwd); Serial.println("<<<");
          Serial.print("website_upwd>>>"); Serial.print(website_upwd); Serial.println("<<<");
          Serial.print("readString>>>");Serial.println(readString);
    
          if ( (strlen(strupwd)==strlen(website_upwd)) && (strcmp(website_upwd, strupwd)==0)
              && (strlen(struname)==strlen(website_uname)) && (strcmp(website_uname, struname)==0) 
             )         
          {
            authorized = true;
            readString = "";
            return;           
          }    
          
          //if HTTP request has ended
          if (c == '\n') {
             client.flush();
    
            //now output html data header
            String script="";
            script += ("<!DOCTYPE html> \n");
            script += ("<html> \n");
            
            script += ("<head> \n");
    
            // utf-8 für "°" Zeichen
            script +=  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" ;
            script +=  "<title>" ;        
            script +=  "</title> \n" ;
            script +=  "</head> \n" ;
            script +=  "<body> \n" ;
            
            script += ("<h2><p style=\"color:rgb(255,0,191);\"> log in to proceed: </p> </h2> \n");
    
            script += ("<FORM ACTION='/' method=GET > \n");
            script += ("<h2>user name:  <INPUT TYPE=text NAME='uname' VALUE=''  MAXLENGTH='50'> </h2> \n");
            script += ("<h2>password :  <INPUT TYPE=PASSWORD NAME='upwd' VALUE='' MAXLENGTH='50'> </h2> \n");
    
            script += ("<h2><INPUT TYPE=SUBMIT></h2> \n");
    
            script += ("</FORM> \n");
            script += ("<BR> \n");
            script += ("</body> \n");
            script += ("</html> \n");
    
            //----------------------------------
    
            String script1 = "";
            script1 += "HTTP/1.1 200 OK \n";
            script1 += "Content-Type: text/html \n";
            script1 += "Content-Length: " + (String)script.length() + "\n";
            script1 += "\n";  //  do not forget this one //????
      
            script = script1+script;
    
            client.print(script);
            delay(100);
    
            //stopping client        
            /*
            while(client.connected()) { 
              delay(10);    
            }   
            */      
            client.stop();
          }
        }
        delay(1);
      }
    }

  5. #5
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    18.03.2018
    Beiträge
    2.650
    Warum so: (String)script.length()

    bei mir funktionierte das auch ohne das "(String)"

    ???

    PS: gib mal:
    "(String)" bei google ein, mit Gänsefüßchen oder ohne!

  6. #6
    HaWe
    Gast


    bei mir ging es zunächst nicht, aber es stört jetzt auch nicht weiter

  7. #7
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    18.03.2018
    Beiträge
    2.650
    Soll das (String) extra in String konvertieren?

Ähnliche Themen

  1. arduino ide mit esp8266
    Von NotEvil im Forum NodeMCU-Board und ESP8266, ESP32-Serie
    Antworten: 4
    Letzter Beitrag: 12.01.2017, 14:17
  2. Raspberry Pi 3: Neuer Bastelrechner ist ab sofort verfügbar
    Von Roboternetz-News im Forum Neuigkeiten / Technik-News / Nachrichten / Aktuelles
    Antworten: 4
    Letzter Beitrag: 29.02.2016, 20:56
  3. [ERLEDIGT] Google wird am Lenovo-Tablet ohne Zutun beendet
    Von oberallgeier im Forum Offtopic und Community Tratsch
    Antworten: 5
    Letzter Beitrag: 24.11.2015, 08:02
  4. Funktion wird nicht richtig beendet
    Von kowolfgang im Forum C - Programmierung (GCC u.a.)
    Antworten: 16
    Letzter Beitrag: 31.12.2008, 16:37
  5. schieberegister neuer versuch...
    Von Bluesmash im Forum Basic-Programmierung (Bascom-Compiler)
    Antworten: 12
    Letzter Beitrag: 11.07.2005, 21:10

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

fchao-Sinus-Wechselrichter AliExpress