ESP to ESP continuously communication

Hi guys,

Quick help on my project please. I am using 2 Node MCUs (ESP8266).

I would like to have a two way communication between those two, while continuously being connected.

I wrote these 2 project codes but it seems that the client and server disconnects after first message sent between those two.

The clients only sends every 3 seconds a message.

The server tries to connect and then displays the message from the client (if any).

I want the connection to be one time, not in loop, so i’ve done it in setup()

THIS IS THE CLIENT code:

// esp client 2

#include <ESP8266WiFi.h>

byte ledPin = LED_BUILTIN;
char ssid[] = "Lepy_IoT";           // SSID of your AP
char pass[] = "lepy1234";         // password of your AP

IPAddress serverIP(192, 168, 4, 1);  // IP address of the AP
WiFiClient client;


void setup() {

  Serial.begin(9600);

  WiFi.mode(WIFI_STA); // configure as STATION not as ACCES POINT
  WiFi.begin(ssid, pass);           // connects to the WiFi AP (Server)

  Serial.println();
  Serial.println("Connection to the AP");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println();
  Serial.println("Connected"); // prints IP, Gateway and SERVER MAC to Serial
  Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
  Serial.println("MAC:" + WiFi.macAddress());
  Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
  Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr());
  pinMode(ledPin, OUTPUT);
  client.connect(serverIP, 80); // connects one time to server Ip and port
 // client.connect(serverIP, 80); 
 
}

void loop() { // this sends every 3 seconds a message

  
sendSomething();
//client.connect(serverIP, 80); 
  //client.flush();
  digitalWrite(ledPin, HIGH);
 
 // client.flush();
  delay(3000);

}
void sendSomething()
{
  String s = "Hello, 3 s passed!";
  Serial.println("Sending string: " + s);
  client.print(s + "\r");
  // client.flush(); // flush or not the client ?
}

THIS IS THE SERVER CODE:

// This is the ACCES POINT (SERVER):

#include <ESP8266WiFi.h>

WiFiServer server(80); // port of the server
IPAddress IP(192, 168, 4, 1); // set the IP of the server
IPAddress mask = (255, 255, 255, 0); // set the mask of the server (OPTIONAL)

byte ledPin = LED_BUILTIN; // select LED pin

WiFiClient client;

void setup() {
  Serial.begin(9600);
  
  WiFi.mode(WIFI_AP); // set the mode to ACCES POINT not STATION!
  WiFi.softAP("Lepy_IoT", "lepy1234"); // set the SSID and PASSWORD
  WiFi.softAPConfig(IP, IP, mask); // WiFi.softAP(ssid, password, channel, hidden, max_connection) -> https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/soft-access-point-class.html
  // de incercat cu un IP numai!

  server.begin(); // start the server

  pinMode(ledPin, OUTPUT);
  Serial.println();
  Serial.println("Server started.");
  Serial.print("IP: "); Serial.println(WiFi.softAPIP()); // print IP and MAC address
  Serial.print("MAC:"); Serial.println(WiFi.softAPmacAddress());
}

int conn = 0; // 0 if not connected, 1 else

void conect() // try to connect with client
{
  client = server.available();
  if (!client)
  {
    Serial.println("Try to connect with client... ");
    delay(1000);
    conect(); 
    return;
  }
  else
  {
    conn = 1;
    Serial.println("Connected with Client");
  }
}


void loop() {
  
  if (!(client = server.available())) // check if still connected
    conn = 0;
    
  while (conn == 0) // while not connected to client...
  {
    conect(); // try to connect
  }

  String request = client.readStringUntil('\r'); // get string from CLIENT
  if (request == "") // if no string is sent
  {
    Serial.println("No msg recieved");
  }
  else // string is recieved
  {
    Serial.println("Msg recieved: ");
    Serial.println("From the client: " + request);
  }
  // client.flush(); // flush the client ?

}

And…

should i use client.flush() after every message send and received ? or never if continuously connected?

What is wrong with this ? why it keeps disconnecting after the first message received.

  1. Clients connects with server
  2. Clients sends server msg
  3. Server receives msg and disconnects

“and disconnects” is the problem => it should still receive a message every 3 seconds.

Thank you in advance