# Dynamic IP address in node TCP in & out?

**URL:** <https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184>\
**Category:** General\
**Created:** [20 June 2022 18:58 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184 "2022-06-20T18:58:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [20 June 2022 18:58 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/1 "2022-06-20T18:58:10Z")

</div>

Hello,

I have a request regarding TCP in and TCP out nodes. They work perfectly.  
However I would like to be able to pass the TCP address dynamically.  
That is, I would like to fill in the destination IP address from the dashboard and then push it into a global context variable. This would then be reused by the TCP in/out node.  
Do you think this is possible?

II am actually reviving this [topic](https://discourse.nodered.org/t/tcp-output-node-dynamically-change-host-address/1398) that was exposed in 2018.  
There is indeed a way to create an environment variable or to use the Linux SED command to modify the `flows.json` but indeed it is more or less dynamic.  
Thanks for your advice

Edit: I just found out that the UDP node can have the dynamic address and port via msg.ip and msg.port.  
So maybe I'll go that way although the connection is not assured as it can be via TCP.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [21 June 2022 06:55 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/2 "2022-06-21T06:55:21Z")

</div>

I tested this morning with **UDP**. So it works. For the UPD in node I have well the 2 messages **msg.ip** and **msg.port** that are displayed.  
But I don't know how to pass these values to the UDP out node. It refuses my values passed via change nodes

_ **UDP in --\> OK** _  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/3988d51d45d4459f74cfd4de82709efcd9fbede4.png)

_ **Change node to define a flow variables** _  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/6/164722340c5ab993a97ed3ef38b6b38c2cc29e31.png)

**Change node to define both msg.ip and msg.port**  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33dfe7a56fa2a1a580b31e5e95dac6f7bdcb4901.png)

_ **It works if i statically define the destination and port number** _

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74a0d201f09b7edc3cee917ffe5bfb1e2717c9d6.png)

_ **but it didn't works if i try to push the msg.ip and msg.port that came from change node node with the node UDP out cleared** _

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2b0317c5a130f39768ba423afa8516bc146ba9ae.png)

What am I doing wrong ? 🤔

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 June 2022 07:06 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/3 "2022-06-21T07:06:02Z")

</div>

I think it is possible.

I am not doing what you are doing (or want to do) but something similar.

What I have/do:  
I have ONE machine that polls and gets my public IP address.  
If it changes, I am told.  
I press a button on the dashboard and the new IP address is sent to all my machines.  
Some of the other machines have routines that also ping the public IP address.  
So they would suddenly not be able to PING the public IP.  
I'll cop that they stop for a while. (Outside the scope here)  
The public IP is updated on receipt of the new message (IP) and then start pinging the new public IP address.

Do you think that is along the track you want/need?

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [21 June 2022 07:16 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/4 "2022-06-21T07:16:26Z")

</div>

thanks for the answer.  
I'm just trying to fill in once from the dashboard the IP that my rs232--wifi gateway got via dhcp.  
Then I do not intervene any more.  
My software is installed at several people and therefore the IP configuration of the gateway is inevitably different. the user just needs to define it once and that's it

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 June 2022 07:19 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/5 "2022-06-21T07:19:21Z")

</div>

Well, you should be able to parse the RS-232 message and set the IP address automatically.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [21 June 2022 07:23 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/6 "2022-06-21T07:23:20Z")

</div>

yes the info I get via UDP in but I can't find the right syntax to send to the UDP out node as I show on the debug.  
Do you see a big mistake in my change node?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 June 2022 07:34 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/7 "2022-06-21T07:34:36Z")

</div>

Sorry, I haven't actually loaded the flow.

I just saw the topic, had a quick read and replied.

Looking at the **"It works if I statically define the destination"** part I can't see what the _real_ message looks like from the RS-232 port.

# Oh!

Why do you have 2 `change` nodes after the `inject` node?

Shouldn't one be in the connection between where the RS-232 message is received and the next node?

Could you cut down the flow to the `RS-232 in` node to the node that sets the IP?  
(and an example of what the message received looks like)

---

<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:** [21 June 2022 08:10 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/8 "2022-06-21T08:10:23Z")

</div>

Can you show us what is coming out of the debug you have disabled ?

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [21 June 2022 08:54 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/9 "2022-06-21T08:54:26Z")

</div>

> [@dceejay](#):
>
> Can you show us what is coming out of the debug you have disabled ?

you mean this one?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49e57b3a75ea0dcb89e3680fa9422d35ab968db5.png)

> [@Trying\_to\_learn](#):
>
> Why do you have 2 `change` nodes after the `inject` node?

This is just in anticipation of being able to use the dashboard to store the variables later. It's just for the demo. In the end there will be only one IP address field. Only 1 node change left

> [@Trying\_to\_learn](#):
>
> Could you cut down the flow to the `RS-232 in` node to the node that sets the IP?  
> (and an example of what the message received looks like)

Sorry I don't understand the question. I'm not an English speaker and the translator doesn't help me on this one

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/3988d51d45d4459f74cfd4de82709efcd9fbede4.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 June 2022 08:56 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/10 "2022-06-21T08:56:47Z")

</div>

Can you make a small flow that receives the RS-232 message and displays it.

Then _we_ (as I'm sure @dceejay will try also) can may better see what is going on.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 June 2022 09:03 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/11 "2022-06-21T09:03:52Z")

</div>

Your last reply.

The first picture is/shows the message being sent INTO the `ask RS232 gateway` node.  
Why are you sending an IP address into that node?

That node (`Ask RS232 gateway`) is a request you send to..... what?

The second picture: `udp` node.  
That seems (to me) to be what you just injected above.

I think there is more things happening here than are shown.  
Ok, privacy...... Not helping.

But putting that aside, I am missing something.

You have a WiFi uplink (access to the internet) and that is connected via a serial port.  
In the old days the _smarter_ modems had things like `AT` commands to control them.

Normally serial ports don't allow complex messages like internet routers.  
So sending a command to get/request the uplink IP would be a command.

I'll stop here.

---

<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:** [21 June 2022 09:17 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/12 "2022-06-21T09:17:34Z")

</div>

My simple test seems to work... how exactly do you have the udp node(s) configured ?

```auto
[{"id":"0cb2cab620e5133f","type":"inject","z":"9a66423268ae47c4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":195,"y":195,"wires":[["4e7da2a4f4f61a62"]]},{"id":"4e7da2a4f4f61a62","type":"change","z":"9a66423268ae47c4","name":"","rules":[{"t":"set","p":"ip","pt":"flow","to":"192.168.1.218","tot":"str"},{"t":"set","p":"port","pt":"flow","to":"9999","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":385,"y":195,"wires":[["3544ad02120edec7"]]},{"id":"3544ad02120edec7","type":"change","z":"9a66423268ae47c4","name":"","rules":[{"t":"set","p":"ip","pt":"msg","to":"ip","tot":"flow"},{"t":"set","p":"port","pt":"msg","to":"port","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":595,"y":195,"wires":[["0d11396c7fd5478b","fc5a2156ebc99c67"]]},{"id":"0d11396c7fd5478b","type":"debug","z":"9a66423268ae47c4","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":195,"wires":[]},{"id":"fc5a2156ebc99c67","type":"udp out","z":"9a66423268ae47c4","name":"","addr":"","iface":"","port":"","ipv":"udp4","outport":"","base64":false,"multicast":"false","x":805,"y":240,"wires":[]},{"id":"9beb1b234433e43e","type":"udp in","z":"9a66423268ae47c4","name":"","iface":"","port":"9999","ipv":"udp4","multicast":"false","group":"","datatype":"buffer","x":240,"y":360,"wires":[["0ad4f9ebf4d57a39"]]},{"id":"0ad4f9ebf4d57a39","type":"debug","z":"9a66423268ae47c4","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":525,"y":360,"wires":[]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/353f40629744be398cec873935fe1b86e3d77609.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab77381d4c632de4cf08fb0beb311bbcb0e91d2c.png)

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [21 June 2022 12:32 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/13 "2022-06-21T12:32:42Z")

</div>

@Trying_to_learn Thank you so much for trying to help me, I really appreciate it

I had forgotten to place the change node before calling the UDP out node 🙃. It's better this way, I don't get any error messages. But ....

If I fill in the UDP out node with the IP and the port I get the data from my inverter without any problem !

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab83c91817c4a559ca91b980a1dbb234fa5aedaf.png)

Here is the configuration of the node when it works:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/422d0206cbdb38ab65fc06e1c4f125ebf7473253.png)

**if I remove the port number** from the node, it doesn't work. It's like it doesn't receive msg.port ?!

on the other hand, if I delete the IP address and leave the port number, **it works as expected**.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/61fde16d1188d701a37d9f12bb1f4b0b30acdd58.png)

There must be something so obvious that I don't see it. Some days are like that.

The gateway i'm using is this one Elfin ew10a  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/0672a48147f55a30c0c66f956d99afbad5d8eabf.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 June 2022 22:14 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/14 "2022-06-21T22:14:38Z")

</div>

> [@Jean-Luc](#):
>
> It's better this way, I don't get any error messages. But ....

The `But` is a bit confusing.

That normally means the next bit goes against what was just said.  
_But_ then you go on and say:

> [@Jean-Luc](#):
>
> If I fill in the UDP out node with the IP and the port I get the data from my inverter without any problem !

So that is good - yes?

I am not sure why you are putting the `192.168.1.114` in the `address` field.  
I thought you said the IP address was dynamic.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [22 June 2022 05:47 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/15 "2022-06-22T05:47:50Z")

</div>

Oh, the translation problems make it not always clear.  
What I meant by dynamically setting the IP address is the possibility to pass it to **TCP out/in nodes** with a msg.payload and not to enter it once and for all in the statytic configuration of the node in question.

With the **UDP out** node I've seen that **it's possible** to pass the IP port values via msg.port and msg.ip.  
These are filled in once and for all at the start of my flows by the user via the dashboard. The gateway having obtained its IP address via DHCP, this one is obviously different for each one. Hence the need to fill it in at least once.

Now with the UDP out node, **I found my solution**. I can pass the IP address to the node via a global variable that was previously filled in from the user.  
I hope I have been clearer and that the translator doesn't play tricks on me

In my tests with UDP out, I manage to use the msg.ip, but not the msg.port. But fortunately the port is fixed, so it works like that for me.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 June 2022 22:26 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/16 "2022-06-22T22:26:07Z")

</div>

Ok, good to hear you got it working.

Also good to hear you got it all working automatically too.

---

<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:** [23 June 2022 09:26 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/17 "2022-06-23T09:26:13Z")

</div>

> [@Jean-Luc](#):
>
> In my tests with UDP out, I manage to use the msg.ip, but not the msg.port. But fortunately the port is fixed, so it works like that for me.

dynamic port works for me...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/900b6d051d6f65f85da67955f7d0224f6f9bd75a.png)

```auto
[{"id":"2c40f9172a1d19b7","type":"udp out","z":"3b9a9d02857dd4a7","name":"","addr":"","iface":"","port":"","ipv":"udp4","outport":"","base64":false,"multicast":"false","x":680,"y":1080,"wires":[]},{"id":"2e938d0a578b2881","type":"udp in","z":"3b9a9d02857dd4a7","name":"2001","iface":"","port":"2001","ipv":"udp4","multicast":"false","group":"","datatype":"buffer","x":630,"y":960,"wires":[["b54bf3cf43d58410"]]},{"id":"b54bf3cf43d58410","type":"debug","z":"3b9a9d02857dd4a7","name":"debug 30","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":780,"y":960,"wires":[]},{"id":"7f63bedbd47a7d14","type":"udp in","z":"3b9a9d02857dd4a7","name":"2002","iface":"","port":"2002","ipv":"udp4","multicast":"false","group":"","datatype":"buffer","x":630,"y":1020,"wires":[["5186b80b1336d33c"]]},{"id":"5186b80b1336d33c","type":"debug","z":"3b9a9d02857dd4a7","name":"debug 31","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":780,"y":1020,"wires":[]},{"id":"f11d075eabbbc488","type":"inject","z":"3b9a9d02857dd4a7","name":"127.0.0.1:2001","props":[{"p":"payload"},{"p":"ip","v":"127.0.0.1","vt":"str"},{"p":"port","v":"2001","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":1060,"wires":[["2c40f9172a1d19b7"]]},{"id":"e85512d2892ad1c5","type":"inject","z":"3b9a9d02857dd4a7","name":"127.0.0.1:2002","props":[{"p":"payload"},{"p":"ip","v":"127.0.0.1","vt":"str"},{"p":"port","v":"2002","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":480,"y":1100,"wires":[["2c40f9172a1d19b7"]]}]

```

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [24 June 2022 20:34 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/18 "2022-06-24T20:34:54Z")

</div>

Stephen,  
you are totally right, it works also for the port number.  
I'm not sure what I did wrong last time, but this is ok now.  
Thanks for testing and correcting my false statement. I appreciate it.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da71298395862c9b7af95285d423c1aa61ae1ffd.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fd5a71922a897ee200d5567f2334a90696929037.png)

---

<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:** [8 July 2022 20:35 UTC](https://discourse.nodered.org/t/dynamic-ip-address-in-node-tcp-in-out/64184/19 "2022-07-08T20:35:05Z")

</div>

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