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