Why my server cannot send the data to client?

Hi,
I have to send data to the client in the interrupt function. But apparently, the client cannot receive the data. The server did get the question from the client but the client cannot reply back.

When I try the communication in void loop it is a success. But once I put it in the interrupt, I cannot get the data back. The data that the server has to send is simply a number which will then trigger the led at the client side. It is really helpful if someone can help me figure it out.

This is the server code.
#include <SPI.h>
#include <ESP8266WiFi.h>

#include <math.h>
#include <Wire.h>
#include <BH1750FVI.h>

#define timeSeconds 10
#define threshold 100

uint16_t LightValue;
const int led = 12;
const int motionSensor = 14;
const int light = 4;

// Settings for BH1750
uint8_t ADDRESSPIN = 13;
BH1750FVI::eDeviceAddress_t DEVICEADDRESS = BH1750FVI::k_DevAddress_H;
BH1750FVI::eDeviceMode_t DEVICEMODE = BH1750FVI::k_DevModeContHighRes;

// Create the Lightsensor instance
BH1750FVI LightSensor(ADDRESSPIN, DEVICEADDRESS, DEVICEMODE);

// PIR Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;

byte ledPin = 2;
char ssid[] = "IOTWIFI";
// SSID of your home WiFi
char pass[] = "12345678"; // password of your home WiFi
WiFiServer server(80);

IPAddress ip(192, 168, 0, 101); // IP address of the server
IPAddress gateway(192,168,0,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network

void ICACHE_FLASH_ATTR detectsMovement()
{
LightValue = LightSensor.GetLightIntensity();
Serial.print("Light: ");
Serial.println(LightValue);

if (LightValue < threshold )
{
Serial.println("MOTION DETECTED!!!");
analogWrite(led, 1023);
startTimer = true;
lastTrigger = millis();

WiFiClient client = server.available();
if (client)
{
if (client.connected())
{
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println("");
String request = client.readStringUntil('\r'); // receives the message from the client
Serial.print("From client: "); Serial.println(request);
client.flush();
client.println("1\r"); // sends the answer to the client
digitalWrite(ledPin, HIGH);
}
client.stop(); // terminates the connection with the client
}
}
}

void setup() {
Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}

pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);

server.begin(); // starts the server

pinMode(light, INPUT);
LightSensor.begin();
Serial.println("Running...");

pinMode(ledPin, OUTPUT);
pinMode(led, OUTPUT);

}

void loop () {

// Current time
now = millis();

// Turn off the LED after the number of seconds defined in the timeSeconds variable
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
startTimer = false;}

delay(1000);
}

And the client code
#include <SPI.h>
#include <ESP8266WiFi.h>

byte ledPin = 2;
char ssid[] = "IOTWIFI"; // SSID of your home WiFi
char pass[] = "12345678"; // password of your home WiFi

unsigned long askTimer = 0;

IPAddress server(192,168,0,101); // the fix IP address of the server
WiFiClient client;

void setup() {
Serial.begin(115200); // only for debug
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

pinMode(ledPin, OUTPUT);
pinMode(D0, OUTPUT);
}

void loop () {
client.connect(server, 80); // Connection to the server
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
client.println("Hello server! Are you sleeping?\r"); // sends the message to the server
String answer = client.readStringUntil('\r'); // receives the answer from the server
Serial.println("from server: " + answer);
if (answer == "1")
{
Serial.println("Success!");
analogWrite(D0, 512);
}
client.flush();4
digitalWrite(ledPin, HIGH);
delay(2000); // client will trigger the communication after two seconds
}

first switch the roles. client must the one initiating the communication

then don't do the communication in interrupt. set a flag variable with interrupt and handle things in loop()

Juraj:
first switch the roles. client must the one initiating the communication

then don't do the communication in interrupt. set a flag variable with interrupt and handle things in loop()

Thank you for replying.
Isn't the client already the one initiating the communication?
Do you have any example that I can refer to about the flag?

maisarrahh:
Thank you for replying.
Isn't the client already the one initiating the communication?
Do you have any example that I can refer to about the flag?

the trigger is the sensor, so the esp8266 with the sensor should be the client

global:
volatile bool flag = false;

in ISR:
flag = true;

in loop():

if (flag) {
if (client.connect(...)) {
...
flag = false;
}
}

Juraj:
the trigger is the sensor, so the esp8266 with the sensor should be the client

My project is quite complicated. At least for me. I have to make the esp8266 to be both client and server. So if the motion sensor senses something it will trigger the interrupt then turn on the led and them it will notify the other esp. In the same time, it will also be ready for any notification from the other esp.