Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP8266 as station keeps losing connection to router then re-connect shortly after #356

Open
zhiyong-ft opened this issue Feb 15, 2022 · 0 comments

Comments

@zhiyong-ft
Copy link

Hi,

We use ESP8266 as WiFi station with MBed. It works mostly fine but upon careful inspection, I noticed that ESP8266 will lose connection frequently. If we run constant connection like WebSocket from MBed to a server, we can see from server side that those WebSocket connections usually last 10 minutes, sometimes longer, about 20 ~ 30 minutes. If we repeatedly make HTTP requests via ESP8266, sometimes we see following error:

// MBed's way saying it thinks ESP8266 has lost connection
NSAPI_ERROR_CONNECTION_LOST     = -3016,     /*!< connection lost */

In the Arduino version, some state that disabling sleep mode can fix the problem.
esp8266/Arduino#5083
But disabling sleep mode in MBed doesn't seem to help:

nsapi_error_t ESP8266::connect(const char *ap, const char *passPhrase)
{
    nsapi_error_t ret = NSAPI_ERROR_OK;

    _smutex.lock();
    set_timeout(ESP8266_CONNECT_TIMEOUT);

    bool res = _parser.send("AT+CWJAP_CUR=\"%s\",\"%s\"", ap, passPhrase);
    if (!res || !_parser.recv("OK\n")) {
        if (_fail) {
            if (_connect_error == 1) {
                ret = NSAPI_ERROR_CONNECTION_TIMEOUT;
            } else if (_connect_error == 2) {
                ret = NSAPI_ERROR_AUTH_FAILURE;
            } else if (_connect_error == 3) {
                ret = NSAPI_ERROR_NO_SSID;
            } else {
                ret = NSAPI_ERROR_NO_CONNECTION;
            }
            _fail = false;
            _connect_error = 0;
        }
    }
    // Added lines to ensure sleep mode is turned off upon connect()
    res = _parser.send("AT+SLEEP=0");
    if (!res || !_parser.recv("OK\n")) {
        printf("%d: ESP8266 set sleep mode failed\r\n", time(nullptr));
    } else {
        printf("%d: ESP8266 sleep mode set to 0\r\n", time(nullptr));
    }

    set_timeout();
    _smutex.unlock();

    return ret;
}

Disabling sleep mode will increase system power consumption by 30mA, but ESP8266 still keeps losing connection.

Can anybody offer some pointers on how to customize firmware to fix the problem? We tried stock firmware 1.7.4 and 1.7.3, problem exists in both version.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant