# \[Solved\] How to control a raspi with mqtt?

**URL:** <https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950>\
**Category:** General\
**Created:** [19 May 2020 20:34 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950 "2020-05-19T20:34:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maximus1](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Maximus1](https://discourse.nodered.org/u/Maximus1)\
**Post date:** [19 May 2020 20:34 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950/1 "2020-05-19T20:34:47Z")

</div>

hi i have raspi 2 as ambilight controller and want to send commands like stop hyperion etc. to that machine. can you show me a best practise?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [19 May 2020 21:02 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950/2 "2020-05-19T21:02:51Z")

</div>

As always, there are various ways to do this. perhaps the main thing to think about before getting started is security. Assuming that you are running with a standard Pi and Node-RED setup, you are running Node-RED under the Pi user. This is OK as long as you never connect your Pi to outside of your local network.

The default setup requires a passcode to be entered when running system commands as the pi user with sudo.

So to be able to call system commands such as stopping a service from within node-RED, you typically use the `exec` node but would need to prefix the command with sudo which would prompt for a passcode which you cannot then provide very easily in a flow.

There are probably 3 main ways to get around this. Each has strengths and weaknesses and you will need to decide which is best for your use-case:

1. Change the sudo permissions for the pi user so that a password is not required for certain system commands.

2. Put the commands into a bash script with the password embedded.

3. Change the bash input stream to take the password from the exec node.

4. Write a separate service (using node.js, python or bash, etc) that has the permissions and validates specific input to only allow certain commands from Node-RED only.

---

<div class="post-metadata">

**Author:** ![Maximus1](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@Maximus1](https://discourse.nodered.org/u/Maximus1)\
**Post date:** [19 May 2020 22:48 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950/3 "2020-05-19T22:48:25Z")

</div>

thanks for your reply.  
i finaly played around with `node-red-contrib-ssh-v2-reconnection` and a sonoff plug with measuring.  
If "Power" is under 11 Watt ambilight is switched off and over 10 Watt it is switched on.

I think it is the best solution without hacking tonns of code into an IDE or stealing code without understanding what it does.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [19 May 2020 23:50 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950/4 "2020-05-19T23:50:42Z")

</div>

> [@TotallyInformation](#):
>
> if you wanted to, put the password into a configuration node. There is a node that lets you do that, sorry, the name escapes me.

`node-red-contrib-credentials`

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [20 May 2020 01:59 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950/5 "2020-05-20T01:59:35Z")

</div>

> [@afelix](#):
>
> node-red-contrib-credentials

Isn't this node suffering a fairly significant (still unresolved) issue at the moment? [Hidden field not hidden · Issue #3 · Steveorevo/node-red-contrib-credentials · GitHub](https://github.com/Steveorevo/node-red-contrib-credentials/issues/3)

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [20 May 2020 05:21 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950/6 "2020-05-20T05:21:07Z")

</div>

I wasn’t aware of that, and you’re right. I can see what causes it too. It’s an error by design looking at the code. All credentials-rules set up are stored in a complex object in a single credentials field stored as “text”, meaning encrypted in the credentials but entirely received and gotten back at config time.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 June 2020 05:21 UTC](https://discourse.nodered.org/t/solved-how-to-control-a-raspi-with-mqtt/26950/7 "2020-06-03T05:21:12Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
