Passing msg.ip in a loop

Hi,
I'm a node red newbie. I have a flow which requires sending some commands to some sensors having different ip addresses. I'm sending these commands to the sensors using udp out node. As these sensors are large in number I can't keep adding these udp out nodes for each senors assigning their ip addresses. To addresses this issue I'am trying to make use of function node to loop these ip addresses along with the command to be sent to each ip address or sensor.

My problem is to loop the ip addresses and also send the command to this set of ip addresses which are looped. To get a better sense of what I need to achieve, I have attached my flow.

I have also attached a function in which I have tried to do the above. The only thing is I wasn't able to pass the ip address to msg.ip required for the udp out node.

Any help in this regard will be greatly appreciated.
image
Funct

[{"id":"f2179e7.b4a206","type":"udp out","z":"96b3b3c.ce7ea5","name":"","addr":"","iface":"","port":"7123","ipv":"udp4","outport":"","base64":false,"multicast":"false","x":580,"y":180,"wires":[]}]

Thanks.

Hi your exported flow is only one node

Have you tried sending the messages using node.send in your loop.
https://nodered.org/docs/user-guide/writing-functions#sending-messages-asynchronously

2 Likes

Did you leave the ip field blank?

EDIT...

ignore that - I think your msg is not fully formed.

You will need to send .ip and .payload, your code screen shot shows you send an object with .payload only in the array of outputMsgs.

EDIT 2...
you are sending separate messages with the ip and com - you need to send 1 msg per comm (with the .ip and .payload fields both populated in the same msg)

PS: when posting function code - please paste the actual function code instead of a screen shot (so helpers can simply edit and post back to you - I'm not re-typing that)

Apologies for the multiple edits (its early :slight_smile: )

By looking at the function node screenshot I see two issues:

  • you are creating two separate msg objects to be added to your outputMsgs array ({payload: ipc} and {payload: comm})
  • you set msg.port value in the loop but msg is not even returned from the node

It's not entirely clear to me what each message should look like but maybe this would be close to what you wanted to accomplish:

for (let i = 2; i < 20; i++) {
  node.send({
    ip: "192.168.0." + i,
    port: 7123,
    payload: "on"
 });
}

This will create and immediately send a new object for each IP. The array and the other predeclared variables can be deleted.

Alternatively if you want separate variables, this should accomplish the same:

const port = 7123;
const payload = "on";

for (let i = 2; i < 20; i++) {
  let ip = "192.168.0." + i;
  let sensorMsg = {ip, port, payload}; // property shorthand, same as {ip: ip, port: port, payload: payload}
  node.send(sensorMsg);
}
2 Likes

Node.send did the trick.
Thank you ristomatti and all of you guys !

1 Like

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