Motion detection to turn lights on or off

I’m using a nodemcu product with controls the lights in my house using digital pins, tasker, and restask plugin. i have everything working properly. Its turns the lights on or off via location setting on tasker. what i would like to do is get some type of motion sensor so when ever I’m in a room that specific light will turn on or off. can anyone help me?

@Costas, @Dmitriy… can you guys help?

I’ll post my code here a little while later and explain how it works

here is code i run on an esp controlling the lights on my stairs. they can be turned on by blipping the power to the esp and turned off by blipping power again. they are controlled via RTC when my alarm is armed and lights are in auto mode. and the are also motion sensitive via a pir sensor on the ceiling:
https://www.adafruit.com/product/189

here is my code: its not the neatest solution ever… but it works for me:

#include <SPI.h>
#include <Ethernet.h>
//#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <EEPROM.h>
#include <WidgetRTC.h>
bool isFirstConnect = true;
int addr = 0;
int state = 0;
int oldstate = 0;
int latch = 0;
int loungeon = 0;
int loungeoff = 0;
int h = 0;
int pirlatch = 0;



char auth[] = "xxx";
SimpleTimer timer;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V5);

void setup()
{

  EEPROM.begin(512);
  pinMode(14, OUTPUT);
  pinMode(5, INPUT);
  int value = EEPROM.read(addr);  // toggle the output state every time you turn on the esp so lights can be controlled without the app.
  if (value == 1)
  {
    digitalWrite(14, 0);
    EEPROM.write(addr, 0);
    EEPROM.commit();
  }

  if (value == 0)
  {
    digitalWrite(14, 1);
    EEPROM.write(addr, 1);
    EEPROM.commit();
  }

  Blynk.begin(auth, "xx", "xxx");
  while (Blynk.connect() == false)
  {
    // Wait until connected
  }

  delay(200);
  rtc.begin();
  state = digitalRead(14);
  if (state == 1)
  {
    Blynk.virtualWrite(V22, HIGH);
  }
  else
  {
    Blynk.virtualWrite(V22, LOW);
  }
  setSyncInterval(20);
  timer.setInterval(5000L, hourDisplay);
  Blynk.syncVirtual(V20);
  Blynk.syncVirtual(V21);
 
}

void hourDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  h = hour();
  Blynk.syncVirtual(V23);
  state = digitalRead(14);
  if (state == 1)
  {
    Blynk.virtualWrite(V22, HIGH);
  }
  else
  {
    Blynk.virtualWrite(V22, LOW);
  }

}


BLYNK_WRITE(V22)// Lounge lights toggle from push button in app
{
  if ((param.asInt() == 1) )//&& (latch2 == 0)
  {
    digitalWrite(14, !digitalRead(14));
    delay(200);
    state = digitalRead(14);
    if (state == 1)
    {
      Blynk.virtualWrite(V22, HIGH);
    }
    else
    {
      Blynk.virtualWrite(V22, LOW);
    };
    EEPROM.write(addr, state);
    EEPROM.commit();
  }
  Blynk.run();
}

BLYNK_WRITE(V20)
{

  loungeon = param.asInt(); // get the switch on time from the slider
  //Blynk.syncVirtual(V3);
  Blynk.run();

}
BLYNK_WRITE(V21)
{

  loungeoff = param.asInt();  // get the switch off time from the slider
  ///Blynk.syncVirtual(V3);
  Blynk.run();

}

BLYNK_WRITE(V23)//auto or manual on switch from app
{

  if (param.asInt() == 1)
  {
    automatic();
  }


}





void automatic()
{
  h = hour();
  if ((h >= loungeon) && (h < loungeoff)) {
    digitalWrite(14, HIGH);
    oldstate = state;
    state = digitalRead(14);
    if(oldstate == !state){
    if (state == 1) {
      Blynk.virtualWrite(V22, HIGH);
    }
    else {
      Blynk.virtualWrite(V22, LOW);
    }
    EEPROM.write(addr, state);
    EEPROM.commit();
    
    }
Blynk.run();
  }

  else {
    digitalWrite(14, LOW);
    oldstate = state;
    state = digitalRead(14);
    if(oldstate == !state){
    if (state == 1) {
      Blynk.virtualWrite(V22, HIGH);
    }
    else {
      Blynk.virtualWrite(V22, LOW);
    }
    EEPROM.write(addr, state);
    EEPROM.commit();

  }}
Blynk.run();

}

void loop()
{
  int PIR = digitalRead(5);               // check to see if the PIR motion sensor picked up any movement(my PIR has its own timer, so after a few min. the lights turn off again)
  if((PIR == 1) && (pirlatch == 0)){
    digitalWrite(14, HIGH);
    pirlatch = 1;
    }
  if ((PIR == 0) && (pirlatch == 1)){
    pirlatch = 0;
    digitalWrite(14, LOW);
  }
  
    
  Blynk.run();
  timer.run();
}

so the way i have my system set up is i have 3 esp’s linked to the blynk app. I’m using the gpio outputs 1,2,4, and 5 in order to turn my lights on or off. i accomplished this by using the blynk app, tasker, and restask plugin. so right i have i have set so that if I’m approaching my house it’ll send a [’‘1’’] signal to trigger the gpio port and if i leave the area it sends [’‘0’’] to turn off the port. i just purchased a motion detection module that works with the esp and i want to be able to send the same gpio ports a 0 or 1 signal if there is motion or no motion in that room. how can make this possible?