BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Alarm with multiple pir sensors

#1

Hi i have use a code from ErfanDL for a home alarm Security alarm system with works fine but i want to add more pir sensors tot the code how do i adjust te code or should this be done in wirering multiple pir sensors to one pin connected?

hope you can help me
i run the code on a nodemcu v3

0 Likes

#2

i have adjusted the code but it sometimes work and somethimes not? (i mean the notifications are Always from pirPin and some thimes from pirPin_1) i would also like some serial prints in it…

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial 

char auth[] = "ad749283f7b54285b49c024d6f92f889";
char ssid[] = "ziggo-ap-5788707";
char pass[] = "28,a:B^S[7ZJP";
char server[] = "192.168.178.33";

#define ledPin D4 
#define pirPin D1
#define pirPin_1 D2


int pirState;
int val;
int x;

SimpleTimer timer;

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

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

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

  void pir(){
  if (x == 1){
    if (digitalRead(pirPin) == HIGH){
       Blynk.notify("ALARM On Pirsensor");
      if (digitalRead(pirPin_1) == HIGH){
       Blynk.notify("ALARM On Pirsensor 1");
         }
    }
  }
  }
void setup(){
  Blynk.begin (auth, ssid, pass, server, 8080);//local server
   // You can change server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);

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

  timer.setInterval(1000L, PIRval);
   timer.setInterval(1000L, pir);
}
void loop(){
   Blynk.run();
   timer.run();
}
0 Likes

#3

You need to edit post so that your code displays correctly:

Blynk%20-%20FTFC

Also, you’ve asked the same question in two different topics. That causes problems when people start responding to both topics - it gets very confusing.
So, I’ve deleted your post in the other topic.

Pete.

0 Likes

#4
void PIRval(){ 
val = digitalRead(pirPin); 
val = digitalRead(pirPin_1);    // dont you want this to be a different varible?
if (val == HIGH) { 
    digitalWrite(ledPin, HIGH); 
    } 
    else { 
        digitalWrite(ledPin, LOW); 
    } 
}

Im not a natural C coder, but it looks like you are overwriting val

0 Likes

#5

@lvennard is spot on.

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

Note: val and val_1 can be declared local to the PIRval() function.

I assume V0 and x correspond to some kind of “enable” button. You may want to check the curly bracket nesting in the pir() function. The way it’s currently written, the “ALARM On Pirsensor 1” notification will only be sent if both pirPin and pirPin_1 are HIGH. If you want the notifications sent independent of one another consider …

void pir(void){
   if (x == 1) {
      if (digitalRead(pirPin) == HIGH)
         Blynk.notify("ALARM On Pirsensor");
      if (digitalRead(pirPin_1) == HIGH)
         Blynk.notify("ALARM On Pirsensor 1");
   }
}
1 Like

#6

:slight_smile: thanks brotha…

it goes to show there are 30 ways to write the same code… i had a bunch of fun writing this section .

