setTimeout in node.js

Hi friends. in below sketch I want to running the V9 only for 1 second. in the below code when I press V9 button the command runs continuously. how can I running the V9 only for 1 second ?

the code:

setTimeout(function(){
var Blynk = require('blynk-library');

var AUTH = '81cc3c5ee9754be8ab6fa4a67d8d3xe0';
var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient( options = { addr:"10.5.51.5", port:8080 } ) });

var exec = require('child_process').exec, child;
var v0 = new blynk.VirtualPin(0);
var v1 = new blynk.VirtualPin(1);
var v2 = new blynk.VirtualPin(2);
var v3 = new blynk.VirtualPin(3);
var v5 = new blynk.VirtualPin(5);
var v6 = new blynk.VirtualPin(6);
var v7 = new blynk.VirtualPin(7);
var v8 = new blynk.VirtualPin(8);
var v9 = new blynk.VirtualPin(9);
var slider = new blynk.VirtualPin(4);
var writeVal = '';

//volume control using slider
slider.on('write', function(param) {
	child = exec('amixer set PCM ' + param[0] + '%',
		function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
             console.log('exec error: ' + error);
        }
    });
});

//turn on squeezelite turn off others
v1.on('write', function(param) {
	if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/reboot.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'squeezelite ON'; 
	}
	else if (param[0] == '0') {
		child = exec('sudo /etc/init.d/squeezelite stop',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'squeezelite OFF';
	}
});


v2.on('write', function(param) {
	if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/lcdon.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'shairport ON'; 
	}
	else if (param[0] == '0') {
		child = exec('sudo systemctl stop shairport-sync',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'shairport OFF';
	}
});

v3.on('write', function(param) {
	if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/lcdoff.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'analog ON'; 
	}
	else if (param[0] == '0') {
		child = exec('sudo killall -15 ecasound',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'analog OFF';
	}
});

v5.on('write', function(param) {
	if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/dsloff.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'analog ON'; 
	}
	else if (param[0] == '0') {
		child = exec('sudo killall -15 ecasound',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'analog OFF';
	}
});

v6.on('write', function(param) {
	if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/lcdon.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'analog ON'; 
	}
	else if (param[0] == '0') {
		child = exec('sudo killall -15 ecasound',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'analog OFF';
	}
});

v7.on('write', function(param) {
	if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/pcoff.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'analog ON'; 
	}
	else if (param[0] == '0') {
		child = exec('sudo killall -15 ecasound',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'analog OFF';
	}
});

v8.on('write', function(param) {
	if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/reboottp.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'analog ON'; 
	}
	else if (param[0] == '0') {
		child = exec('sudo killall -15 ecasound',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'analog OFF';
	}
});

v9.on('write', function(param) {
	if (param[0] == '0') {
		child = exec('sudo python /home/pi/MikroTik/wifion.py',
		    function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
		        if (error !== null) {
		             console.log('exec error: ' + error);
		        }
		    });
		writeVal = 'analog ON'; 
	}
	else if (param[0] == '1') {
		child = exec('sudo python /home/pi/MikroTik/wifioff.py',
	    	function (error, stdout, stderr) {
		        console.log('stdout: ' + stdout);
		        console.log('stderr: ' + stderr);
	        	if (error !== null) {
	            	console.log('exec error: ' + error);
        		}
    		});
		writeVal = 'analog OFF';
	}
});

v0.on('read', function() {
  v0.write(writeVal);
});
}, 60000);

thanks.

This looks like a SimpleTimer command… but simple timer is C++ code, not JS (javascript)

You will have to Google for the various timer methods used in NodeJS… as your question is JS related, not Blynk specific.

Find some and test them out… report back with any successes as us few Blynk & JS users are the “pioneers” in using Blynk with these other coding languages :smiley:

1 Like

@ErfanDL Haha… you know, I had completely forgot that I had played around with a timed action on my NodeJS test script… It just donned on me, as I was looking at an LED widget that has been blinking away on my NodeJS project for a few months now :stuck_out_tongue:

I cannot recall where I read about this, but I think it is either a version of BlynkTimer in the Blynk NodeJS library, or actually part of NodeJS itself??

Either way… look at this and extrapolate what you need.

1 Like

Thanks I will check your topic :heart_eyes:

@Gunner None of the timers code do not what I want

Why not?? Can you clarify what you want?

1 Like

at the above code in the first post, when I press V9 button the command runs continuously. That command running python file that can send the ssh command to my mikrotik router for turning on wifi or turning off wifi. but the V9 button running continuously and my mikrotik router blocking the ssh services. how can I running the V9 only once and not continously?

The logic is exactly the same as with Arduino or ESP

One way is to code your function to respond to Button HIGH press, start timeout timer and process your command…

Then when timeout timer "times out :wink: " have it send LOW to widget and run a sync command…

Then when the function is called to respond to LOW, do whatever you need to stop your processed command.

1 Like

Thanks. Can you show me an example with js?

?? I just showed you the link to my all my NodeJS examples… But instead of repeatedly blinking an LED, just run a single timeout to stop whatever you want to stop doing.

1 Like

thanks. your code permanently solved my problem :heart_eyes:

var process = require('child_process'); // Allows this script to run CLI commands

// ----- RPi Reboot Command -----
var RPiReboot = new blynk.VirtualPin(20);  // Setup Reboot Button

RPiReboot.on('write', function(param) {  // Watches for V20 Button
  if (param == 1) {  // Runs the CLI command if the button on V20 is pressed
    process.exec('sudo /sbin/shutdown -r', function (msg) { console.log(msg) });
}
});
1 Like

Hmm… OK :stuck_out_tongue: Not sure how a shutdown at button press equates to your OP, a needed timed function, but glad something was useful from my collection :smiley:

1 Like