Motion detection using esp01 and rcwl-0516

Thread Starter

muhammadanas25

Joined Jun 21, 2023
20
Hello Helpers,

I need some help I have an Rcwl-0516 microwave sensor and an esp01 project basically I made a code but it's not working properly I wanted to make any logic like that if the person enters the room the device detects the person and turns on the Air conditioner using IR command and when a person leaves the room then it ac should be turn off but from my code it on the ac and turn off the ac but when the person in the room it automatically turn off the ac can you check my code or give any recommendation?
 

Ya’akov

Joined Jan 27, 2019
10,226
Welcome to AAC.

I suspect your problem is that the RCWL-0516 is not an occupancy sensor, it is a motion sensor. Once the person stops moving, the RCWL-0516 will not “see” them anymore. This is a classic problem.

There are some new options, very sophisticated, that are actual occupancy sensors, also using radar but with clever modulation. These can tell if there is a person present, not just someone or something moving. But, they are somewhat more expensive and much more complicated.

This one seems to be a more simplified version, and the price is not too bad. HiLink is a leader in this new technology, and I have used other modules from them. Whatever you do, though, you are going to have to work out how to detect presence instead of movement to make your project work.
 

Thread Starter

muhammadanas25

Joined Jun 21, 2023
20
Thank you Ya’akov,

But I have this rcwl-0156 sensor which I used to detect the presence of the person but this sensor only works when someone is moving and enters the room after that it gives a low trigger when someone is already in the but in the constant state of no movement can you check my code and give me the hint what can I do I use timer whenever the motion is triggered high the timer runs and when the sensor detect no movement the timer runs and when detect movement timer is stop I use timer for the turn off part of Airconditioner.

This is my code:

Mod: added Code Quotes

C-like:
//Libraries

#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>

#include <WiFiManager.h>

WiFiManager wm;

#include <Arduino.h>

#include <IRremoteESP8266.h>

#include <IRsend.h>

#include <Adafruit_Sensor.h>

#include <DHT.h>

#define WEBSERVER_H

#include <ESPAsyncTCP.h>

#include <ESPAsyncWebServer.h>

#include <AsyncElegantOTA.h>


AsyncWebServer server(80);

HTTPClient http;

//Variables

char msg[50];


WiFiClient wifiClient;

const uint16_t kIrLed = 4;

const int motionSensorPin = 5;

bool motionDetected = false;

// Timer configuration

const unsigned long lightTimeout = 300000;  // Time in milliseconds to turn off lights after inactivity

unsigned long lastMotionTime = 0;

String IP;


#define DHT_PIN 0

IRsend irsend(kIrLed);

const unsigned long eventInterval1 = 10000;

unsigned long previousTime1 = 0;

// const unsigned long eventInterval = 60000;

// unsigned long previousTime = 0;


uint16_t rawDataon[229] = {3050,3000,3050,4350,550,1650,550,500,600,1600,600,500,550,550,550,1600,600,1600,600,500,550,550,550,500,600,500,550,550,550,500,600,1600,600,1600,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,1650,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,1650,550,550,550,1600,600,500,550,550,550,550,550,500,550,550,550,550,550,1650,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,500,600,1600,550,1650,550,1650,550,1650,550,500,550,550,550,550,550,1650,550,500,550,550,550,550,550,500,600,500,550,550,550,550,550,500,550,1650,550,1650,550,1650,550,1650,550,500,550,550,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,550,550,550,500,550,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,1650,550,500,550,1650,550,1650,550,1650,550,1650,550,500,550,1650,550,1650,550};

uint16_t rawDataoff[229] = {3100,2950,3100,4350,550,1650,550,500,600,1600,600,500,550,550,550,1600,600,1600,600,500,550,550,550,1650,550,500,600,1600,550,550,550,550,550,1600,600,500,550,550,550,550,550,500,600,1600,600,500,550,1650,550,1650,550,1650,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,1650,550,500,550,550,550,550,550,1650,550,500,600,500,550,1650,550,550,550,500,600,500,550,550,550,550,550,500,600,500,550,550,550,1650,550,1650,550,1600,600,1600,550,550,550,550,550,550,550,1600,600,500,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,1650,550,1650,550,1650,550,1650,550,500,600,500,550,550,550,550,550,500,600,500,550,550,550,550,550,500,600,500,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,550,550,550,550,500,550,550,550,1650,550,550,550,1650,550,1600,550,550,550,1650,550,550,550,500,600,500,550,550,550,1650,550};

#define DHTTYPE DHT22

DHT dht(DHT_PIN, DHTTYPE);

float targetTemperature = 25.0;


//Start setup

void setup() {

  //  delay(60000);

  irsend.begin();

  Serial.println("All Systems initialize");

#if ESP8266

  Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);

#else  // ESP8266

  Serial.begin(115200, SERIAL_8N1);

#endif  // ESP8266

  //  Serial.begin(115200);

    pinMode(motionSensorPin, INPUT);

  bool res;


    res = wm.autoConnect("Motion device 3"); // password protected ap

     if(!res) {

        Serial.println("Failed to connect");

     }

    else {

        //if you get here you have connected to the WiFi  

        Serial.println("connected:)");

    }

   dht.begin();

    AsyncElegantOTA.begin(&server);    // Start AsyncElegantOTA

  server.begin();

  Serial.println("HTTP server started");


}


void loop() {



   int motionStatus = digitalRead(motionSensorPin);

    IP=WiFi.localIP().toString();

   float currentTemperature = dht.readTemperature();

   dtostrf(currentTemperature,2,2,msg);


   motiondetect(motionSensorPin);





}

void motiondetect(int motionPin)

{

  if (digitalRead(motionPin) == HIGH) {

     if (!motionDetected) {

    // Motion detected

    // delay(1000);

    // digitalWrite(lightPin, HIGH);  // Turn on the lights

    lastMotionTime = millis();

     motionDetected = true;

     irsend.sendRaw(rawDataon, 229, 38);

 

    Serial.println("Motion detection");    // Update the last motion detection time

    delay(1000);                   // Delay to avoid multiple rapid triggers

  }

  } if (digitalRead(motionPin) == LOW){

    if(motionDetected){



    // No motion detected

    Serial.println("Motion detection time: "+String(lastMotionTime));

    if (millis() - lastMotionTime >= lightTimeout) {

      //  delay(300000);

       irsend.sendRaw(rawDataoff, 229, 38);



      motionDetected = false;

      // digitalWrite(lightPin, LOW);  // Turn off the lights after timeout

      Serial.println("nO MOTION");

    }

    }

  }

  }
 
Last edited by a moderator:

Ya’akov

Joined Jan 27, 2019
10,226
As I said the sensor you are using is a motion sensor. When there is no motion it provides no signal, so anyone sitting still in the room will be ignored.

It’s not a presence sensor, and it can’t be used as one if you need to detect a static person. Your code doesn’t matter, you can’t change the nature of the sensor, it will not work if you want the air conditioner to run while the person is present because it can only be triggered by the person moving.

I’m not sure where we are not connecting.
 

ericgibbs

Joined Jan 29, 2010
21,390
hi m25,
The link states X-band radar-based motion sensor module developed
So it is not suitable for detecting non movement.

E
 
Top