Safe Shutdown of RPi using a Blynk command button

I use Blynk to control my Central Heating but I wish to be able to execute a safe shutdown remotely. Is this possible within Blynk?

Yes it’s possible. Two possible ways:

  1. Set up a physical shutdown button with Python (all covered on Google) and then set the pin high with Blynk.

or

  1. nodejs --> child process —> shutdown script

Ok … I would like to assign a button to effect the shutdown. Presumably you indicate the child process could be invoked using a virtual button.

That’s right and if you search this site for child process you will be able to pick up the required code.

Thanks for this. I have been searching for ages.

Probably many ways to do the same thing…

I use this method in my Blynk client script to activate my RPi3 “shutdown now with reboot” command… works correctly for my setup.

var RPiReboot = new blynk.VirtualPin(20);  // Setup Reboot Button on V20

// Run RPi Reboot Command
RPiReboot.on('write', function(param) {  // Watches for V20 Button
  if (param == 1) {  // Runs the command if the button on V20 is pressed
    process.exec('sudo /sbin/shutdown -r now', function (msg) { console.log(msg) });  // Shuts down with reboot
}
});

If you remove the -r I believe it will just shutdown, and removing the now seems to take about a minute longer… possibly doing some further cleanup?

No, the -now just executes it faster. LIke, instant. It’s a bit like the windows command which seems to do the same.

The only thing you need to realize is, the user which executes the sudo command has to have the rights to do so. In the sudoers file you can actually set the user under which the server runs to allow for single commands to be executed on a higher privilege level.

Linux is very used to handle the principle of “least privilege”. You can read more about the sudoers file here: http://www.atrixnet.com/allow-an-unprivileged-user-to-run-a-certain-command-with-sudo/

It’s all a matter of security. You don’t want to be hacked on the regular account and have it be able to sudo everything. Like rm -rf * / (DO NOT RUN THIS!!!) which will remove everything from your file system.

As to the process.exec, I’d recommend using a script to first cleanly shutdown the Blynk server and after that shtudown the Pi. That way everything shuts down nice. There are several nice shutdown scripts for the Blynk server on the Pi to be found on the forum here :slight_smile:

Thank @Lichtsignaal

Couple of clarifications, I am not using Blynk Server on my RPi3, just client, so no concerns about hacking as I am the only one using it inside my local network… I can’t say the exact same for the OP, but it seemed like it as well (the client bit at least).

That I knew, but removing -now means it takes exactly 1 minute from button press to reboot… so I just thought it might have a reason?? but I don’t see any real activity on the LED leading up to the reboot, so I just leave it in for faster reboot… I put that function in because sometimes after VNCing in and making changes to my script, it is simpler to reboot the whole thing then it is to stop and restart the client if i don’t already have those commands queued up in the CLI. I can be that lazy :stuck_out_tongue_winking_eye: