Skip to content

Esp8266 Wifi connection often disconnects (bcn_timout) #5083

Closed

Description

@stefanodelbosco

Platform

  • Hardware: NodeMCU 1.0 (ESP12E-Module)
  • Core Version: latest git 2.4.2 && 2.2.1
  • Development Env: Arduino IDE (last git) && Platformio 2.2.1
  • Operating System: Ubuntu

Settings in IDE

  • Module: Nodemcu
  • Flash Size: 4MB
  • CPU Frequency: 80Mhz
  • Upload Using: OTA && SERIAL
  • Upload Speed: [115200 (Serial)]

Problem Description

I looked for a solution between the various issues but none worked right now.
I have updated sdk to the latest version with git
The connection to the WiFi remains active for a time ranging from 30 seconds to 6 hours.
The module disconnects and reconnects by the connectDeviceToWiFi () function.

I tried to lower the time beecon in the router. Now it's every 30 ms (default 100ms).

I've been trying for three days to find a solution but the situation is not changing.
I tried with two different modules near the router and both give problems.

The RSSI signal is always between -70 and -78. I did a -68 signal tests but the problem persists.

I hope someone can help me.

MCVE Sketch

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266CustomTools.h>
#include "ota.h"
#include <ArduinoOTA.h>

ESP8266WebServer server(80);
ESP8266CustomTools device("SensorTest", D7, "sensor");

String IP_SERVER = "192.168.1.5";


void setup() {
  Serial.begin(9600);
  pinMode(D7, INPUT);
  device.connectDeviceToWiFi();
  device.syncCurrentTimestamp();
  device.generateOTPSecret();
  server.on("/info", handleInfo);
  server.begin();
  Serial.println(ESP.getCoreVersion());
  Serial.println(ESP.getSdkVersion());

  ota_setup("Esp8266", "PASSWORD");
}

void loop() {
    server.handleClient();
    //Check if connection is alive
    device.connectDeviceToWiFi();
    //Update IP/MAC
    device.checkAndUpdateConnectionInfo();
    if(digitalRead(D7) == 0){
      Serial.println("Change state");
    }
    device.checkAndUpdateTimestampOTP();
    //OTA Check
    ArduinoOTA.handle();
    delay(10);
}

ESP8266CustomTools::connectDeviceToWiFi()

void ESP8266CustomTools::connectDeviceToWiFi(){
  while(WiFi.status() != WL_CONNECTED){
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(200);
    WiFi.begin(ssid, password);
    long timewait = 0;
    long timeout = 10000;
    while(WiFi.status() != WL_CONNECTED && timewait < timeout){
       delay(1000);
       timewait = timewait + 1000;
    }
    if(WiFi.status() == WL_CONNECTED){
      String IP = WiFi.localIP().toString();
      String MACADDRESS = WiFi.macAddress();
      MACADDRESS.toLowerCase();
      Serial.println(IP);
      HTTPClient http;
      http.begin("http://" + IP_SERVER + "/api/device/connected_wifi_device?ip=" + IP + "&mac=" + MACADDRESS + "&type=" + TYPE);
      int httpCode = http.GET();
      String payload = http.getString();
      if(httpCode == 200){
        Serial.println("Message sent");
        last_server_update = millis();
      }
      http.end();
    }
  }
}

Debug Messages

SDK:2.2.1(cfd48f3)/Core:2.4.2/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1-13-g163bb82)/BearSSL:6d1cefc
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt 

connected with dlink-XXXX, channel 10
dhcp client start...
ip:192.168.1.4,mask:255.255.255.0,gw:192.168.1.1
192.168.1.4
    SyncIpServer httpCode: 200
    SyncTimestamp httpCode: 200
    SyncOTP httpCode: 200
    COREVersion: 2_4_2
    SDKVerison: 2.2.1(cfd48f3)

bcn_timout,ap_probe_send_start
ap_probe_send over, rest wifi status to disassoc
state: 5 -> 0 (1)
rm 0
pm close 7
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt 

connected with dlink-XXXX, channel 10
dhcp client start...
ip:192.168.1.4,mask:255.255.255.0,gw:192.168.1.1
192.168.1.4
....

