Great job @countrypaul , I followed your instructions and made necessary changes. Now the code is working perfect; server and client are communicating flawlessly. I added the final version of the codes here.
Updated Server and Client Codes are attached.
Following links were my main references to start this code:
multiple client server over wifi
esp32-server-and-many-esp8266-clients-controlled-over-wifi
Version 0.1 of Server Code:
// Server:
// This code designed to enable server and client board to communicate with each other
// by calling their name and exchanging messages over wifi
#include <Arduino.h>
#include "WiFi.h"
#define NUM_CLIENTS 2
String data;
String CLIENT;
String ACTION;
const char *ssid = "ESP32-Access-Point";
const char *password = "123456789";
IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(80);
// This matrix is defined for further development, it has not been used in the code
WiFiClient *clients[NUM_CLIENTS] = {NULL};
void clientCommand(String input)
{
for (int i = 0; i < NUM_CLIENTS; i++)
{
WiFiClient client = server.available();
if (client)
{
if (client.connected())
{
client.println(input);
delay(10);
}
}
client.stop();
}
}
String clientRequest(String input)
{
String response = "\0";
for (int i = 0; i < NUM_CLIENTS; i++)
{
WiFiClient client = server.available();
if (client)
{
client.setTimeout(50);
if (client.connected())
{
client.println(input);
data = client.readStringUntil('\r'); // received the server's answer
if (data != "\0")
{
int Index = data.indexOf(':');
CLIENT = data.substring(0, Index);
ACTION = data.substring(Index + 1);
if (CLIENT == "ACK")
{
response = ACTION;
}
data = "\0";
}
}
}
}
return response;
}
// This void is for further developmets and has not ben used
void connect_wifi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
//delay(100);
}
}
void setup()
{
Serial.begin(9600);
//Start Server
Serial.println();
Serial.print("Setting soft AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
WiFi.begin();
server.begin();
//End: Server
}
void loop()
{
if ((clientRequest("SENSOR1:TEMPERATURE?").toInt()) > 30) //Request the temperature from SENSOR1
{
clientCommand("OUTPUT1:ON"); //Sets the output on OUTPUT1
Serial.println("This is Server: Output1:On was sent to client");
}
else
{
clientCommand("OUTPUT1:OFF"); //Sets the output on OUTPUT1
Serial.println("This is Server: Output1:Off was sent to client");
}
}
Version 0.1 of Client Code:
// Client:
// This code designed to enable server and client board to communicate with each other
// by calling their name and exchanging messages over wifi
#include <Arduino.h>
#include <WiFi.h>
const char *ssid = "ESP32-Access-Point";
const char *password = "123456789";
unsigned long previousMillis = 0;
int interval = 500;
String CLIENT_NAME = "SENSOR1";
WiFiClient client;
// IP Address of the server
IPAddress server(192, 168, 4, 1);
String ServerMessage;
void setup()
{
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(50);
Serial.print(".");
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
Serial.println("Attemting to connect");
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("wifi status connected");
client.connect(server, 80);
while (client.connected())
{
Serial.println("client connected");
String data = client.readStringUntil('\r');
Serial.println(data);
if (data != "\0")
{
Serial.print("Received data from Server:");
Serial.println(data);
int Index = data.indexOf(':');
String CLIENT = data.substring(0, Index);
String ACTION = data.substring(Index + 1);
if (CLIENT == CLIENT_NAME)
{
if (ACTION == "TEMPERATURE?")
{
client.println("ACK:10");
Serial.println("This is Client: ACK10 was sent to server");
}
}
else if (CLIENT == "OUTPUT1")
{
if (ACTION == "ON" || ACTION == "OFF")
{
Serial.println("This is Client: server is sending output1 On or Off command");
}
}
client.stop();
data = "\0";
}
}
}
previousMillis = millis();
}
}