Can a Blynk button execute a code on the pi?

I was wondering if it is possible to have a button from blynk issue a command on the raspberry pi when pressed?

Can you be a little more specific? What exactly are you trying to do?

I have gas sensors and laser trip wires connected to my pi. I want to be able to start and stop the python script that controls their alarm right from my blynk dashboard. Essentially like a armed/unarmed or on/off button.

Have you seen this?

I use child process to send commands to multiple 3rd party processes from my Blynk sketch… Python based OLED screen demos, PiCam commands to take pictures, BrightPi commands to turn on/off the White and IR LEDs, etc…

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

For example, this will reboot my RPi with a Button Widget on V20

// ----- 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 V520 is pressed
    process.exec('sudo /sbin/shutdown -r', function (msg) { console.log(msg) });

Well you can have a code always running a loop that does nothing until the button is pressed and executes some code when the butto nis pressed.

The RPi NodeJS works differently… there is no “loop” perse as in C++

Well on JS is even easier. Just execute some code when the event of the write on the pin occurs :wink:

Edit: actually as you did on your code :slight_smile:

your code seem to do the trick. too bad don’t understand c++. I’m more of a python mid level person.

1 Like