Motion sensor with esp01 detection problem

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?

Code:

//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");
    }
    }
  }
  }



Your topic has been moved to a more suitable location on the forum. See the first and third post in Uncategorized - Arduino Forum.

As always, the way to approach this is not to try to solve the entire problem in one go!

Instead, break it down into small steps.
Thoroughly test each step before moving onto the next.

eg,

  1. detect person entering & leaving room;
  2. turn Air conditioner on/off using IR commands;
  3. An HTTP server is a whole project in itself!

Get each of those working independently first, then bring them together.

Please give a link to the actual device you're using and, preferably, a datasheet.

It seems that this gives a simple logic high/low to indicate if motion is detected or not?

So start without any Arduino at all, and just use a meter to check that your unit is actually giving the output that you expect/require.

Then you could move on to a simple Arduino sketch that just lights an LED when movement is detected, and turns it off with no movement.

Keep this as part of your full system so that you can see what's happening.

etc, etc, ...
You could also

I checked each but the problem is that,

I just want it when a person enters the room it triggered the ac on and when he left the room it turns off but in my case, it gets turned off after a specific time I want to turn it off when the person is not in the room only

So you've checked that you are getting a "no motion" signal from the sensor as soon as the person leaves the room?

Have you looked through your code to see what could cause this issue?

Add serial output and/or lighting LEDs to see what your code is actually doing...

Debugging is a key part of software development - you really need to learn how to do it.
Just like programming itself, it's a problem solving exercise - ask yourself:

  • What should my code be doing ?
  • What is my code actually doing?
  • What tests/investigations can I do to find out what's happening?

How To Debug

eg, what is this code doing?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.