HowTo: Node Red – Restart, shut down and start remote Raspberry Pi

This article describes how to run commands from NodeRed on a remote Linux system and thus be able to reboot and shut down another system.

Hints for our lovely english readers: Basically, many of the articles on Nerdiy.de are translations from the original german articles. Therefore, it may happen here and there that some illustrations are not available in english and that some translations are weird/strange/full of mistakes or generally totaly wrong. So if you find some obvious (or also not obvious) mistakes don't hesitate to leave us a hint about that in the comment section. 
Also please don't get confused, that instead of a "dot" often a "comma" is used as decimal separator. 🙂


Safety instructions

I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/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 shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂


Requirements

Helpful Articles:
Of course NodeRed should already be installed so that you can take over this configuration.
How to prepare this and a RaspberryPi and then install NodeRed on it is described in the following articles.

RaspberryPi – Setting up for Nerdiys!
RaspberryPi – The first configuration
RaspberryPi – Controlling the RaspberryPi via SSH
NodeRed – Installing NodeRed on the RaspberryPi

Required tools:
-none-

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 prompt

For this node code to work, you must first create a SSH key pair on the RaspberryPi from which you want to send the commands (in this case the RaspberryPi running on the NodeRed) and exchange this with the RaspberryPi you want to control (in this case, the RaspberryPi on the MagicMirror installation is running).
How to do this is explained in the article RaspberryPi – Login via SSH without password prompt.


Log in to the NodeRed configuration interface

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

View of the login screen. Here you have to enter the login details that you entered during the configuration of the login. Information about this can be found in the article NodeRed - User Login Setup.

Node Code

The following NodeCode causes you to shut down or restart the remote system from the Dashboard of your NodeRed installation. After inserting the node code, do not forget to position the two buttons on your dashboard. You can find 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\")\n{\n    msg.payload=false;\n} else if (msg.payload==\"OK\")\n{\n    msg.payload=true;\n    return msg;\n}\n","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\")\n{\n    msg.payload=false;\n} else if (msg.payload==\"OK\")\n{\n    msg.payload=true;\n    return msg;\n}\n","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, you can of course also execute other commands on the remote system.

Simply change the specified command between the quotation marks. This command will then be executed on the remote system.

I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂

Fab

P.S. 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 appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at ko-fi.com

Leave a Reply

Your email address will not be published. Required fields are marked *