PIR sensor+notify

Hi everyone, I want to when the turn on widget button, message received from the PIR sensor.

char auth[] = "12844f188a8446c126c2c6c8cedd6915";




/* HC-SR501 Motion Detector */
#define ledPin 13 
#define pirPin 5
int pirState;
int val = 0;
int x = 0;
SimpleTimer timer;

void setup()
{
   Serial.begin(115200);
  delay(10);
  Blynk.begin(auth, ssid, pass);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
 }

BLYNK_CONNECTED() {
      Blynk.syncVirtual(V0);
  }


BLYNK_WRITE(V0)
{
 int x = param.asInt();
 }


void loop()
{
   Blynk.run();
 
 val = digitalRead(pirPin);  
  if (val == HIGH) {           
    
    digitalWrite(ledPin, HIGH); 
    if (pirState == LOW) {
             pirState = HIGH;
    }
     if (x == 1){
  if (digitalRead(pirPin) == HIGH){
 Blynk.notify("MOTION detect!!!");   
  }
}
  } else {
    digitalWrite(ledPin, LOW); 
    if (pirState == HIGH){
            pirState = LOW;
    }
  }
  }

What am I doing wrong?

Please properly format sketch above. Otherwise it is difficult to read.

@gydota, i would like to help, but can you straighten up the language a little bit? i do not understand what you want :frowning:

@Gunner, can i steal this picture? :smile:

1 Like

Yes :+1:

I tried, but bad know English :smile:

hm, then maybe @Dmitriy, could you help please?

Code formatting is a mess to be honest. But there is at least one obvious bug: your global x variable is never gets assigned.
Change this:

BLYNK_WRITE(V0)
{
 int x = param.asInt();
}

to this:

BLYNK_WRITE(V0)
{
 x = param.asInt();
}

I believe he wants to enable or disable the PIR sensing with a button (in switch mode). But too much happening in the main loop.

actually, it doesn’t seems to happen too much in main loop. there are some condition checks, that is hardly needs to much cpu time.

contrary to what a lots of people think, it is not necessary causing errors if you have lots of code in main loop. it depends what it does. for example in my current project i have around 500 lines of code in my main loop, and it works flawlessly with blynk! i think we should speak about these theories in some dedicated topics…

can we call that picture FTFC?
i guess you can figure out the meaning… :wink:

1 Like

I thank everyone for the advice.
Made by this method and it worked :slight_smile:

 BLYNK_WRITE(V0)
{
 x = param.asInt();
 }


void loop()
{
   Blynk.run();
   pir();

  val = digitalRead(pirPin);  
  if (val == HIGH) {            
    
    digitalWrite(ledPin, HIGH);  
    if (pirState == LOW) {
             pirState = HIGH;
    }
     } else {
    digitalWrite(ledPin, LOW); 
    if (pirState == HIGH){
            pirState = LOW;
    }
  }
}
  void pir(){
  if (x == 1)
  {
  if (digitalRead(pirPin) == HIGH){
 Blynk.notify("MOTION detect!!!");   
  }
  }
  }
3 Likes

hi mr …what is the board type? nodemcu?

yes

why is not working for me…pir cannot response.can you share wiring schema?
thx

try to use this code here

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

char auth[] = "**************************";
char ssid[] = "*********";
char pass[] = "********";
char server[] = "192.168.1.100";// your local server, otherwise it should be commented

#define ledPin 16 
#define pirPin 14

int pirState;
int val = 0;
int x = 0;

SimpleTimer timer;

BLYNK_CONNECTED() {
      Blynk.syncVirtual(V0);
  }

BLYNK_WRITE(V0){
 x = param.asInt();
 }

void PIRval(){
val = digitalRead(pirPin);
    if (val == HIGH) {
      digitalWrite(ledPin, HIGH);  
    if (pirState == LOW){
      pirState = HIGH;
    }
      else {
        digitalWrite(ledPin, LOW); 
      }
    if (pirState == HIGH){
      pirState = LOW;
  }
 }
}

  void pir(){
  if (x == 1){
    if (digitalRead(pirPin) == HIGH){
 Blynk.notify("ALARM!!!");
 }
    }
  }

void setup(){
  Blynk.begin (auth, ssid, pass, server);//local server
   // You can change server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);

  timer.setInterval(1000L, PIRval);
   }

void loop(){
   Blynk.run();
   timer.run();
}

16= D0
14= D5

im edit

char server[ ]= "192.168.1.100"; 

to

 //char server[ ] = "192.168.1.100"; 

and

Blynk.begin (auth, ssid, pass, server);

edit to (because im not use local server)

Blynk.begin (auth, ssid, pass);

but im move pin to D0 for led and D5 for pir input…R3 is blank (not use)

Why is not working for too me with nodemcu lolin?
something wrong?
thx for advice

I do not use resistors with PIR sensor

i will try again with your suggestion…can provide the wiring advice that works normally?

I will come to the house in the evening and again check the scheme for work capacity.

made corrections and checked, now everything is working :slight_smile:

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

char auth[] = "**************************";
char ssid[] = "*********";
char pass[] = "********";
char server[] = "192.168.1.100";// your local server, otherwise it should be commented

#define ledPin 12 
#define pirPin 14

int pirState;
int val;
int x;

SimpleTimer timer;

BLYNK_CONNECTED() {
      Blynk.syncVirtual(V0);
  }

BLYNK_WRITE(V0){
 x = param.asInt();
 }

void PIRval(){
val = digitalRead(pirPin);
    if (val == HIGH) {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

  void pir(){
  if (x == 1){
    if (digitalRead(pirPin) == HIGH){
 Blynk.notify("ALARM!!!");
 }
    }
  }

void setup(){
  Blynk.begin (auth, ssid, pass, server);//local server
   // You can change server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);

  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);

  timer.setInterval(1000L, PIRval);
 timer.setInterval(1000L, pir);
   }

void loop(){
   Blynk.run();
   timer.run();
}


https://youtu.be/uVXJXGBoCHY

1 Like

wow thx very much…