Standalone TCP Server [ Solved #4]

I want to continuously send some data once every fixed interval of say 50 or 100ms via TCP to a connected PC .

I am using the ESP8266 as a Access Point for this and the PC will be connected to the same via WiFi. The functionality I need is continuous transmission by the ESP8266 server if I initiate the connection once. Now everytime I initiate , it send data once and closes connection. ( which I think is the way TCP works ) Can this be modified to the way I need ?

//-- Libraries Included -------------
#include <ESP8266WiFi.h>
//------------------------------------
// Authentication Variables
char*       ssid;              // SERVER WIFI NAME
char*       password;          // SERVER PASSWORD

//------------------------------------
// WiFi settings
#define     MAXSC     6           // MAXIMUM NUMBER OF CLIENTS

IPAddress APlocal_IP(192, 168, 4, 1);
IPAddress APgateway(192, 168, 4, 1);
IPAddress APsubnet(255, 255, 255, 0);

unsigned int TCPPort = 2390;

WiFiServer  TCP_SERVER(TCPPort);      // THE SERVER AND THE PORT NUMBER
WiFiClient  TCP_Client[MAXSC];        // THE SERVER CLIENTS Maximum number
//-------------------------------------
// Some Variables
char result[10];


void setup() {

  // Setting the serial port
  Serial.begin(9600);           // Computer Communication

  // setting up a Wifi AccessPoint
  SetWifi("DataTransfer", "");
}

//======================================

void loop() {

  HandleClients();

}

//======================================

void SetWifi(char* Name, char* Password) {
  // Stop any previous WIFI
  WiFi.disconnect();

  // Setting The Wifi Mode
  WiFi.mode(WIFI_AP_STA);
  Serial.println("WIFI Mode : AccessPoint");

  // Setting the AccessPoint name & password
  ssid      = Name;
  password  = Password;

  // Starting the access point
  WiFi.softAPConfig(APlocal_IP, APgateway, APsubnet);                 // softAPConfig (local_ip, gateway, subnet)
  WiFi.softAP(ssid, password, 1, 0, MAXSC);                           // WiFi.softAP(ssid, password, channel, hidden, max_connection)
  Serial.println("WIFI < " + String(ssid) + " > ... Started");

  // wait a bit
  delay(50);

  // getting server IP
  IPAddress IP = WiFi.softAPIP();

  // printing the server IP address
  Serial.print("AccessPoint IP : ");
  Serial.println(IP);

  // starting server
  TCP_SERVER.begin();                                                 // which means basically WiFiServer(TCPPort);

  Serial.println("Server Started");
}

//========================================

void HandleClients() {
  unsigned long tNow;

  WiFiClient TCP_Client = TCP_SERVER.available();
  tNow = millis();
  dtostrf(tNow, 8, 0, result);
  TCP_Client.println(result);

}

make client a global or static variable and use it while it is connected

static WiFiClient client = server.available();

if (!client) {
client = server.available();
}
if (client) {
if (client.connected()) {
while (client.available()) {
int c = client.read();
...
}
} else {
client.stop();
}
}

@Juraj

Tried the idea as suggested by you changing the HandleClients() function as below. But the behaviour is same as earlier. I get a response only when I send something and then it stops... maybe I have made a mistake in implementing your idea ?

void HandleClients() {
  unsigned long tNow;
  static WiFiClient TCP_Client = TCP_SERVER.available();

  if (!TCP_Client) {
    TCP_Client = TCP_SERVER.available();
  }
  if (TCP_Client) {
    if (TCP_Client.connected()) {
      while (TCP_Client.available()) {
        int c = TCP_Client.read();
        // WiFiClient TCP_Client = TCP_SERVER.available();
        tNow = millis();
        dtostrf(tNow, 8, 0, result);
        TCP_Client.println(result);
      }
    } else {
      TCP_Client.stop();
    }
  }
}

Mogaraghu:
@Juraj

Tried the idea as suggested by you changing the HandleClients() function as below. But the behaviour is same as earlier. I get a response only when I send something and then it stops... maybe I have made a mistake in implementing your idea ?

void HandleClients() {

unsigned long tNow;
  static WiFiClient TCP_Client = TCP_SERVER.available();

if (!TCP_Client) {
    TCP_Client = TCP_SERVER.available();
  }
  if (TCP_Client) {
    if (TCP_Client.connected()) {
      while (TCP_Client.available()) {
        int c = TCP_Client.read();
        // WiFiClient TCP_Client = TCP_SERVER.available();
        tNow = millis();
        dtostrf(tNow, 8, 0, result);
        TCP_Client.println(result);
      }
    } else {
      TCP_Client.stop();
    }
  }
}

because you send only in while (available())

if (TCP_Client) {
if (TCP_Client.connected()) {
while (TCP_Client.available()) {
int c = TCP_Client.read();
Serial.write(c);
}
tNow = millis();
dtostrf(tNow, 8, 0, result);
TCP_Client.println(result);
} else {
TCP_Client.stop();
}
}

@Juraj

Yes that did the trick and its working. :slight_smile: Thanks. I hope it will be the same logic for UDP also ?

I was sending data from my ESP8266 at 100ms intervals but find that the TCP test tool prints one dummy line without data in between. See below ... any idea why this happens ?

TCP_ClientRead.JPG

TCP_ClientRead.JPG

println sends \r\n at the end. if you print this on server again with println then it adds \r\n again