HowTo: Node Red – Reboot, Shutdown and Boot Remote Raspberry Pi

This article describes how you can execute commands from NodeRed on a remote Linux system and thus be able to reboot and shutdown another system.


Safety instructions

I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/

Affiliate links/advertising links

The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂 


Requirements

Helpful articles:
So that you can install new nodes, NodeRed should of course already be installed.
How to prepare a RaspberryPi and then install NodeRed on it is described in the following articles.

The following three articles describe what needs to be done to prepare the RaspberryPi:
RaspberryPi – setup for nerdiys!
RaspberryPi – The first configuration!
RaspberryPi – Control the RaspberryPi via SSH

NodeRed – Installing NodeRed on the RaspberryPi

Required tool:
-no-

Required material:

In the following list you will find all the parts you need to implement this article.


Set up login via SSH without password request

For this Node code to work you first need to create an SSH key pair on the RaspberryPi you want to send the commands from - in this case the RaspberryPi running NodeRed - and exchange it with the RaspberryPi you want to control - in this case the RaspberryPi running the MagicMirror installation.
How to do this is described in the article RaspberryPi - Login via SSH without password request explained.


Log into the NodeRed configuration interface

Before you can edit your NodeRed configuration, you must - if activated - first log into the NodeRed configuration interface.

View of the login dialog. Here you have to enter the login data that you specified during the configuration of the login. You can find information about this in the article NodeRed - set up user login.

Node code

The following NodeCode causes that you can shutdown or restart the remote system from the dashboard of your NodeRed installation. Don't forget to position the two buttons on your dashboard after inserting the node code. You can find more information about this in the article NodeRed - Create a user interface with dashboard nodes.

[{"id":"73cd461.3b36fb8","type":"exec","z":"5ebf035f.49459c","command":"ssh pi@raspi 'sudo reboot'","addpay":false,"append":"","useSpawn":"","timer":"","oldrc":false,"name":"Reboot","x":900,"y":200,"wires":[[],[],[]]},{"id":"1a778b31.9113b5","type":"exec","z":"5ebf035f.49459c","command":"ssh pi@raspi 'sudo shutdown -h now'","addpay":false,"append":"","useSpawn":"","timer":"","oldrc":false,"name":"Shutdown","x":900,"y":280,"wires":[[],[],[]]},{"id":"91b9aaff.d3c2a8","type":"ui_button","z":"5ebf035f.49459c","name":"","group":"86d0df05.3b41b","order":3,"width":0,"height":0,"passthru":false,"label":"MagicMirror Neustarten","color":"","bgcolor":"","icon":"","payload":"Sicher?","payloadType":"str","topic":"","x":190,"y":200,"wires":[["32a5cb04.d66954"]]},{"id":"4244b4dc.e34d0c","type":"ui_button","z":"5ebf035f.49459c","name":"","group":"86d0df05.3b41b","order":4,"width":0,"height":0,"passthru":false,"label":"MagicMirror Herunterfahren","color":"","bgcolor":"red","icon":"","payload":"Sicher?","payloadType":"str","topic":"","x":200,"y":280,"wires":[["16e4d592.cf3ffa"]]},{"id":"32a5cb04.d66954","type":"ui_toast","z":"5ebf035f.49459c","position":"dialog","displayTime":"3","highlight":"","outputs":1,"ok":"OK","cancel":"Abbrechen","topic":"MagicMirror neu starten?","name":"MagicMirror neu starten?","x":472,"y":198.99994277954102,"wires":[["470e3428.e7f03c"]]},{"id":"470e3428.e7f03c","type":"function","z":"5ebf035f.49459c","name":"","func":"if (msg.payload=="Abbrechen")
{
    msg.payload=false;
} else if (msg.payload=="OK")
{
    msg.payload=true;
    return msg;
}
","outputs":1,"noerr":0,"x":712,"y":198.99994277954102,"wires":[["73cd461.3b36fb8"]]},{"id":"16e4d592.cf3ffa","type":"ui_toast","z":"5ebf035f.49459c","position":"dialog","displayTime":"3","highlight":"","outputs":1,"ok":"OK","cancel":"Abbrechen","topic":"MagicMirror Herunterfahren?","name":"MagicMirror Herunterfahren?","x":482,"y":278.999942779541,"wires":[["4f2bc503.a2cc3c"]]},{"id":"4f2bc503.a2cc3c","type":"function","z":"5ebf035f.49459c","name":"","func":"if (msg.payload=="Abbrechen")
{
    msg.payload=false;
} else if (msg.payload=="OK")
{
    msg.payload=true;
    return msg;
}
","outputs":1,"noerr":0,"x":712,"y":278.999942779541,"wires":[["1a778b31.9113b5"]]},{"id":"86d0df05.3b41b","type":"ui_group","z":"","name":"System","tab":"3e1c2ede.fa5842","disp":true,"width":"6","collapse":false},{"id":"3e1c2ede.fa5842","type":"ui_tab","z":"","name":"Einstellungen","icon":"dashboard"}]

In this way, of course, you can also execute other commands on the remote system.

To do this, simply change the specified command between the quotes. This command is then executed on the remote system.

Have fun with the project

I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂

PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂

Buy Me a Coffee at ko-fi.com       

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert

This site uses Akismet to reduce spam. Learn how your comment data is processed.