Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

TCP server ESP8266 resets client connection

Open hakeemta opened this issue 10 years ago • 12 comments

I need to make my ESP8266 Mod12 have more than one socket.

At least I want it to act as a client connecting to a cloud server via websocket. In this way, it joins an existing Internet gateway (or AP).

Also, I want to have use ESP8266 softAP and create some server sockets listening for client from other devices.

I have the following specifications/schemes:

  1. The ESP configured or setup in STA+AP mode.
  2. As a Station, it connects to an existing Internet AP.
  3. As a "soft" AP, it accepts connection from other WiFi-enabled device.
  4. A client socket to a cloud server (websocket)
  5. Server socket listening to two or three clients' connection.

I succeeded in establishing all these but a moment later after testing, my phone looses connection with the Server socket running on the ESP; logging ECONNRESET error. Since then, I've not been able to establish a connection with the server even in just a single socket.

Pls assist.

hakeemta avatar Jan 07 '16 15:01 hakeemta

if still open, attach your code / a MWE and state the versions of IDE and arduino-esp variant used (2.0.0, 2.1.0RC, GIT, ...).

nouser2013 avatar Jan 14 '16 20:01 nouser2013

Thank you so much. Below is the code, I decided to use this sample below and still the same issue persists.

IDE: Arduino 1.65 (also Visual Micro + Atmel Studio) Ard-ESP Variant: 1.6.5.1160-gef26c5f

hakeemta avatar Jan 14 '16 20:01 hakeemta

#include <ESP8266WiFi.h>
#include <WebSocketClient/WebSocketClient.h>
#include <WiFiClient.h>

/* Set these to your desired credentials. */
const char *AP_ssid = "AP_chas";
const char *AP_passkey = "12345678";

int status = WL_IDLE_STATUS;
boolean alreadyConnected[2] ; // whether or not the client was connected previously

WiFiServer server(6000);
WiFiClient client[2];

#define Config_Led D2

void setup()
{
    delay(1000);
    Serial.begin(115200);
    Serial.println();
    Serial.println("\nConfiguring access point...");

    WiFi.softAP(AP_ssid, AP_passkey);
    delay(10000);
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);

    Serial.println("\nStarting server...");
    // start the server:
    server.begin(); 
    Serial.println("\nServer Started.");
}

void loop()
{
    unsigned int i;
    for (i= 0; i<2; i++)
    {
        // wait for a new client:
        if (!client[i])
        {
            client[i] = server.available();
        }
        else
        {
            if (client[i].status() == CLOSED)
            {
                client[i].stop();
                Serial.print("\nConnection closed on client: "  );
                Serial.println(i);
                Serial.println();
                alreadyConnected[i] = false;
            }

            else
            {
                if(!alreadyConnected[i]) // when the client sends the first byte, say hello:
                {
                    // clean out the input buffer:
                    client[i].flush();
                    Serial.print("\nNew client: ");
                    client[i].println("Hello, client !");
                    Serial.println(i);
                    alreadyConnected[i] = true;
                }

                if (client[i].available())
                {
                    // read the bytes incoming from the client:
                    String data = client[i].readString();
                    // echo the bytes back to the client:
                    client[i].print(data);
                    // echo the bytes to the server as well:
                    Serial.println(data);
                }               
            }
        }
    }
}

hakeemta avatar Jan 14 '16 20:01 hakeemta

well, I dont see anything wrong with the code. You can try with one single client first, though that should not make a difference. Also I suggest that you upgrade the IDE to 1.6.7 and the variant to at least 2.0.0 or even the 2.1.0-rc1.

nouser2013 avatar Jan 14 '16 20:01 nouser2013

Thanks. I'll try that and get back with results.

hakeemta avatar Jan 14 '16 20:01 hakeemta

I had the same problem, solved by adding the following line:

WiFi.mode(WIFI_AP);

sounds like if you don't add this line, it won't notice data coming from AP...

nimamahmoudi avatar Jan 24 '16 07:01 nimamahmoudi

That's really incredible! That solves it, you guys here are geniuses.

Thanks @nouser2013, @nima72 and others. I can move on with my work now.

hakeemta avatar Jan 24 '16 22:01 hakeemta

Hello nima72, where do you include this WiFi.mode(WIFI_AP); Thanks a lot.

ghost avatar Jul 28 '16 23:07 ghost

Hey Guys, I am re-bumping this, since this is still happening on 2.3.0, @igrr I had the problem that my WiFiClient's status changed from 4 to 0 (CLOSED) and a FIN packet was sent. This might be related to the WiFiClient#close calls I was seeing, but I can't verify that. So this meant that my connection was killed after a few bytes and then I had to reestablish it.

@ByTorrez You just need to specify WiFi.mode(WIFI_AP); before the WiFi.softAP calls. #529 might have been related to that, so you'd have to specify it after softAP, but this seems to be fixed indeed.

Another thing worth mentioning: Unlike in this Issue, I did explictly specifiy WiFi.mode(WIFI_STA_AP); (not knowing what I was doing, actually, I think the WebServerExample also does this wrong?).

So to me it seems that something in STA_AP mode is killing the connections, when you spawn an AP but don't connect to an AP on your own (that's at least what I could experience)

Thanks for the awesome work, guys!

MeFisto94 avatar Dec 07 '16 21:12 MeFisto94

hello, hakeemta? are u create local server hakeemta?. i want to listen another cloud server data ,this data sent to connected client what i do?

sprabu37 avatar Jan 02 '18 07:01 sprabu37

Just to confirm: Indeed, if WIFI_STA was set I experienced constant websocket connection interruptions, usually 1-2 times / min. :

WiFi.mode(WIFI_STA);
WiFi.begin(..., ...);

Setting WiFi.mode(WIFI_AP); has completely solved this, even though the ESP connects to the existing network as if were in STA mode..

WiFi.mode(WIFI_AP);
WiFi.begin(..., ...);

(I need to connect to an existing network - so using WIFI_AP is actually wrong, but apparently still connecting if the specified network already exists).

With WIFI_STA mode set I couldn't get a stable websocket connection. Tested with two devices to be sure. Looks like a bug to me. @igrr ?

bachi76 avatar Mar 05 '18 12:03 bachi76

Thank you bachi76, changing my mode to WIFI_AP solved all my server connection issues!

youmebangbang avatar Apr 04 '18 19:04 youmebangbang