# Using python and raspberry with node red and using MQTT

**URL:** <https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139>\
**Category:** General\
**Created:** [19 November 2019 09:55 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139 "2019-11-19T09:55:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirou\_IoT](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mirou\_IoT](https://discourse.nodered.org/u/mirou_IoT)\
**Post date:** [19 November 2019 09:55 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/1 "2019-11-19T09:55:37Z")

</div>

good morning , i need help i want to control solenoid valve 12 v by a raspberry 5 v and use python prog via MQTT and the switch that i will use it to control solenoid valve is in node red dashboard how i can do that please

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2019 10:12 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/2 "2019-11-19T10:12:42Z")

</div>

Are you asking how to organise the hardware to connect a 12v solenoid to a Pi? If so then I don't think this is the right place to ask that.  
I don't understand what you mean 'use a python prog via MQTT' but again that does not appear to be node-red related.  
If the node-red part of the question is how to publish information to MQTT when you press a dashboard button then all you have to do is to connect the button to the MQTT node, so you need to explain what is not working.

---

<div class="post-metadata">

**Author:** ![mirou\_IoT](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mirou\_IoT](https://discourse.nodered.org/u/mirou_IoT)\
**Post date:** [19 November 2019 10:19 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/3 "2019-11-19T10:19:58Z")

</div>

this is my python code that i use it to control solenoid valve by the switch in node red dashboard and i ask how i connect raspberry 5 v with soleinoid valve 12 v i use the  
relay but don't know if its the good solution or not

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2019 10:23 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/4 "2019-11-19T10:23:40Z")

</div>

As I said, a node-red forum is not the best place to ask a question about how to connect the solenoid.  
However, I would ask why you are using python. Why don't you use node-red to drive the GPIO directly?

---

<div class="post-metadata">

**Author:** ![mirou\_IoT](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mirou\_IoT](https://discourse.nodered.org/u/mirou_IoT)\
**Post date:** [19 November 2019 10:27 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/5 "2019-11-19T10:27:47Z")

</div>

i use the python because i use the raspberry and i use the node red and mqtt to control the soleinoid valve by a switch in node red via mqtt " remote server"

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2019 10:32 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/6 "2019-11-19T10:32:13Z")

</div>

Do you mean that the python code is running on a different machine to node-red?

---

<div class="post-metadata">

**Author:** ![mirou\_IoT](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mirou\_IoT](https://discourse.nodered.org/u/mirou_IoT)\
**Post date:** [19 November 2019 10:39 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/7 "2019-11-19T10:39:29Z")

</div>

python code i use it to connect raspberry and node red by using MQTT and the the switch in node red dashboard to control on / off the solenoid valve that i use it with raspberry

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2019 10:44 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/8 "2019-11-19T10:44:44Z")

</div>

You just keep stating the same information without answer the question.

1. Is node-red running on the same pi that you are running the python code? Yes or no.
2. If so then why not use node-red to subscribe to the MQTT broker and drive the GPIO, why do you need to use Python?

---

<div class="post-metadata">

**Author:** ![mirou\_IoT](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mirou\_IoT](https://discourse.nodered.org/u/mirou_IoT)\
**Post date:** [19 November 2019 10:56 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/9 "2019-11-19T10:56:24Z")

</div>

yes node red is running on the same raspberry that i run the pythgon code , secondly i don't know an other solution to drive GPIO and using MQTT to connect raspberry and remote server node red i just know the python  
it's clear..???

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2019 11:02 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/10 "2019-11-19T11:02:20Z")

</div>

This should get you started  
[https://cookbook.nodered.org/mqtt/](https://cookbook.nodered.org/mqtt/)

---

<div class="post-metadata">

**Author:** ![mirou\_IoT](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mirou\_IoT](https://discourse.nodered.org/u/mirou_IoT)\
**Post date:** [19 November 2019 11:43 UTC](https://discourse.nodered.org/t/using-python-and-raspberry-with-node-red-and-using-mqtt/18139/11 "2019-11-19T11:43:26Z")

</div>

thank you @Colin
