I’m fighting with what looks to be a really simple problem. At below code I need that <blynk.notify>
work inside <PIR1.watch> function, but ir works only outside, as you see in my comments.
Am I doing a big or small mistake? (or too many)?
Code works just ok, it reads a PIR sensor value and I would like to notify an alert just when a virtual button is ON (kind of alarm start/stop button).
Now I just need to know how deal with <blynk.notify>.
Any help appreciated. Thanks in advance.
var Blynk = require('blynk-library');
var AUTH = 'yabadaba';
var blynk = new Blynk.Blynk(AUTH, options = {
connector : new Blynk.TcpClient()
});
var v5 = new blynk.VirtualPin(5); // Alarm button
blynk.on('connect', function() {
blynk.syncVirtual(5);
notify();
});
v5.on('write', function(param5) { // Watches for V5 Button
console.log('V5:', param5[0]); // prints value to CLI
});
var Gpio = require('/usr/lib/node_modules/onoff').Gpio
pir1 = new Gpio(24, 'in', 'both'); // Pi BCM # 24 - sensor PIR1
function notify()
{
blynk.notify('Alert!'); ====> IT WORKS HERE
pir1.watch(function(err, value, param5,blynk) {
blynk.notify('AEEEHH'); ====> DOES NOT WORK HERE
if (err) throw err;
if ((value == 1) & (param5 == 0)) {
console.log('PIR:', value);
}
else if ((value == 1) & (param5 == 1)) {
console.log('PIR:', value);
blynk.notify('Alert!'); ====> DOES NOT WORK HERE
}
else {
console.log('PIR:', value);
}
});
}
I don’t think the param value is carried outside of its assigned function. You would need to transfer it to a variable first.
This looks like you are triggering the function on rise and fall of a sensor trigger… this could cause rapid double triggers and notifications have a 5 second limit between such (Looks like that was recently updated from previous 15 seconds )
Param and pir1 are working well, console shows PIR sensor states changing as we pass near it, as well changes on virtual button value.
I just don’t understand why “blynk.notify” does not work inside <pir1.watch>, just outside.
Ok, I’m at work right now, will try to understand JS better to know what is happening, and will post results after reach them (I hope).
Thanks a lot, guys.
I’m suffering with Javascript but at least code is working right now, maybe it is not perfect but works.
When alarm button is off, no notifications sent, after turn it on I get max 3 notifications and it stop to
send them (to avoid message overload). If I turn alarm button off and on again and PIR sensor get new movements it will send more 3 notifications max.
Thanks a lot for your support, Gunner (Gicus too). Change the status for “solved” if you want.
In time: now I know that only one ampersand works with numbers but is discouraged, so I’m using 2 now. Learned a little bit of JS, thanks!
var Blynk = require('blynk-library');
var AUTH = 'yabadaba';
var blynk = new Blynk.Blynk(AUTH, options = {
connector : new Blynk.TcpClient()
});
var v5 = new blynk.VirtualPin(5); // Alarm button
notify();
function notify() {
blynk.notify('Alert!');
}
blynk.on('connect', function() {
blynk.syncVirtual(5);
});
var Gpio = require('/usr/lib/node_modules/onoff').Gpio
pir1 = new Gpio(24, 'in', 'both'); // Pi BCM # 24 - sensor PIR1
v5.on('write', function(param) { // Watches for V5 Button
// console.log('V5:', param[0]); // prints value to CLI
counter = 0;
pir1.watch(function(err, value) {
if (err) throw err;
if ((value == 1) && (param == 1)) {
notify();
counter++;
if (counter == 3) {//notifies only 3 times (turn off/on alarm button to reactivate)
param = 0;
}
// blynk.email('PIR Alert', 'Movement');//email optional
}
});
});
Comparing data with different types could give you an unexpected results.
Comparing string with a number JavaScript converts both variables to a number and then do the comparison.
Case Value
3 < 13 true
3 < “13” true
3 < “Billy” false
3 > “Billy” false
3 == “Billy” false
“3” < “13” false
“3” > “13” true
“3” == “13” false
Thank you a lot, vowukur!
I’m not proficient on programming and totally new to JS, so I appreciate your attention and tips! I’ll pay attention on this from now on, now I see that JS has its own idiosyncrasies and its not obvious.
Kind regards.