void PIRval(){ 
val = digitalRead(pirPin); 
val1 = digitalRead(pirPin_1);    
if (val != HIGH) && (val1 != HIGH) // here im asking if either val and val1 see any hippies THEY TELL ME NO
{
// chill out... take a chill pill and relax... no hippies detected.
{
else {  // one of the two pir's has detected hippies or i wouldnt be reading this. 
//  freak out .. god damn hippies detected.. sound some alarms
// is val high?  serial print "hell ya, pir detected hippies"
// is val1 high?  serial print "holy crap, i see hippies on pir1"
// anything else i see in this section of code will react to the hippies
// deploy machineGuns();
// deploy rockMusic();
// deploy poLice();
}
0 Likes

#7

Woha, dooooode… chill on the 5-0 man (sorry, my best and only attempt at hippy imitation :stuck_out_tongue_winking_eye: :peace_symbol: also works for Canadian stoner… again, not from experience :rofl: )

1 Like

#8

Did you happen to compile it? I think you’re missing a pair of outer parenthesis. One curly bracket is backwards. And you’re missing a curly bracket at the end. But other than that it looks great. :wink:

void PIRval(){ 
   int val = digitalRead(pirPin); 
   int val1 = digitalRead(pirPin_1);
   // v                               v <- need outer parenthesis
   if ((val != HIGH) && (val1 != HIGH)) // here im asking if either val and val1 see any hippies THEY TELL ME NO
   {
      // chill out... take a chill pill and relax... no hippies detected.
   }  // { <- curly bracket is backwards
   else
   {  // one of the two pir's has detected hippies or i wouldnt be reading this. 
      //  freak out .. god damn hippies detected.. sound some alarms
      // is val high?  serial print "hell ya, pir detected hippies"
      // is val1 high?  serial print "holy crap, i see hippies on pir1"
      // anything else i see in this section of code will react to the hippies
      // deploy machineGuns();
      // deploy rockMusic();
      // deploy poLice();
   }
}  // <- missing curly bracket
1 Like

#9

heck no! crown royal wont let me compile a dang thing right now.

0 Likes

#10

“Don’t drink and code.”

1 Like

#11

Hello thanks for your help!

and the fun of HIGH hippies :rofl::joy:

I adjusted the code and it works fine now thanks to you guys…:heart:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial 

char auth[] = "ad749283f7b54285b49c024d6f92f889";
char ssid[] = "ziggo-ap-5788707";
char pass[] = "28,a:B^S[7ZJP";
char server[] = "192.168.178.33";

#define ledPin D4 
#define pirPin_1 D1
#define pirPin_2 D2
#define pirPin_3 D3

int pirState;
int val;
int x;

SimpleTimer timer;

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

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

void PIRval(){
int val_1 = digitalRead(pirPin_1);
int val_2 = digitalRead(pirPin_2);
int val_3 = digitalRead(pirPin_3);
    if ((val_1 == HIGH) || (val_2 == HIGH) || (val_3 == HIGH))  {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

  void pir(void){
   if (x == 1) {
      if (digitalRead(pirPin_1) == HIGH)
         Blynk.notify("ALARM On Pirsensor 1");
      if (digitalRead(pirPin_2) == HIGH)
         Blynk.notify("ALARM On Pirsensor 2");
      if (digitalRead(pirPin_3) == HIGH)
         Blynk.notify("ALARM On Pirsensor 3");
      }
    }
void setup(){
  Blynk.begin (auth, ssid, pass, server, 8080);//local server
   // You can change server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);

  pinMode(ledPin, OUTPUT);
  pinMode(pirPin_1, INPUT);
  pinMode(pirPin_2, INPUT);
  pinMode(pirPin_3, INPUT);

  timer.setInterval(500L, PIRval);
   timer.setInterval(1000L, pir);
  }
void loop(){
   Blynk.run();
   timer.run();
}
0 Likes

#12

Nice! Minor point … you can deprecate the following,

int pirState;
int val;

Joe

1 Like

#13
deploy chamPagne();

:slight_smile:

1 Like

#14

i love chamPagne… i would like to add some serial print information:
for connected to my server
for pir sensor
for door reed sensor ( i have added)

the v0 virtual button to set the alarm is there also a posability to also create a real button but also the virtual one will work?

greetings

0 Likes

#15
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial 

char auth[] = "ad749283f7b54285b49c024d6f92f889";
char ssid[] = "ziggo-ap-5788707";
char pass[] = "28,a:B^S[7ZJP";
char server[] = "192.168.178.33";

#define ledPin D4 
#define pirPin_1 D1
#define pirPin_2 D2
#define pirPin_3 D3
int pinSpeaker = D5;
int switchReed = D0; // reed contact to D0 See link for connections
int ledOpen = D6;    // If door is open led on D6 will burn
int ledClose = D7;   // If door is open led on D7 will burn

int x;

SimpleTimer timer;

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

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

void PIRval(){
int val_1 = digitalRead(pirPin_1);
int val_2 = digitalRead(pirPin_2);
int val_3 = digitalRead(pirPin_3);
    if ((val_1 == HIGH) || (val_2 == HIGH) || (val_3 == HIGH))  {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

  void pir(void){
   if (x == 1) {
      if (digitalRead(pirPin_1) == HIGH)
         Blynk.notify("ALARM On Pirsensor 1");
      if (digitalRead(pirPin_2) == HIGH)
         Blynk.notify("ALARM On Pirsensor 2");
      if (digitalRead(pirPin_3) == HIGH)
         Blynk.notify("ALARM On Pirsensor 3");
         }
    }
void setup(){
  Blynk.begin (auth, ssid, pass, server, 8080);//local server
   // You can change server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin_1, INPUT);
  pinMode(pirPin_2, INPUT);
  pinMode(pirPin_3, INPUT);
  pinMode(pinSpeaker, OUTPUT);
  pinMode(switchReed, INPUT);
  pinMode(ledOpen, OUTPUT);
  pinMode(ledClose, OUTPUT);

  timer.setInterval(500L, PIRval);
   timer.setInterval(1000L, pir);
  }
BLYNK_WRITE(V2)
{
if(ledClose==HIGH)
  {
     Blynk.virtualWrite(V2, HIGH);
  }
  else
  {
     Blynk.virtualWrite(V2, LOW);
  }
}
void loop(){
   Blynk.run();
   timer.run();

   if (digitalRead(switchReed) == HIGH) {
    digitalWrite(ledOpen, LOW);
    digitalWrite(ledClose, HIGH);
    Serial.println("Your Door is Closed");
    Blynk.virtualWrite(V2, HIGH);
  }
  else {
    digitalWrite(ledOpen, HIGH);
    digitalWrite(ledClose, LOW);
    Serial.println("Your Door is Open");
    Blynk.virtualWrite(V2, LOW);
  }
 }
0 Likes

#16

Tonight i tested it with just the board and no pir sensors attached and it still gives me motion detected notifications on my Phone…:smirk: with no sensors… see my video https://youtu.be/EuCqD1I6vQo

0 Likes

#17

Most likely cause your input pins are floating.

0 Likes

#18

You’re doing some very strange stuff in your code…

BLYNK_WRITE(V2)
{
if(ledClose==HIGH)
  {
     Blynk.virtualWrite(V2, HIGH);
  }
  else
  {
     Blynk.virtualWrite(V2, LOW);
  }
}

I’ve no idea what type of widget is connected to pin V2, but the BLYNK_WRITE(V2) function is called automatically by Blynk whenever the value of whatever is attached to V2 changes. But’ you’re then updating V2 from within that function. This isn’t good practice, as it could create an infinite loop of V2 updating itself. The Blynk library seems to have a way of stopping this from happening, but it can’t be a good piece of coding.

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

   if (digitalRead(switchReed) == HIGH) {
    digitalWrite(ledOpen, LOW);
    digitalWrite(ledClose, HIGH);
    Serial.println("Your Door is Closed");
    Blynk.virtualWrite(V2, HIGH);
  }
  else {
    digitalWrite(ledOpen, HIGH);
    digitalWrite(ledClose, LOW);
    Serial.println("Your Door is Open");
    Blynk.virtualWrite(V2, LOW);
  }
 }

In your void loop you’re checking the state of a digital pin every single time the loop executes (potentially thousands of times per second) and, more importantly, you’re doing a Blynk.virtualWrite to pin V2 every single time your void loop executes!

You have to read this document:

And one final comment, don’t do this:

Use variable names that mean something!

Pete.

0 Likes

#19

sorry the code i am using is this one…

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial 

char auth[] = "ad749283f7b54285b49c024d6f92f889";
char ssid[] = "ziggo-ap-5788707";
char pass[] = "28,a:B^S[7ZJP";
char server[] = "192.168.178.33";

#define ledPin D4 
#define pirPin_1 D1
#define pirPin_2 D2
#define pirPin_3 D3


int x;

SimpleTimer timer;

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

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

void PIRval(){
int val_1 = digitalRead(pirPin_1);
int val_2 = digitalRead(pirPin_2);
int val_3 = digitalRead(pirPin_3);
    if ((val_1 == HIGH) || (val_2 == HIGH) || (val_3 == HIGH))  {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

  void pir(void){
   if (x == 1) {
      if (digitalRead(pirPin_1) == HIGH)
         Blynk.notify("ALARM On Pirsensor 1");
      if (digitalRead(pirPin_2) == HIGH)
         Blynk.notify("ALARM On Pirsensor 2");
      if (digitalRead(pirPin_3) == HIGH)
         Blynk.notify("ALARM On Pirsensor 3");
      }
    }
void setup(){
  Serial.begin(9600);
  Blynk.begin (auth, ssid, pass, server, 8080);//local server
  // You can change server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  while (!Blynk.connect()) { 
  //Wait until connected
  }
  BLYNK_CONNECTED();
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin_1, INPUT);
  pinMode(pirPin_2, INPUT);
  pinMode(pirPin_3, INPUT);

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

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

#20

I can not get this working i think the problem is with x = param.asInt(); can anybody explane what this is doing?

wen i activate my arm/disarm button on v0 it imedietly detects motion even without motion sensors?

hope someone can help and point me on what is wrong…

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

char auth[] = "c25b0d1f67904865aba9cd5cb70e1a43";
char ssid[] = "ziggo-ap-5788707";
char pass[] = "28,a:B^S[7ZJP";
char server[] = "192.168.178.33";

#define ledPin D5 
#define pirPin_1 D1
#define pirPin_2 D2
#define pirPin_3 D6

int x;

SimpleTimer timer;

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

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

void PIRval(){
int val_1 = digitalRead(pirPin_1);
int val_2 = digitalRead(pirPin_2);
int val_3 = digitalRead(pirPin_3);
    if ((val_1 == HIGH) || (val_2 == HIGH) || (val_3 == HIGH))  {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

  void pir(void){
   if (x == 1) {
      if (digitalRead(pirPin_1) == HIGH)
         Blynk.notify("ALARM On Pirsensor 1");
      if (digitalRead(pirPin_2) == HIGH)
         Blynk.notify("ALARM On Pirsensor 2");
      if (digitalRead(pirPin_3) == HIGH)
         Blynk.notify("ALARM On Pirsensor 3");
      }
    }
void setup(){
  Blynk.begin (auth, ssid, pass, server, 8080);//local server
   // You can change server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin_1, INPUT);
  pinMode(pirPin_2, INPUT);
  pinMode(pirPin_3, INPUT);

  timer.setInterval(1200L, PIRval);
   timer.setInterval(1000L, pir);
  }
void loop(){
   Blynk.run();
   timer.run();
}
0 Likes