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

[D][WiFiClient.cpp:452] connected(): Disconnected: RES: 0, ERR: 128 problem? #1921

Closed
nae9 opened this issue Oct 3, 2018 · 78 comments
Closed
Labels
Area: BT&Wifi BT & Wifi related issues

Comments

@nae9
Copy link

nae9 commented Oct 3, 2018

Hardware:

Board: ttgo (and other)
Core Installation/update date: 10/3/2018
IDE name: Arduino IDE 1.8.7
Flash Frequency: 80Mhz
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Windows 10

Description:

I try to install new release of arduino-esp and faced with some picularities in my sketch behavior.
win 10, arduino 1.8.7 , esptool 2.3.1, mkspiffs 0.2.3, xtensa 1.22.0-80-g6c443a-5.2.0.
TTGO board.
Early all work fine - i send HTTP packets from PC to ESP32 and recieve answer about 30 "fps". But after i install new version of IDF (download by arduino "https://dl.espressif.com/dl/package_esp32_index.json") then answer not sends or sends with long timeouts. It seemed that new wificlient.cpp work different in compare with old version.
For example i try to use standard arduino example like SimpleWiFiServer.ino and they also freeze answers...
I print IP adress of esp32 in mozilla firefox (or another program) and after about 3-5 sec i see in debug that HTTP recieved by ESP32 and answered, mozilla some time recive answer, but some time no. Early answer returned very fast.
After i see in debug "client disconnected" then after 100ms i see "new client"! but client.available() == 0 and program spin in while(client.connected()) cycle. Then are timeout event (?) after 5-10sec
[D][WiFiClient.cpp:452] connected(): Disconnected: RES: 0, ERR: 128
and client.connected == 0 - program moove later.
The analysis of debug output shows that client.stop() command not make client.connected() == 0 and in second cycle we catch in infinite while(client.connected()) loop.

What i do wrong? How i can do fast series of HTTP request/answer to esp32? Why in old version (06/2017) all was fine?

Sketch: (leave the backquotes for code formatting)

//Change the code below by your sketch
#include <WiFi.h>
const char* ssid     = "myssid";
const char* password = "mypass";
WiFiServer server(80);

void setup()
{
  Serial.begin(115200);
  pinMode(5, OUTPUT);      // set the LED pin mode
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

int value = 0;

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            // send a standard http response header
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");  // the connection will be closed after completion of the response
            //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
              client.print("analog input ");
              client.print("AI0");
              client.print(" is ");
              client.print(analogRead(A0));
              client.println("<br />");
            client.println("</html>");
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(5, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(5, LOW);                // GET /L turns the LED off
        }
      }
    }
    
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

Debug Messages:

ets Jun  8 2016 00:22:57
17:52:57.076 -> 
17:52:57.076 -> rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
17:52:57.076 -> configsip: 0, SPIWP:0xee
17:52:57.076 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
17:52:57.076 -> mode:DIO, clock div:1
17:52:57.076 -> load:0x3fff0018,len:4
17:52:57.116 -> load:0x3fff001c,len:952
17:52:57.116 -> load:0x40078000,len:6084
17:52:57.116 -> load:0x40080000,len:7936
17:52:57.116 -> entry 0x40080310

17:52:57.316 -> 
17:52:57.316 -> Connecting to nae
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 2 - STA_START
17:52:57.396 -> [D][WiFiGeneric.cpp:345] _eventCallback(): Event: 0 - WIFI_READY
.[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:345] _eventCallback(): Event: 7 - STA_GOT_IP
17:52:58.396 -> [D][WiFiGeneric.cpp:389] _eventCallback(): STA IP: 192.168.1.210, MASK: 255.255.255.0, GW: 192.168.1.1
17:52:58.396 -> .
17:52:58.396 -> WiFi connected.
17:52:58.396 -> IP address: 
17:52:58.396 -> 192.168.1.210
New Client.
17:53:51.764 -> GET / HTTP/1.1
17:53:51.764 -> Host: 192.168.1.210
17:53:51.764 -> User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
17:53:51.764 -> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
17:53:51.804 -> Accept-Language: en-US,en;q=0.5
17:53:51.804 -> Accept-Encoding: gzip, deflate
17:53:51.804 -> Connection: keep-alive
17:53:51.804 -> Upgrade-Insecure-Requests: 1
17:53:51.804 -> 
Client Disconnected.
17:53:51.884 -> New Client.
[D][WiFiClient.cpp:452] connected(): Disconnected: RES: 0, ERR: 128
Client Disconnected.

