Home WIFI automatization with PIR motion sensor and light LDR sensor

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxx-xxxx";
char pass[] = "xxxxxxxxxx"; 

#define rele D7 
#define pirPin D8 // Input for HC-S501
int pirValue; // Place to store read PIR Value
int portarele = D0; //digital rele output from LDR
int portaLDR = A0; //Analogical LDR input  
void setup()/
{
  Serial.begin(115200);
  delay(10);
  Blynk.begin(auth, ssid, pass);
  pinMode(rele, OUTPUT);
  pinMode(portarele, OUTPUT); //
  pinMode(pirPin, INPUT);
  digitalWrite(rele, LOW);
}
void loop()
{
  ldr();
  getPirValue();
  Blynk.run();
}
/***************************************************
 * Get PIR data
 **************************************************/
void getPirValue(void)
{
  pirValue = digitalRead(pirPin);
  if (pirValue) 
  { 
    Serial.println("==> Motion detected");
    Blynk.notify("T==> Motion detected");  
  }
  digitalWrite(rele, pirValue);
  delay(500)
}
void ldr()  
{  

  
  int estado = analogRead(portaLDR);  //Read LDR  

  if (estado < 800)    
  {  
digitalWrite(portarele, HIGH);
Serial.println(portaLDR, DEC);
Blynk.notify(portaLDR, DEC); 
  }  
  else    
  {  
digitalWrite(portarele, LOW);  
Serial.println(portaLDR, DEC);
Blynk.notify(portaLDR, DEC); 
  }  

Hi, people, I try to make an algorithm for home automatization using a motion sensor and a light LDR sensor.
I made this code and de PIR motion sensor works well, but for home automatization, I have two problems that I can’t solve:
1: The motion sensor is sensitive, that made my relay, that control my lamp, go on and off many times. The best solution that I have to solve this was to put a large delay after my lights go on. But it isn’t a good solution because I lose de control from the ESP8266 and also would be necessary to have, for example, 1 min movement, to have 1 min lights on.
What I want is, when some movement is detected that the light keeps on for 1 min, and then go off, if there are no more movements. It also can’t put my WIFI control out of working.
2: The second problem is that it isn’t really smart to keep de lights on every movement of the day. Then I ask you, isn’t any possibility to turn the movement identification in a specific time of the day? Haw?
And with my LDR sensor, I actually have the same problem explain before. But also that my code doesn’t work correctly when I put the LDR light sensor.

Could anyone help me? I would be really grateful and post the final project in the community library.

Hello and welcome to the Blynk Forum. We don’t really exist to help write programs, rather we are here to help promote Blynk and assist others learn how Blynk commands work etc.

You have some of the basics apparent in your code, however what you need is timers. Both to remove some of the function calls from the main loop, as they are running hundreds of times a second… and to assist with timed action with the PIR and the lights.

Study up on this link for SimpleTimer, which has been “built into” the Blynk library under the name BlynkTimer. Also search this forum for references like BlynkTimer and delays without blocking, PIR, etc.

https://playground.arduino.cc/Code/SimpleTimer

BlynkTimer is the same as the widely used SimpleTimer library, in that it works more elegantly than manually manipulating millis() and it is NOT dependent on a connection to a Blynk Server, and also fixes some minor issues with the original library.

A typical Interval Timer layout includes a definition, timer setup (pointing to a function) and a timer call in the void loop():

BlynkTimer Mytimer; // Sets up a timer object named Mytimer (could be any name)

Mytimer.setInterval(1000L, MyFunction);  // Goes in Setup() and will call the void MyFunction() every 1000 milis (one second - adjust to your needs).  Blynk Timer allows up to 16 of these same object named timers pointing to differing functions

void MyFunction() 
{
// Mytimer runs stuff here every 1000 millis (one second)
}

void loop()
{
Blynk.run();
Mytimer.run();  // Goes in the void loop() so the BlynkTimer library can run.
}
1 Like

Sorry for the inconvenient.
I’ll study this that you show me. thanks

1 Like