Socket connection to python script on RPi

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?

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

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

# Python code:
s = socket.socket()

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

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.

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!

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?

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.

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 is the right choice for me?

@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!

1 Like

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...