Yes this is another timer/delay question, and another garage door opener…
However, all of the timer, delays and simple timer posts I’ve read have been for microcontrollers using sketches and C++. This project is using a Raspberry Pi and Node.js.
The project uses a magnetic switch for determining the status of the garage door (open/closed). The mobile device app has a labled value indicating the status of the door. The project also uses a 5v relay to power the garage door opener, and the mobile device app has a button to power the relay.
What I’m attempting to do: Have Blynk immediately notify the mobile device by push notification and email when the door is opened. Furthermore, I am attemting to have Blynk notify the moble device when the door has been open for 10 minutes, as a reminder.
The code below works and runs well except for notifying if the door has been open for 10 mins.
var Blynk = require('blynk-library');
var Gpio = require('onoff').Gpio;
var reed = new Gpio(17, 'in', 'both');
var relay = new Gpio(18, 'out');
var MinutesToWait=10;
var AUTH = 'AUTH TOKEN';
var blynk = new Blynk.Blynk(AUTH);
var v0 = new blynk.VirtualPin(0);
v0.on('write', function(param) {
if (param[0] == '1') {
relay.writeSync(1);
} else {
relay.writeSync(0);
}
console.log('V0:', param[0]);
});
var virtualPin = 1;
reed.watch(function(err,value){
if(value==0){
blynk.virtualWrite(virtualPin,"Closed");
console.log('Door Closed');
};
if(value==1){
blynk.virtualWrite(virtualPin,"Open");
console.log('Door Open');
blynk.notify("The door just opened!");
blynk.email("email@email.com", "Door Open", "Door Open");
//Everything above here works like a charm. Below does not. Conditions don't trigger
startCountingIdle();
};
//timing block
var timeEl = 0;
var timeOut = null;
function startCountingIdle(){
if(timeOut !== null){
clearTimeout(timeOut);
timeOut = null;
}else{
time();
}
}
function time()
{
timeOut = setTimeout(function()
{
timeEl = timeEl + 1;
if(timeEl==MinutesToWait*60 && value==1){
blynk.virtualWrite(virtualPin,"Open");
console.log('Door Open for 10 min');
blynk.notify("The door has been open for 10 min");
blynk.email("email@email.com", "Door Open for 10 min", "Door Open for 10 min");
timeEl=0;
}
else{
time();
}
}, 1000);
}
});
I’ve tested the above code in a node js ide and forced value ==1, and the second notification fires off after 10 min. I believe the hardware if functioning properly, because when opening the door(moving the magnet) the first notifications are sent, and the mobile device app correctly indicates door is open.
Any help with getting the second, time dependent, notifications to fire off is greatly appreciated. Thank you.