Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By akashathale
#51570 Hi all,
I am trying to configure ESP8266 as WiFi server AP, and I want to run a TCP server socket on the same..
However, the socket connection disconnects after few seconds..

Kindly let me know a way to keep the client connected..
Here is my code:

//#include <ESP8266.h>
#include <ESP8266WiFi.h>
#include <WiFiServer.h>
#include <SoftwareSerial.h>
#include <WiFiClient.h>

/*Define the input message*/
String AT_COMMAND = "";
String ser_buff = "";
/*to define the SSID and password of the WiFi*/
const char *ssid = "horizon_innova";
const char *password = "hesolutions123";
/*To initiate the WiFi server*/
WiFiServer server(5001);
/*Define the state of WiFi*/
int status = WL_IDLE_STATUS;
/*Initialize the WiFi client object*/
//WiFiClient ILS_Base_Clients;
void setup() {
// put your setup code here, to run once:
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.begin(9600);
status = WiFi.begin();
Serial.println("Welcome to horizon Wifi\n");
Serial.println(status);
server.begin();
}

void Accept_Serial(WiFiClient client){
if(Serial.available()){
char ser_recv = Serial.read();
ser_buff += ser_recv;
}
client.print(ser_buff);
ser_buff = "";
}

/*to accept the client connection*/
void Accept_Client(){
WiFiClient client;
if(!client.connected()){
client = server.available();
//Serial.println("no clients connected\n");
}
if(client.connected() >= 1){
client.print("@@@This is a handshake signal@@@\n");
// Serial.println("client connected is\n");
while(client.available()){
char c = client.read();
AT_COMMAND += c;
}
Serial.println(AT_COMMAND);
client.print(AT_COMMAND);
client.flush();
AT_COMMAND = "";
}
}
// put your main code here, to run repeatedly:
void loop() {
while(1){
Accept_Client();
}
}