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);
}