I'm having problems with connecting between two ESP's when one of them is in softAP mode. When they are both connected to a WiFi router, everything works, but when one of them is in softAP mode, connection fails 100% of the time. Everything works when I'm using a PC as either a TCP server or a TCP client, so the code must be correct.
I have tried different configurations and none of them work:
- ESP(STA + TCP CLIENT) to ESP(AP + TCP SERVER)
- ESP(AP + TCP CLIENT) to ESP(STA + TCP SERVER)
- ESP(STA + TCP CLIENT) through ESP(AP) to ESP(STA + TCP) server
Client:
#include <Arduino.h>
#include <ESP8266WiFi.h>
String ssid = "AAAAAAAA";
String pass = "AAAAAAAA";
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
ESP.eraseConfig();
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid.c_str(), pass.c_str());
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
delay(500);
}
delay(2000);
IPAddress ip(192, 168, 4, 1);
if (client.connect(ip, SRV_PORT)) {
Serial.printf("Success!\n");
} else {
Serial.printf("Fail!\n");
}
}
void loop() {
yield();
}
Server:
#include <Arduino.h>
#include <ESP8266WiFi.h>
String ssid = "AAAAAAAA";
String pass = "AAAAAAAA";
#define SRV_PORT 4210
#define SRV_MAX_CLIENTS 3
WiFiServer server(SRV_PORT);
WiFiClient serverClients[SRV_MAX_CLIENTS];
void setup() {
Serial.begin(115200);
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
ESP.eraseConfig();
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(ssid.c_str(), pass.c_str());
delay(2000);
server.begin();
server.setNoDelay(true);
}
void loop() {
yield();
uint8_t i;
if (server.hasClient()) {
for (i = 0; i < SRV_MAX_CLIENTS; i++) {
if (!serverClients[i] || !isConnected(&serverClients[i])) {
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
serverClients[i].flush();
continue;
}
}
//no free spot
WiFiClient serverClient = server.available();
serverClient.stop();
}
for (i = 0; i < SRV_MAX_CLIENTS; i++) {
if (serverClients[i] && isConnected(&serverClients[i])) {
Serial.prinf("Got data!\n");
}
}
}
bool isConnected(WiFiClient* c) {
return c->connected() && c->status() != 0;
}
I've spent a whole day debugging and trying to fix this, but can't find a solution. I've analyzed the TCP protocol through Wireshark on a PC when the PC was the TCP server and the ESP was the TCP client and the other way around and everything seems to be OK.
Please help!