@nae9 nae9 changed the title WifiClient.cpp problem? [D][WiFiClient.cpp:452] connected(): Disconnected: RES: 0, ERR: 128 problem? Oct 11, 2018
@nae9
Copy link
Author

nae9 commented Oct 23, 2018

I check old version work setup:
Early i use installation in UserDocuments/Arduino/Hardware/espressif/esp32/...
and i have bakup of this folder "https://drive.google.com/file/d/1-4Ab6572dV7KypVEbPMz9a8yU7l_Db8c/view?usp=sharing". (this is old version of esp32 ambient with old wifi libs)

Copy this into UserDocuments/Arduino/hardware/espressif/esp32/
If you use additional libs then put it in UserDocuments/Arduino/libraries/
Your skethes put in UserDocuments/Arduino/
Install Arduino 1.8.7 (or other)
Clear c:\Users\UserName\AppData\Local\Arduino15
Set in arduino IDE props sketch folder as "UserDocuments/Arduino/" and reload IDE.

@raico-de
Copy link

It is not a solution to use a lib from last year,
i have still the same problem:
[D][WiFiClient.cpp:453] connected(): Disconnected: RES: 0, ERR: 128
[D][WiFiClient.cpp:453] connected(): Disconnected: RES: 0, ERR: 128

other problem is the function

size_t WiFiClient::write_P(PGM_P buf, size_t size)
{
return write(buf, size);
}
if you use write there is a
type conflict:

invalid arguments '
Candidates are:
? write(?)
unsigned int write(const char *)
? write(const ? *, ?)
? write(const char *, ?)
unsigned int write(unsigned char)
unsigned int write(const unsigned char *, unsigned int)
unsigned int write(Stream &)
'

@GeorgeFlorian
Copy link

