[SOLVED] Launching programs on Raspberry Pi using Blynk

Hey everyone, I’m trying to use Blynk to start/stop some programs on Raspberry Pi. The start/stop commands are basic Bash commands. I’ve looked around, but I don’t see people using Blynk to execute Bash commands. What is the best way to do this?

Thanks!

While I do have Blynk running on NodeJS, I am not very proficient in either JS or Linux, however since Blynk works with Javascript, I guess that however a bash command works with JS will work in a Blynk NodeJS script.

Google came up with a few sites that seem to give some good examples… heck, I almost understood some of them :stuck_out_tongue: I just might have to dust off my brain and try some more RPi work.

1 Like

thanks @Gunner that looks like a good place to start. I am so very unfamiliar with Javascript that I just don’t even know where to begin, so that was helpful.
What I’m trying to do is
[press button in blynk] -> [run one set of scripts to turn off two programs and turn on one other program]
for 3 different programs on the pi
Seems pretty easy and I have a good idea how to do it in python… but JS … ¯_(ツ)_/¯

Well, after reading a few of those topics found with Google, I was able to merge in a small “run the command ifconfig” snippet into my RPi test sketch when pressing a button on V5… after some syntax fiddling, it seems to work.

This NodeJS example might help you get started. (NOTE: mine is setup for my Local Server, so you will need to adjust accordingly.)

var Gpio = require('onoff').Gpio;  // links variable 'Gpio' to the all important onoff GPIO control library
var Blynk = require('blynk-library');  // Links variable 'Blynk' to the Blynk Library
var AUTH = 'cda957435xxxxxxx283c95018f';  // My top secret auth code... useless to anyone on the cloud ;P
var blynk = new Blynk.Blynk(AUTH, options = { connector : new Blynk.TcpClient( options = { addr:"10.10.3.13", port:8442 } ) });
var process = require('child_process'); // Allows this script to run CLI commands?  Used in the "run the command ifconfig" function

var v1 = new blynk.VirtualPin(1);  		// Setup Button Widget on V1 with variable 'v1'
var v5 = new blynk.VirtualPin(5);  		// Setup Button Widget on V5 with variable 'v5'
var v9 = new blynk.VirtualPin(9);  		// Setup Display Widget on V9 with variable 'v9'
var v10 = new blynk.VirtualPin(10);  	// Setup LED Widget on V10 with variable 'v10'

v1.on('write', function(param) {  		// Watches for V1 Button
  console.log('V1:', param[0]);  		// prints value to CLI
		if (param == 0) {
        		blynk.virtualWrite(10, 0);  // V10 Widget (ORANGE) LED OFF
    			} else if (param == 1) {
                	blynk.virtualWrite(10, 1023);  // V10 Widget (ORANGE) LED ON
        		}
		});

// "run the command ifconfig"
v5.on('write', function(param) {  		// Watches for V5 Button

	console.log('V5:', param[0]);  		// prints value to CLI
		if (param == 1) {				// Runs the CLI command ifconfig if the button on V5 is pressed
			process.exec('ifconfig',function (err,stdout,stderr) {
    				if (err) {
        			console.log("\n"+stderr);
    				} else {
        			console.log(stdout);
    				}
			})
		}
});

v9.on('read', function() {   			// I don't understand why this says read??
	v9.write(new Date().getSeconds());  	// but this sends the seconds 0-59 to the Display Widget 
});

	led = new Gpio(18, 'out'),  			// Sets up the BCM pin 18 as an output for the LED and assigns it to variable "led"
	button = new Gpio(25, 'in', 'both');  // Sets up the BCM pin 25 as an input registering both rising and falling for the variable "button"

button.watch(function (err, value) {  // Watches for button press and assigns 0/1 to value
	if (err) {
		throw err;  // Some form of error handling
	}
led.writeSync(value);  // Sends value (0/1) to Physical LED
	if (value  == 0) {
        blynk.virtualWrite(2, 0);  // V2 Widget (BLUE) LED off
		blynk.virtualWrite(3, 1023);  // V3 Widget (RED) LED on
		} 	else if (value == 1) {
            blynk.virtualWrite(2, 1023);  // V2 (BLUE) Widget LED on
			blynk.virtualWrite(3, 0);  // V3 (RED) Widget LED off
			}
});

Thanks for that!
Here’s what I’ve got so far, a little program that selects from 3 different audio inputs on the Pi

var Blynk = require('blynk-library');

var AUTH = 'nothistime';
var blynk = new Blynk.Blynk(AUTH, options = {
  connector : new Blynk.TcpClient()
});

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 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) {
child = exec('sudo killall -15 ecasound ;\
	sudo systemctl stop shairport-sync ;\
	sudo /etc/init.d/squeezelite start',
    function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
             console.log('exec error: ' + error);
        }
    });
writeVal = 'squeezelite ON';
});
//turn on shairport and turn off others
v2.on('write', function(param) {
child = exec('sudo killall -15 ecasound ;\
	sudo /etc/init.d/squeezelite stop ;\
	sudo systemctl start shairport-sync',
    function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
             console.log('exec error: ' + error);
        }
    });
writeVal = 'shairport ON';
});
//turn on ecasound (analog input) and turn off others
v3.on('write', function(param) {
child = exec('sudo systemctl stop shairport-sync ;\
	sudo /etc/init.d/squeezelite stop ;\
	ecasound -C -q -B:rt -b:1024 -f:16,2,48000 -i:alsahw,1,0',
    function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
             console.log('exec error: ' + error);
        }
    });
writeVal = 'analog input ON';
});

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

It seems to work okay, it does crash sometimes when I switch from input to input, so I’ll probably rewrite it as one button turns one input on/off, so I can have all 3 running if I want and I’m not trying to close programs that aren’t running.

Thanks again :smile:

1 Like

just to share my success, I was able to get the buttons to read as switches to enable on/off of the individual programs. Seems to work pretty well - more stable than the previous version.
I hope it will help someone else :slight_smile:

I used this video as my guide: https://youtu.be/LJ3ic8C8CcA

var Blynk = require('blynk-library');

var AUTH = 'almostmissedit';
var blynk = new Blynk.Blynk(AUTH, options = {
  connector : new Blynk.TcpClient()
});

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 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 /etc/init.d/squeezelite start',
		    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 systemctl start shairport-sync',
		    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('ecasound -C -q -B:rt -b:1024 -f:16,2,48000 -i:alsahw,1,0',
		    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';
	}
});


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