Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By MajorMadness
#32200 Hi,
I've a problem with my current sketch and connection to my local Network. On Boot I load config from EEPROM and connect to my Router (AVM FritzBox):
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
...
ESP8266WebServer server ( 80 );
...
void setup() {
  setupWifi();

  server.on("/", handleRoot);
  server.on ( "/style.css", []() {
    Serial.println("Page: Style.css");
    server.send ( 200, "text/css", PAGE_Style_css );
  } );
  server.on("/time", htmlTime);
  server.on("/temp", htmlTemp);
  server.begin();
  Serial.println("HTTP server started");
}
void setupWifi() {
    Serial.println ( "Setup Wifi" );
  if (EspLoadSettings()) {
    // Wait for connection
    byte tries = 22;
    WiFi.scanNetworks();
    WiFi.mode(WIFI_AP_STA);
    WiFi.begin ( SSID , PASS );
    Serial.println ( "Connecting to router" );
    Serial.println ( SSID );
    Serial.println ( PASS );
    while (--tries && WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print ( "." );
    }

    delay(500);
    Serial.println ( "" );
    Serial.print ( "Connected to " );
    Serial.println ( genSettings.rSSID );
    Serial.print ( "IP address: " );
    Serial.println ( WiFi.localIP() );
  }
}
void loop()
{
  server.handleClient();
    if(WiFi.status()==WL_IDLE_STATUS){
      Serial.println("WL_IDLE_STATUS");
    }else if(WiFi.status()==WL_NO_SSID_AVAIL){
      Serial.println("WL_NO_SSID_AVAIL");
    }else if(WiFi.status()==WL_SCAN_COMPLETED){
      Serial.println("WL_SCAN_COMPLETED");
    }else if(WiFi.status()==WL_CONNECTED){
      Serial.println("WL_CONNECTED");
    }else if(WiFi.status()==WL_CONNECT_FAILED){
      Serial.println("WL_CONNECT_FAILED");
    }else if(WiFi.status()==WL_CONNECTION_LOST){
      Serial.println("WL_CONNECTION_LOST");
    }else if(WiFi.status()==WL_DISCONNECTED){
      Serial.println("WL_DISCONNECTED");
    }
    if (WiFi.status() == WL_DISCONNECTED ) {
???????????????????????????????????????????ß
  }

}

In the beginning everything works fine. Serial.prinln shows me "WL_CONNECTED" but after a while (3-10 Minutes) I get "WL_CONNECT_FAILED" followed by "WL_DISCONNECTED". So if this happens I want to reconnect to my router and this fails.

So my question is: How to deal with that and how to be able to stay connected? if I use WiFiServer instead of ESP8266WebServer it stays connected over 1 hour, so my router isn't the problem but I can't use server.on() or other functions I'm useing so only way is to rewrite everything which can't be the solution I think...

Greetings Moritz
User avatar
By eduperez
#32207 Just my two cents:

* Do not connect on setup(), do it inside loop() instead.
* On each iteration, check whether the connection is up, and try to connect again if it is down.
* If you fail to bring the connection up, just delay the execution and try again.
User avatar
By MajorMadness
#32292 Hi,
thanks but it doesn't realy works:
calling setupWifi(); or WiFi.begin() in loop makes the ESP crash. ( Exeption(0) )
Also 2nd and 3rd doesn't work cause it crashes.

But because of this I made a lil test skatch:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server ( 80 );
uint32_t m = 0;

void setup ( void ) {
  Serial.begin ( 115200 );
  WiFi.begin ( "ssid", "password" );
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
  }
  server.on("/", []() {
    server.send(200, "text/plain", "this works");
  });
  server.begin();
}

void loop ( void ) {
  server.handleClient();
  if (millis() > m + 1000) {
    m = millis();
    if (WiFi.status() == WL_IDLE_STATUS) {
      Serial.println("WL_IDLE_STATUS");
    } else if (WiFi.status() == WL_NO_SSID_AVAIL) {
      Serial.println("WL_NO_SSID_AVAIL");
    } else if (WiFi.status() == WL_SCAN_COMPLETED) {
      Serial.println("WL_SCAN_COMPLETED");
    } else if (WiFi.status() == WL_CONNECTED) {
      Serial.println("WL_CONNECTED");
      Serial.print ( "IP address: " );
      Serial.println ( WiFi.localIP() );
    } else if (WiFi.status() == WL_CONNECT_FAILED) {
      Serial.println("WL_CONNECT_FAILED");
    } else if (WiFi.status() == WL_CONNECTION_LOST) {
      Serial.println("WL_CONNECTION_LOST");
    } else if (WiFi.status() == WL_DISCONNECTED) {
      Serial.println("WL_DISCONNECTED");
    }
  }
}

This gives me a dicconect every once in a while but reconnects directly again. I have the feeling it's really depens on if I use
Code: Select all    WiFi.mode(WIFI_AP_STA);
    WiFi.begin ( SSID , PASS );
    WiFi.softAP("ESP");

or some other mode like WIFI_STA or WIFI_AP.
Setting it up as AP, trying to scan for other Networks didn't work and if I use:
Code: Select all    WiFi.softAP("ESP");
  WiFi.mode(WIFI_AP_STA);
  int n = WiFi.scanNetworks();
I loose connection again after a while to my Router (Scan Network, select Router, save config, reboot and connect to this selected router and AP).

Is it possible only one thing is stable: AP or STA at the time?

Edit:
I looked where which mode is set and think it is strange:
ESP8266WiFi.cpp Line 70:
Code: Select all    if(_useApMode) {
        // turn on AP+STA mode
        mode(WIFI_AP_STA);
    } else {
        // turn on STA mode
        mode(WIFI_STA);
    }

Ok, because ESP8266WiFiClass::ESP8266WiFiClass():_useApMode(false) default on begin, default mode of WiFi is WIFI_STA. When I call WiFi.softAP() it get's switched to _useApMode = true; and because _useClientMode is also true (was set in begin) mode is WIFI_AP_STA.

Next is scanNetworks(): Also sets mode to WIFI_AP_STA if it was only access point before, but this crashed and gives me exeptions while trying to scan in AP mode.