Activity

  1. d-a-v commented on Aug 27, 2018

    @d-a-v
    Collaborator
    • try WiFi.mode(WIFI_NONE_SLEEP) (more power required)
    • try pingAlive (unknown consequence on power)
    • reading Esp8266 IP Address not reachable after a while #2330 is a punishment, I wont tell you to do so
    • we still don't have it but there's new nonos-sdk-v3 espressif API wifi_set_listen_interval()
      (parameter: interval - listen interval of maximum sleep level for modem sleep and light sleep. Unit: one beacon interval of AP. Range: [1, 10])
  2. added
    waiting for feedback
    on Aug 27, 2018
  3. stefanodelbosco commented on Aug 27, 2018

    @stefanodelbosco
    Author

    try WiFi.mode(WIFI_NONE_SLEEP) (more power required)

    I tried only WiFi.setSleepMode(WIFI_NONE_SLEEP)
    It's been about 20 minutes and there were no disconnections. I think the problem was sleep mode.
    I will wait a day of testing to see if the problem was actually that.

  4. stefanodelbosco commented on Aug 29, 2018

    @stefanodelbosco
    Author

    After two days of testing, after applying that command, I had no more disconnections.
    I think I found the problem and I would say that I can close this issue.
    In case I have problems again, I will reopen the issues.
    Thanks for the help @d-a-v

  5. PrateekGoyal18 commented on Jul 4, 2019

    @PrateekGoyal18

    After two days of testing, after applying that command, I had no more disconnections.
    I think I found the problem and I would say that I can close this issue.
    In case I have problems again, I will reopen the issues.
    Thanks for the help @d-a-v

    Did you mention this line "WiFi.setSleepMode(WIFI_NONE_SLEEP)" in setup ?

  6. stefanodelbosco commented on Jul 4, 2019

    @stefanodelbosco
    Author

    I think so.
    I will check this evening and I will let you know.

  7. stefanodelbosco commented on Jul 4, 2019

    @stefanodelbosco
    Author

    My code:

    void setup() {
    Serial.begin(9600);
    WiFi.setSleepMode(WIFI_NONE_SLEEP);
    [.....]
    }

  8. PrateekGoyal18 commented on Jul 5, 2019

    @PrateekGoyal18
  9. tablatronix commented on Aug 22, 2019

    @tablatronix
    Contributor

    I am having this issue suddenly , will not connect at all , if it does it disconnects quickly after
    What is the deal with this ? sleep mode none made no difference

    bcn_timout,ap_probe_send_start
    ap_probe_send over, rest wifi status to disassoc
    state: 5 -> 0 (1)
    rm 0
    
  10. tablatronix commented on Aug 24, 2019

    @tablatronix
    Contributor

    It seems that if I call analogread too often my wifi drops out, I am not sure what mechanism is going on , I am trying to see how the adc works and how long the reads take and see if it is a wifi servicing issue but not enough for wdt.. does anyone know how to ensure wifi has time to service? Is a yeild() enough?

  11. e346615 commented on Oct 17, 2019

    @e346615

    I'm still stuck on this same problem - the WIFI keeps dropping and I have to reconnect. I have maybe one or two seconds before it drops and has to reconnect. I have the board version 2.5.1 and an ESP-12S MCU I'm trying to use for simple UDP broadcasts. Does anybody have this WIFI dropping problem fixed yet?

  12. devyte commented on Oct 17, 2019

    @devyte
    Collaborator

    @e346615 you need to test with latest git before asking that.

  13. e346615 commented on Oct 19, 2019

    @e346615

    Yes, I did, but I read somewhere that only version 2.5.1 worked. I tried both, but neither are working for me.

  14. mayor89 commented on Oct 21, 2019

    @mayor89

    My problem was solved with this command WiFi.setSleepMode(WIFI_NONE_SLEEP); plus WiFi.mode(WIFI_STA);. After this it is stable. Thank you!

  15. pryadav333 commented on Nov 10, 2019

    @pryadav333

    it seems that the NodeMCU/ESP 8266 is not reliable for IoT projects where we need continuous wifi connectivity. It goes most of time in sleep mode and doesn't respond to the commands. Ideal all time wifi active should be its state. All wakeup commands fails most of time to wake up the nodemcu/esp8266

  16. d-a-v commented on Nov 10, 2019

    @d-a-v
    Collaborator

    @pryadav333 This thread is closed. Please open a new issue if you still have this problem with version 2.6.0 of this core.

  17. Tejas-MD commented on May 24, 2021

    @Tejas-MD

    I have this issue on latest release of core: 3.0 . I don't understand why the issue is closed. Please help out with a direction to proceed? I tried WiFi.setSleepMode(WIFI_NONE_SLEEP) , not working.

  18. Pablo2048 commented on May 25, 2021

    @Pablo2048

    There is one rule of thumb in such situation - do a full chip erase before uploading your code. I guess that the problem is by switching SDK version using new core 3.0.0 vs the old one so rfcalib/config area is corrupted...

  19. jeroenst commented on Jul 11, 2021

    @jeroenst

    it seems that the NodeMCU/ESP 8266 is not reliable for IoT projects where we need continuous wifi connectivity. It goes most of time in sleep mode and doesn't respond to the commands. Ideal all time wifi active should be its state. All wakeup commands fails most of time to wake up the nodemcu/esp8266

    FYI: I have esp8266/nodemcu devices with uptimes over 60 days and continuous wifi without any problem.

  20. Tejas-MD commented on Jul 11, 2021

    @Tejas-MD

    For me, it works fine after WiFi.setSleepMode(WIFI_NONE_SLEEP) --> Chip erase ---> Flash with latest firmware.

    Very grateful to @Pablo2048

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting for feedback

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions