Server client Nodemcu

Who can help me out…

I have a server and client Nodemcu and I want to connect a PIR to the client and let a led turn on when motion is detected.

The server seems to work fine and I can connect and switch on the led by the internet using my cellphone. The client however will not let the led turn on and will only sends the messag “motion detected” in the serial monitor. It does connect met seems to keep repeating the same step even if the PIR is not connected to the board. I can’t figure out what I am missing. Could anyone help me out?

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
 
const char* ssid = "Mike PIR";
const char* password = "123456789";
 int pir = D7;
 





void setup () {
 
pinMode(pir, INPUT);   // declare sensor as input
digitalWrite(D7, LOW);
Serial.begin(9600);
WiFi.begin(ssid, password);
 
while (WiFi.status() != WL_CONNECTED) {
 
delay(1000);
Serial.print("Connecting..");
 
}
 
}
 
void loop() {
 
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status




   int State = digitalRead(pir);
   Serial.println(State);
   
 if(State == HIGH) {
      Serial.println("Motion detected!");



HTTPClient http;  //Declare an object of class HTTPClient
http.begin("http://192.168.4.1/PirON");  //Specify request destination
int httpCode = http.GET();                                                                  //Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString();   //Get the request response payload
Serial.println(payload);                     //Print the response payload 
}
 
http.end();   //Close connection


delay(1000);
    }
    else {
HTTPClient http;  //Declare an object of class HTTPClient
http.begin("http://192.168.4.1/PirOFF");  //Specify request destination
int httpCode = http.GET();                                                                  //Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString();   //Get the request response payload
Serial.println(payload);                     //Print the response payload 
}
 
http.end();   //Close connection


delay(1000);
      delay(1000);
      }



 
}
 
 
}