This is still an issue. I've downloaded the latest Arduino Core for ESP32, the newest libraries and I still get this error:
[D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128

@nae9
Copy link
Author

nae9 commented Feb 4, 2019

I find a method that fix this strange behavior... #2345

@GeorgeFlorian
Copy link

I find a method that fix this strange behavior... #2345

I don't understand where or what.

@nae9
Copy link
Author

nae9 commented Feb 6, 2019

Click on #2345 tag and read!
Briefly:

void setup()
{
...
WiFi.begin(ssid, password);
WiFi.setSleep(false);// <--- this command disables WiFi energy save mode and eliminate connected(): Disconnected: RES: 0, ERR: 128 problem
...
}

@GeorgeFlorian
Copy link

Click on #2345 tag and read!
Briefly:

void setup()
{
...
WiFi.begin(ssid, password);
WiFi.setSleep(false);// <--- this command disables WiFi energy save mode and eliminate connected(): Disconnected: RES: 0, ERR: 128 problem
...
}

WiFi.setSleep(false); doesn't work for me. I also don't have a WiFi.begin(ssid, password);
Here is the code. I'm trying to implement Hieromon's AutoConnect and FSBrowser.
From what I know AutoConnect handles WiFi.begin(ssid, password); and maybe that is why it doesn't work.

/*
  FSWebServer - Example WebServer with FS backend for esp8266/esp32
  Copyright (c) 2015 Hristo Gochkov. All rights reserved.
  This file is part of the WebServer library for Arduino environment.
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  upload the contents of the data folder with MkSPIFFS Tool ("ESP32 Sketch Data Upload" in Tools menu in Arduino IDE)
  or you can upload the contents of a folder if you CD in that folder and run the following command:
  for file in `ls -A1`; do curl -F "file=@$PWD/$file" esp32fs.local/edit; done
  access the sample web page at http://esp32fs.local
  edit the page by going to http://esp32fs.local/edit
*/
#include <FS.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <AutoConnect.h>

#define FILESYSTEM SPIFFS
#define FORMAT_FILESYSTEM false
#define DBG_OUTPUT_PORT Serial

#define LED 2

#if FILESYSTEM == FFat
#include <FFat.h>
#endif
#if FILESYSTEM == SPIFFS
#include <SPIFFS.h>
#endif

const char* ssid = "wifi-ssid";
const char* password = "wifi-password";
const char* host = "esp32fs";

WebServer server(80);
AutoConnect portal(server);

//holds the current upload
File fsUploadFile;

bool startCP(IPAddress ip) {
    digitalWrite(LED, HIGH);
    DBG_OUTPUT_PORT.println("Captive Portal started ! IP: " + WiFi.localIP().toString());
    return true;
  }

//format bytes
String formatBytes(size_t bytes) {
  if (bytes < 1024) {
    return String(bytes) + "B";
  } else if (bytes < (1024 * 1024)) {
    return String(bytes / 1024.0) + "KB";
  } else if (bytes < (1024 * 1024 * 1024)) {
    return String(bytes / 1024.0 / 1024.0) + "MB";
  } else {
    return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
  }
}

String getContentType(String filename) {
  if (server.hasArg("download")) {
    return "application/octet-stream";
  } else if (filename.endsWith(".htm")) {
    return "text/html";
  } else if (filename.endsWith(".html")) {
    return "text/html";
  } else if (filename.endsWith(".css")) {
    return "text/css";
  } else if (filename.endsWith(".js")) {
    return "application/javascript";
  } else if (filename.endsWith(".png")) {
    return "image/png";
  } else if (filename.endsWith(".gif")) {
    return "image/gif";
  } else if (filename.endsWith(".jpg")) {
    return "image/jpeg";
  } else if (filename.endsWith(".ico")) {
    return "image/x-icon";
  } else if (filename.endsWith(".xml")) {
    return "text/xml";
  } else if (filename.endsWith(".pdf")) {
    return "application/x-pdf";
  } else if (filename.endsWith(".zip")) {
    return "application/x-zip";
  } else if (filename.endsWith(".gz")) {
    return "application/x-gzip";
  }
  return "text/plain";
}

bool exists(String path){
  bool yes = false;
  File file = FILESYSTEM.open(path, "r");
  if(!file.isDirectory()){
    yes = true;
  }
  file.close();
  return yes;
}

bool handleFileRead(String path) {
  DBG_OUTPUT_PORT.println("handleFileRead: " + path);
  if (path.endsWith("/")) {
    path += "index.htm";
  }
  String contentType = getContentType(path);
  String pathWithGz = path + ".gz";
  if (exists(pathWithGz) || exists(path)) {
    if (exists(pathWithGz)) {
      path += ".gz";
    }
    File file = FILESYSTEM.open(path, "r");
    server.streamFile(file, contentType);
    file.close();
    return true;
  }
  return false;
}

void handleFileUpload() {
  if (server.uri() != "/edit") {
    return;
  }
  HTTPUpload& upload = server.upload();
  if (upload.status == UPLOAD_FILE_START) {
    String filename = upload.filename;
    if (!filename.startsWith("/")) {
      filename = "/" + filename;
    }
    DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
    fsUploadFile = FILESYSTEM.open(filename, "w");
    filename = String();
  } else if (upload.status == UPLOAD_FILE_WRITE) {
    //DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
    if (fsUploadFile) {
      fsUploadFile.write(upload.buf, upload.currentSize);
    }
  } else if (upload.status == UPLOAD_FILE_END) {
    if (fsUploadFile) {
      fsUploadFile.close();
    }
    DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
  }
}

void handleFileDelete() {
  if (server.args() == 0) {
    return server.send(500, "text/plain", "BAD ARGS");
  }
  String path = server.arg(0);
  DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
  if (path == "/") {
    return server.send(500, "text/plain", "BAD PATH");
  }
  if (!exists(path)) {
    return server.send(404, "text/plain", "FileNotFound");
  }
  FILESYSTEM.remove(path);
  server.send(200, "text/plain", "");
  path = String();
}

void handleFileCreate() {
  if (server.args() == 0) {
    return server.send(500, "text/plain", "BAD ARGS");
  }
  String path = server.arg(0);
  DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
  if (path == "/") {
    return server.send(500, "text/plain", "BAD PATH");
  }
  if (exists(path)) {
    return server.send(500, "text/plain", "FILE EXISTS");
  }
  File file = FILESYSTEM.open(path, "w");
  if (file) {
    file.close();
  } else {
    return server.send(500, "text/plain", "CREATE FAILED");
  }
  server.send(200, "text/plain", "");
  path = String();
}

void handleFileList() {
  if (!server.hasArg("dir")) {
    server.send(500, "text/plain", "BAD ARGS");
    return;
  }

  String path = server.arg("dir");
  DBG_OUTPUT_PORT.println("handleFileList: " + path);


  File root = FILESYSTEM.open(path);
  path = String();

  String output = "[";
  if(root.isDirectory()){
      File file = root.openNextFile();
      while(file){
          if (output != "[") {
            output += ',';
          }
          output += "{\"type\":\"";
          output += (file.isDirectory()) ? "dir" : "file";
          output += "\",\"name\":\"";
          output += String(file.name()).substring(1);
          output += "\"}";
          file = root.openNextFile();
      }
  }
  output += "]";
  server.send(200, "text/json", output);
}

void setup(void) {
  DBG_OUTPUT_PORT.begin(115200);
  DBG_OUTPUT_PORT.print("\n");
  DBG_OUTPUT_PORT.setDebugOutput(true);
  
  if (FORMAT_FILESYSTEM) FILESYSTEM.format();
  
  FILESYSTEM.begin();
  {
      File root = FILESYSTEM.open("/");
      File file = root.openNextFile();
      while(file){
          String fileName = file.name();
          size_t fileSize = file.size();
          DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
          file = root.openNextFile();
      }
      DBG_OUTPUT_PORT.printf("\n");
  }


  //WIFI INIT
  //DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid);
//  if (String(WiFi.SSID()) != String(ssid)) {
//    WiFi.mode(WIFI_STA);
//    WiFi.begin(ssid, password);
//  }
//
//  while (WiFi.status() != WL_CONNECTED) {
//    delay(500);
//    DBG_OUTPUT_PORT.print(".");
//  }
  



  //SERVER INIT
  //list directory
  server.on("/list", HTTP_GET, handleFileList);
  //load editor
  server.on("/edit", HTTP_GET, []() {
    if (!handleFileRead("/edit.htm")) {
      server.send(404, "text/plain", "FileNotFound");
    }
  });
  //create file
  server.on("/edit", HTTP_PUT, handleFileCreate);
  //delete file
  server.on("/edit", HTTP_DELETE, handleFileDelete);
  //first callback is called after the request has ended with all parsed arguments
  //second callback handles file uploads at that location
  server.on("/edit", HTTP_POST, []() {
    server.send(200, "text/plain", "");
  }, handleFileUpload);

  //called when the url is not defined here
  //use it to load content from FILESYSTEM
  server.onNotFound([]() {
    if (!handleFileRead(server.uri())) {
      server.send(404, "text/plain", "FileNotFound");
    }
  });

  //get heap status, analog input value and all GPIO statuses in one json call
  server.on("/all", HTTP_GET, []() {
    String json = "{";
    json += "\"heap\":" + String(ESP.getFreeHeap());
    json += ", \"analog\":" + String(analogRead(A0));
    json += ", \"gpio\":" + String((uint32_t)(0));
    json += "}";
    server.send(200, "text/json", json);
    json = String();
  });
  
  

  pinMode(LED,OUTPUT);
  digitalWrite(LED, LOW);
  portal.onDetect(startCP);
  
  if (portal.begin()) {
    digitalWrite(LED, LOW);
  }
  WiFi.setSleep(false);

  DBG_OUTPUT_PORT.println("HTTP server started");
  
  DBG_OUTPUT_PORT.println("");
  DBG_OUTPUT_PORT.print("Connected! IP address: ");
  DBG_OUTPUT_PORT.println(WiFi.localIP());

  MDNS.begin(host);
  DBG_OUTPUT_PORT.print("Open http://");
  DBG_OUTPUT_PORT.print(host);
  DBG_OUTPUT_PORT.println(".local/edit to see the file browser");

}

void loop(void) {
  portal.handleClient();
}

@GeorgeFlorian
Copy link

WiFi.setSleep(false); doesn't help !

@CMGeorge
Copy link

CMGeorge commented Feb 10, 2019

WiFi.setSleep(false); doesn't help !

D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
[D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
[D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128
[D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128

192.168.1.175 200 document Other 8.8 KB 136 ms
bootstrap.min.js (failed) script (index) 0 B 110 ms
bootstrap-table.min.css (failed) stylesheet (index) 0 B 99 ms
jquery-3.3.1.min.js 200 script (index) 85.0 KB 4.16 s
bootstrap-table.min.js (failed) script (index) 0 B 114 ms
bootstrap.min.css (failed) stylesheet (index) 0 B 106 ms
logo.png (failed)   (index) 0 B 4.14 s
favicon.ico 200 x-icon Other 1.2 KB 39 ms

All files exists, and works if I open them individually.

To check this, add to SPIFFS in /js this JS files, and to /css the css files
Create a index html file and include them.

  <head>
    <meta name="viewport">
    <script src="/js/jquery-3.3.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/bootstrap-table.min.css">
    <script src="/js/bootstrap-table.min.js"></script>
    <link href="/css/bootstrap.min.css" rel="stylesheet" title="main">
  </head>

not all src's are loaded. Accessing /js/FILENAME, everything works correctly
tried adding file reference using:

server.serveStatic("/js", SPIFFS, "/js");
  server.serveStatic("/img", SPIFFS, "/img");
  server.serveStatic("/css", SPIFFS, "/css");

or

server.serveStatic("/js/bootstrap.min.js", SPIFFS, "/js/bootstrap.min.js");
  server.serveStatic("/js/bootstrap-table.min.js", SPIFFS, "/js/bootstrap-table.min.js");
  server.serveStatic("/js/jquery-3.3.1.min.js", SPIFFS, "/js/jquery-3.3.1.min.js");
  server.serveStatic("/css/bootstrap.min.css", SPIFFS, "/css/bootstrap.min.css");
  server.serveStatic("/css/bootstrap-table.min.css", SPIFFS, "/css/bootstrap-table.min.css");

index file is set for load as
server.serveStatic("/", SPIFFS, "/web/index.html");

@me21
Copy link

me21 commented Feb 10, 2019

Since these files are loading all at the same time, you may be hitting SPIFFS max open files limit. I saw similar behavior in my project. Firefox re-requests the files in that case, so it only slowed down the page loading for me.

Try using Firefox if you're not already using it and see if it makes any difference.

@CMGeorge
Copy link

Since these files are loading all at the same time, you may be hitting SPIFFS max open files limit. I saw similar behavior in my project. Firefox re-requests the files in that case, so it only slowed down the page loading for me.
Try using Firefox if you're not already using it and see if it makes any difference.

Interesting that my esp8266 is managing this situation without any problem.

@MadDogMayCry0
Copy link

Same issue...

@LucasFeliciano21
Copy link

Same problem here, but with the MQTT client, it just freezes and the Debug just shows "[D][WiFiClient.cpp:463] connected(): Disconnected: RES: 0, ERR: 128"

I think it's something related with the WifiClient.

Any news guys?

@nae9
Copy link
Author

nae9 commented Feb 18, 2019

WiFi.setSleep(false); after wifi.begin() help me eliminate appearing phantom client that block client parsing (with free buffer). Any way simple examples with wifi on ESP32 today get topic problem if not add WiFi.setSleep(false) in their setup. Probably this solving only masked initial cause of problem.

@me21
Copy link

me21 commented Feb 18, 2019

@nae9 can you please describe what exactly happens if you don't call WiFi.setSleep(false);?

@nae9
Copy link
Author

nae9 commented Feb 19, 2019

@nae9 can you please describe what exactly happens if you don't call WiFi.setSleep(false);?

See SimpleWiFiServer.ino example from WiFi examples branch:
`void loop() {
WiFiClient client = server.available(); // listen for incoming clients

if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read();
Serial.write(c);
if (c == '\n') { // if the byte is a newline character
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
//...
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
//...
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
`
Normally when client connects, there is some message in buffer and you can read it by client.read() and this action decrement client.available() number by 1. After read last symbol (commonly = '\n') client.available()==0 and program jump from while (client.connected()) loop by break; and then client.stop(); zeroes client number.
But on ESP32 release libs there is some bug... client.stop() not zeroing client number and in second loop we have client==1 but client.available() == 0 and program spin in infinity while loop. After some time timeout appeared and client disconnects by timeout, while loop ends and we see in debug console info about timeout:

...
New Client.
17:53:51.764 -> GET / HTTP/1.1
17:53:51.764 -> Host: 192.168.1.210
17:53:51.764 -> User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
17:53:51.764 -> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
17:53:51.804 -> Accept-Language: en-US,en;q=0.5
17:53:51.804 -> Accept-Encoding: gzip, deflate
17:53:51.804 -> Connection: keep-alive
17:53:51.804 -> Upgrade-Insecure-Requests: 1
17:53:51.804 ->
Client Disconnected.
17:53:51.884 -> New Client.
[D][WiFiClient.cpp:452] connected(): Disconnected: RES: 0, ERR: 128
Client Disconnected.

If i type WiFi.setSleep(false); after WiFi.begin(), then client.stop() make client==0 and all work fine (as on other platforms). A couple year ago all work fine on ESP32 with no WiFi.setSleep(false); directive, but when WiFiClient was modified to use buffer somebody bugged...

@atanisoft
Copy link
Collaborator

This issue appears to go a bit deeper than just the sleep param. The read() method doesn't check if the client is still connected thus it may mask the disconnected state until some sort of timeout occurs. This may be a bug in the rxBuf code as it is only checking for bytes to read and not detecting the disconnected state until some timeout inside LwIP.

@locchi93
Copy link

Same issue!

@tfry-git
Copy link

For what it's worth, a few things that I think I have understood about this problem, before hitting a wall:

  1. Error code 128 is ENOTCONN (socket is not connected).
  2. There appear to be two sources of the debug message. One is for connections that are simply closed by the remote. That one is quite benign.
  3. The other source of the message is when a new connection comes in, it sometimes isn't accepted, properly. That one is problematic.
  4. The closest I could get to the point of trouble was WiFiServer::available(). I inserted a check whether the newly created WiFiClient is actually connected(), inside this function. By this, I found that the error - if it occurs - occurs directly after the connection has been accepted.
  5. I toyed a bit with re-trying to accept the connection in this case, but if the error occurs, the same happens in the retries (only with different fd-numbers).
  6. Finally, I do seem to note that the problem is more likely to occur, when there are multiple incoming connections in close proximity (less than a second or so). When connections are more spaced out, things seem to run smoothly, for the most part. A race condition?

Not sure, whether this helps any. However at least it offers some insight into why setSleep(false) will help in some cases: As the error occurs when accepting connections, if you can avoid closing and re-opening connections, that will be a good thing. However, if you need to deal with a lot of short incoming connections, you're out of luck. Unfortunately, my use-case falls into the latter category...

@paulhaggard
Copy link

I'm seeing the same message with very similar circumstances as you guys, except it's not causing me any issues that I can see - I'm not trying to keep a websocket open. I have two clients connected, and they are able to interact with the http server I'm running.

I also appear to only see the message when log level is Debug or Verbose. Http headers look fine in Chrome inspector.

Are you all trying to have a websocket stay open?

@tfry-git
Copy link

tfry-git commented Mar 12, 2019

@paulhaggard : Yes, the message is harmless in some case, but goes along with a real error in other cases.

TLDR: The error occurs, if the ESP is trying to accept a new client, while it has not yet finished writing. Testcase below.

I think I have finally narrowed down the conditions when this occurs. Hopefully, this will help in fixing this annoying issue. Below, you'll find a self-contained testcase. Amend setup() with your connection credentials, then point a browser at your ESP. Play with the parameters, and hit "Go" to send a series of AJAX requests. Long story short, for me the pattern is quite clear: If either the payload size (written by the ESP) is getting high, or, the timeout between requests is getting short, you'll start seeing errors. Near the tipping point, you'll see a distinct pattern of exactly every other request failing. For me the tipping point is around a payload size of 2500, and a delay of 500 ms, but YMMV depending on connection strength, etc.

So, in combination with what I've written earlier, I think we can conclude the problem is that accepting incoming connections is broken, while data has not yet finished writing.

Testcase:

#include <WebServer.h>
WebServer server(80);
void handlePage();

void setup() {
    Serial.begin (115200);
    Serial.setDebugOutput(true);

    // Example WIFI setup as an access point. Change this to your needs
    WiFi.mode(WIFI_STA);
    WiFi.begin("MyNet", "password");

    server.on("/", handlePage);
    server.begin();
}

void loop() {
    server.handleClient();
}

void handlePage() {
    server.setContentLength(CONTENT_LENGTH_UNKNOWN);
    if(server.method() == HTTP_POST) { // AJAX request
        int payload = server.arg("payload").toInt();
        Serial.print("Serving payload size: ");
        Serial.println(payload);
        server.send(200, "text/json", "");

        // spread the payload across a couple writes, for a more realistic API use
        for(int i = payload / 32; i > 0; --i) {
          server.sendContent("xxxxxxx.xxxxxxx.xxxxxxx.xxxxxxx.");
        }
        for(int i = payload % 32; i > 0; --i) {
          server.sendContent("x");
        }
    } else {  // Page load
        Serial.println("Serving test page");
        server.send(200, "text/html", "");
        server.sendContent(
"<!DOCTYPE html>\n"
"<html>\n"
"<body>\n"
"<h2>Connectivity test</h2>\n"
"\n"
"n <input type=\"text\" value=\"4\" id=\"n\"/>\n"
"payload <input type=\"text\" value=\"256\" id=\"payload\"/>\n"
"delay <input type=\"text\" value=\"1000\" id=\"delay\"/>\n"
"<button type=\"button\" onclick=\"document.getElementById('result').clear(); doTest(document.getElementById('n').value, document.getElementById('payload').value, document.getElementById('delay').value);\">Go</button>\n"
"\n"
"<h2>Result</h2>\n"
"<div id=\"result\">No test, yet</div>\n"
"\n"
"<script>\n"
"var results = document.getElementById('result');\n"
"result.update = function(sent, success, error, timeout) {\n"
"  this.innerHTML = 'Sent: ' + (this.sent += sent) + ' Successes: ' + (this.successes += success) + ' Errors: ' + (this.errors += error) + ' Timeouts: ' + (this.timeouts += timeout);\n"
"};\n"
"result.clear = function() {\n"
"  this.sent = this.successes = this.errors = this.timeouts = 0;\n"
"  this.update(0,0,0,0);\n"
"};\n"
"results.clear();\n"
"\n"
"function doTest(n, payload, delay) {\n"
"  if(n < 1) return;\n"
"  \n"
"  var req = new XMLHttpRequest();\n"
"  req.timeout = 5000;\n"
"  req.onload = function() { document.getElementById('result').update(0, (this.readyState == 4), 0, 0); };\n"
"  req.onerror = function() { document.getElementById('result').update(0, 0, 1, 0); };\n"
"  req.ontimeout = function() { document.getElementById('result').update(0, 0, 0, 1); };\n"
"  results.update(1, 0, 0, 0)\n"
"  req.open(\"POST\", \"/?payload=\" + payload, true);\n"
"  req.send(\"\");\n"
"  \n"
"  setTimeout(function() {doTest(n-1, payload, delay)}, delay);\n"
"}\n"
"</script>\n"
"</body>\n"
"</html>\n"
        );
    }
}

@tfry-git tfry-git mentioned this issue Mar 12, 2019
@tfry-git
Copy link

tfry-git commented Mar 26, 2019

For what it's worth, I will add that the porting my test case to AsyncTCP / ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer) makes the problem disappear.

So that might be both a workaround for the time being, and - hopefully - a hint towards a fix.

@stale
Copy link

stale bot commented Aug 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Aug 1, 2019
@tfry-git
Copy link

tfry-git commented Aug 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I believe the reason that this report has not received any recent activity is not that it is no longer relevant. However, everything of relevance has already been stated. Please keep it open.

@cyberman54
Copy link
Contributor

@xreef so far so fair, but what's your insight regarding the root cause of this issue?

@xreef
Copy link

xreef commented Nov 10, 2020

@cyberman54 ahh ok, sorry.
In the examples of second article I do a simple request that return a page and an image inside, and there aren't problem, but the problem become from third article where I'm going to return a whole site (with multiple image and js), the problem seems that the WiFiClient can't manage multiple/parallel request and stop work.
A similar problem in AsyncWebServer fixed on AsyncTCP with this issue, and I can confirm that, in the third article I replicate the same behavior of full site and works correctly.

When I have more time I'd like to check the change on AsyncTCP.

Bye Renzo

@stale
Copy link

stale bot commented Jan 9, 2021

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Jan 9, 2021
@tfry-git
Copy link

tfry-git commented Jan 9, 2021

Dear stale bot. Here's some useless noise to keep you happy. Thanks.

@stale
Copy link

stale bot commented Jan 9, 2021

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.

@sim1234
Copy link
Contributor

sim1234 commented Feb 7, 2021

The issue is still here.

@torntrousers
Copy link
Contributor

Still here for me too. Tried the WiFi.setSleep(false); suggested near the top of this issue but it doesn't help.

jludwig75 added a commit to jludwig75/ESPAlarmSystem that referenced this issue Feb 15, 2021
I ran into this ESP32 bug:
espressif/arduino-esp32#1921
Evrything works fine form FireFox, but not at all from
chrome or Edge.
@alejandro-garcia-fhnw
Copy link

having the same problem.. any news?

@cyberman54
Copy link
Contributor

cyberman54 commented Mar 6, 2021

For me, it seems this issue finally disappeared with IDF 4.1 arduino-esp32 v1.0.5

@Schrolli91
Copy link

@cyberman54
When was 4.1 released?

@cyberman54
Copy link
Contributor

@Schrolli91 i was wrong, talking on arduino-esp32 version. Corrected.

@torntrousers
Copy link
Contributor

After @cyberman54 comment I gave it a go with v1.0.5 - but I still get the 'Disconnected: RES: 0, ERR: 128' problem. It prompted me to do a bit more debugging - my code is making an HTTP request and its sending a "Connection: close" header, if I take that out (so it defaults to keep-alive) then it works fine and I don't see the 'Disconnected: RES: 0, ERR: 128' problem any more.

@ElToberino
Copy link

For me too, this issue is gone with V 1.0.5.

@cyberman54
Copy link
Contributor

@torntrousers That's in an important note. I checked again my application running on v1.0.5 and can confirm: HTTP headers with "connection close" trigger a "Disconnected" thrown by WiFiClient.cpp, but this does not happen if HTTP header "connection keep-alive" is used. Maybe not a bug, but a feature?

@DriekdeGadgetfreak
Copy link

Never use Connection:close in combination with the current wifi-client. The connection will be gone before data is received.

@xreef
Copy link

xreef commented Mar 31, 2021

I re-test all my examples, and with 1.0.5 core works.
Thanks

@cyberman54
Copy link
Contributor

Finally closed ;-)

@rugbroed
Copy link

I'm happy to have found this thread.

Even though the problem seems fixed by the latest comments, I'm still struggling with the 'Disconnected: RES: 0, ERR: 128' error on a new/updated platform.io installation on linux with ESP32 Pico Kit. What am I missing?


Processing pico32 (platform: espressif32; board: pico32; framework: arduino)

CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/pico32.html
PLATFORM: Espressif 32 (4.3.0) > ESP32 Pico Kit
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:

  • framework-arduinoespressif32 @ 3.20003.0 (2.0.3)
  • tool-esptoolpy @ 1.30300.0 (3.3.0)
  • toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
    LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 33 compatible libraries
    Scanning dependencies...
    Dependency Graph
    |-- EEPROM @ 2.0.0
    |-- Update @ 2.0.0
    |-- WebServer @ 2.0.0
    | |-- WiFi @ 2.0.0
    | |-- FS @ 2.0.0
    |-- WiFi @ 2.0.0
    Building in release mode
    Compiling .pio/build/pico32/lib3ce/EEPROM/EEPROM.cpp.o
    Compiling .pio/build/pico32/libefe/Update/HttpsOTAUpdate.cpp.o
    Compiling .pio/build/pico32/libefe/Update/Updater.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFi.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiAP.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiClient.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiGeneric.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiMulti.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiSTA.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiScan.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiServer.cpp.o
    Compiling .pio/build/pico32/lib232/WiFi/WiFiUdp.cpp.o
    Compiling .pio/build/pico32/libf3d/FS/FS.cpp.o
    Compiling .pio/build/pico32/libf3d/FS/vfs_api.cpp.o
    Compiling .pio/build/pico32/lib151/WebServer/Parsing.cpp.o
    Compiling .pio/build/pico32/lib151/WebServer/WebServer.cpp.o
    Compiling .pio/build/pico32/lib151/WebServer/detail/mimetable.cpp.o
    Linking .pio/build/pico32/firmware.elf
    Retrieving maximum program size .pio/build/pico32/firmware.elf
    Checking size .pio/build/pico32/firmware.elf
    Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
    RAM: [= ] 11.6% (used 38112 bytes from 327680 bytes)
    Flash: [===== ] 54.8% (used 718181 bytes from 1310720 bytes)

@theotherjenkutler
Copy link

I am also having this problem.

@redgreenloko
Copy link

redgreenloko commented Jun 16, 2023

Arduino Release v2.0.9 - the problem is still there. The only thing that helped me to reduce the impact of this bug it is to run the client-server communication loop in a higher priority task. The nasty "Disconnected: RES: 0, ERR: 128" still pops in the serial monitor from time to time and ESP32 resets connection (can see it in WireShark) but not as often as with 0 priority.

@skokovsergey
Copy link

I noticed that I had this error when a lot of data were sent via WIFI, when I added a 10 ms delay between messages, the error disappeared. This didn't help: WiFi.setSleep(false);

@jenschr
Copy link

jenschr commented Aug 12, 2024

This is still happening despite the issue opened 6 years ago and closed 2 years ago. I'm using PlatformIO (Core 6.1.15·Home 3.4.4) with Espressif32 v6.7.0.

I found the reason in my case and it's a pure PEBKAC. The error started showing when I connected to a server with illegal characters in the URI. I've got to be more careful about those string copies since invisible characters can also be illegal....

@jameszah
Copy link

I was reading this old post looking for incites.
My solution turned out to be that the WebServer was running at low priority (normal loop() prio 1), and there was other stuff running at 5 (httpd server stuff used in the same program). This was re-compiling with more recent arduino-core on a stable old program with 3-4 tasks at different prioritys and different cores -- the new arduino stuff seems to upset the balance.

Solution was to run the WebSerever.handleClient(); at a higher priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues
Projects
None yet
Development

No branches or pull requests