# Socket connection to python script on RPi

**URL:** <https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728>\
**Category:** General\
**Created:** [26 July 2019 17:22 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728 "2019-07-26T17:22:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [26 July 2019 17:22 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/1 "2019-07-26T17:22:56Z")

</div>

I have a RPi with SMS Hat running a python script serving as SMS gateway. Mostly for misc alarm purposes. To send SMS, I open a socket connection from another RPi and send a simple message to the server. I'd like to this from Node Red as well, but I am not sure how.

I could, of course use some mqtt or other to trigger this, but if I could do this directly from a node in Node Red, I'd prefer that. Any thouhts how this could be achieved?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 July 2019 19:35 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/2 "2019-07-26T19:35:23Z")

</div>

What sort of socket connection ? Node-RED has TCP and UDP as well as Http Mqtt etc.

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [26 July 2019 20:38 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/3 "2019-07-26T20:38:52Z")

</div>

I am on "thin ice" here, but a qualified guess would be TCP. I initialize without parametres, like

```auto
# Python code:
s = socket.socket()

```

and that defaults to TCP, AFAIK.  
So if we go for TCP, what would you suggest?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 July 2019 22:04 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/4 "2019-07-26T22:04:25Z")

</div>

Do you just send it some bytes ? Or do you need to send it something, wait for a reply, send something else, etc.  
If the former then I’d suggest a tcp out node, if the latter try a tcp request node.

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [27 July 2019 07:47 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/5 "2019-07-27T07:47:04Z")

</div>

I'ts just a (constant) string for now.  
I tried with Inject (containing the string) -\> TCP out, configured with the gateway ip and port and disconnect after send. Seems promising!

Thank you!

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [27 July 2019 10:37 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/6 "2019-07-27T10:37:44Z")

</div>

After playing around with the TCP node, I have a problem with blocking. I have chosen the "Close connection after…" option in the TCP node, but it seems that it is still reconnecting and keep the connection alive even when there is no new message arriving TCP node, making my SMS gateway wait for message from Node Red and blocking other connections.

Maybe this could be solved in the gateway (I don't know how), so could this be solved in Node Red?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2019 11:26 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/7 "2019-07-27T11:26:32Z")

</div>

I'd also look into the possibility of triggering the SMS directly from node-red. Might be easier than you think, then you get rid of the python element altogether.

Node-red to node-red communication is child's play (UDP, TCP, MQTT, and more) all none blocking.

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [27 July 2019 13:06 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/8 "2019-07-27T13:06:44Z")

</div>

Yeah, I have considered that as an option. And good to hear that this is not too hard to get running.

Since I have a little universe of units using this gateway, i should perhaps let my SMS gateway keep serving the python universe for now, and run Node Red on a second RPi with SMS Hat that can take care of SMS needs within my Node galaxy…

Seems I have a decision to make...

Are there any specific node you would suggest for talking to my GSM Hat?  
Maybe [SMS-tools](https://flows.nodered.org/node/node-red-contrib-smstools) is the right choice for me?

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [29 July 2019 12:03 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/9 "2019-07-29T12:03:32Z")

</div>

@Steve-Mcl and @dceejay  
I actually ended up with a ESP32 running micropython acting as a mqtt to sms router. Not a very robust solution since then I have another unit that can possibly fail…!

But for now it works perfect and let me go on and explore the possibilities in Node Red.  
Thanks for your guidance!

---

<div class="post-metadata">

**Author:** ![solano8](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@solano8](https://discourse.nodered.org/u/solano8)\
**Post date:** [19 October 2020 10:28 UTC](https://discourse.nodered.org/t/socket-connection-to-python-script-on-rpi/13728/10 "2020-10-19T10:28:16Z")

</div>

And if someone still reads this post, I found out for a few weeks ago, that this service was perfect to run as a Docker container, so I did and now the ESP32 module is ready for other tasks...
