TCP/IP Server in Node-RED

Hello,

I am trying to return robot data in Node-RED. Currently, I have a robot script that runs on the robot (client) that sends the data e.g. joint position to my RPi. I have been able to used a python script to create a server socket such that when I run it, and then run the robot script on the robot, the robot data sends to the output window in my python ide. I thought that I would be able to use the TCP node in node red to bring this data in, but after various attempts I have been unsuccessful. I have both tried to listen and connect to the RPi IP and port, tried the various output settings within the tcp node. I thought it might be a decoding issue, so I have tried to use the convert (iconv) node to decode it (utf8), and used a function to try and decode it. I have also tried to put my server socket script into a python node, but I am unfamiliar with this an I am sure on how to get the msg payload out. I would like to stick to the TCP node if possible. I have also tried the tcp-server and the tcp-client nodes to no success. I would appreciate any guidance on my problem, thanks in advance! The output in the python ide for the joint angles is a list: [89.587, 5.007, 98.900, -8.145, 64.251, -102.354]. The python code for reference is:

import socket

HOST = "192.168.0.50" 
PORT = 2000

server = socket.socket()
#print('socket created')
server.bind((HOST, PORT))
server.listen(1)
client, addr = server.accept()
msg = client.recv(1024)
print(msg.decode('utf-8'))

client.close()
server.close()

hi, I have a few questions to better understand the issue.

  1. does the tcp node connect to the port on your rpi?
  2. can you use a terminal emulator to connect (i.e. realterm, yat, etc.)

if not the issue might be with the creation of your tcp server on the RPI side.

hello,

  1. Yes sorry, to confirm the robot is connected to my Rpi via an ethernet connection, and my Node-Red instance is on my Rpi. The IP address in the code above is the IP I have set on the Pi. Using the code above, I am able to use an IDE on the Pi and it returns those angles from the robot itself, when I first run the python socket script on the pi (it is then listening for a connection and data), and then I run the robot script on the robot, which sends the joint angles to the Pi IP address, on port 2000.
  2. I am unfamiliar with emulators, sorry. I know it is possible to retrieve this data with the current set up, I am just struggling to understand why the TCP node in won't work for me as I would expect.

As a side note, when I try to set the TCP node as 'listen on' port 2000, with a debug node and deploy, and for example I move the debug node and redeploy, the error message appears saying something along the lines of that port is already in use. When I set the TCP node as 'connect' to port 2000, the node says that it is connected.

Hi,

I am assuming that the IP adres 192.168.0.50 is your RPI and not your robot. If not. stop reading the issue is different.

  1. you created a listerener on the RPI with the python script. you can do the same thing in nodered with the TCP node. Which I think you are already doing the right way.
[
    {
        "id": "d4a070c1ba14fcfd",
        "type": "tcp in",
        "z": "845f8fb4cea10752",
        "name": "TCP in",
        "server": "server",
        "host": "",
        "port": "2000",
        "datamode": "stream",
        "datatype": "buffer",
        "newline": "",
        "topic": "Data from Robot",
        "base64": false,
        "tls": "",
        "x": 890,
        "y": 440,
        "wires": [
            []
        ]
    }
]

This i what I assume you configured.

The error that you get "Port already in use" means that something else on the RPI is already using port 2000. Most likely your Python script is still running and occupying port 2000.

Additionally you say that when you set the TCP node to 'Connect to' 2000 it does connect. Meaning that it probaly is connecting to your python TCP port. You can check that by sending some data to this port from nodered and see if it is coming in in the IDE of the RPI. You can use this flow to do so:

[
    {
        "id": "ab18187491483c0a",
        "type": "tcp out",
        "z": "845f8fb4cea10752",
        "name": "Connect to Python TCP",
        "host": "192.168.0.50",
        "port": "2000",
        "beserver": "client",
        "base64": false,
        "end": false,
        "tls": "",
        "x": 1130,
        "y": 540,
        "wires": []
    },
    {
        "id": "4447e775299dbf43",
        "type": "inject",
        "z": "845f8fb4cea10752",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Hello from Node-red",
        "payloadType": "str",
        "x": 910,
        "y": 540,
        "wires": [
            [
                "ab18187491483c0a"
            ]
        ]
    }
]

So... basically when putting 'Connect to' in the TCP node. You do this:

Thanks for the responses. Yes, that is the IP address of my Pi. I do not have access to my Pi today, but I will try your idea of sending data to the port from node red tomorrow. I think in regards to point 1 , I am doing it that way, but when I run the robot script to send the joint values to that ip and port, nothing appears in the debug window in node red. When I use the python script in an IDE, the values do return. I make sure to close all references to that port so it is not occupied when I am trialing this in node red. It is, for example, if I redeployed the node, it seems to not remember that that port is open because I have done that already in node red before the second redeploy, which I thought was weird. I have to stop and restart node red to get rid of this error, each time I make a change and deploy the flow. Also it only connects when I choose the setting 'connect' to, rather than 'listen' to. I think I should be on the listen to selection.

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