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
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();
}
You need to edit post so that your code displays correctly:
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.
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
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");
}
}
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();
}
Woha, dooooode… chill on the 5-0 man (sorry, my best and only attempt at hippy imitation also works for Canadian stoner… again, not from experience )
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.
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
Tonight i tested it with just the board and no pir sensors attached and it still gives me motion detected notifications on my Phone… with no sensors… see my video https://youtu.be/EuCqD1I6vQo
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!