Splitting Data and Publishing to MQTT Topic & Message

Hello,

I feel familiar with Publishing Data to MQTT using an iterative method where an index number is the 'Topic' and the data I am looking to present is the 'Message'.

Ex/
0= Version 1F34/22
1= Nextdata 1212/44

This being said, I am looking to take my data and publish it as follows:
Version 1F34/22
Nextdata 1212/44

I have successfully published the data in the desired form as listed above however the '/' is creating a new message underneath the topic and splitting the message into multiple messages underneath the topic. For example,

Version 1F34
22
Nextdata 1212
44

My function node used to publish the data contains the following code:

var ioStatusInfo = msg.payload;
flow.set('ioStatusInfo', msg.payload);

for (var i = 0; i < ioStatusInfo.length; i++) {
    var topic = ioStatusInfo[i]
    var payload = ioStatusInfo[i+1]
    node.send({ topic: topic, payload: payload });
}

return msg

The msg entering the topic node is a string value within an array. For example,
UI[ 16] OFF RSR8/PNS8/STYLE8

Further, to note, I am splitting the string over ']' where 'UI[ 16]' is the topic and 'OFF RSR8/PNS8/STYLE8' would be the message

Thanks for any help in advance.

Can you show us an example message that you are sending to MQTT please. Set the debug node to Output Complete Message.

Thanks for the reply Colin.

This is what enters into my 'Publish' Function Node:

[ 'F Number', 'F205804   ' ]
3 Aug 11:22:57 - [info] [debug:debug 364]
[ 'VERSION ', 'LR HandlingTool      ' ]
3 Aug 11:22:57 - [info] [debug:debug 364]
[ '$VERSION', 'V8.30226      9/27/2017' ]
3 Aug 11:22:57 - [info] [debug:debug 364]
[ 'DATE', '    07-JUL-23 03:52 ' ]

This is what leaves the Node:

{
  topic: 'FANUC Robot/Initial Information/F Number',
  payload: '"F205804   "',
  _msgid: 'f3dcaaa027c1cb42'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/F205804   ',
  payload: undefined,
  _msgid: 'f3dcaaa027c1cb42'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: [ 'F Number', 'F205804   ' ],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 0 },
  _msgid: 'f3dcaaa027c1cb42'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/VERSION ',
  payload: '"LR HandlingTool      "',
  _msgid: '8e5b7ad3704bbc19'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/LR HandlingTool      ',
  payload: undefined,
  _msgid: '8e5b7ad3704bbc19'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: [ 'VERSION ', 'LR HandlingTool      ' ],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 1 },
  _msgid: '8e5b7ad3704bbc19'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/$VERSION',
  payload: '"V8.30226      9/27/2017"',
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/V8.30226      9/27/2017',
  payload: undefined,
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: [ '$VERSION', 'V8.30226      9/27/2017' ],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 2 },
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/DATE',
  payload: '"    07-JUL-23 03:52 "',
  _msgid: '7587543be12b3167'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/    07-JUL-23 03:52 ',
  payload: undefined,
  _msgid: '7587543be12b3167'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: [ 'DATE', '    07-JUL-23 03:52 ' ],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 3 },
  _msgid: '7587543be12b3167'
}

This is an example of my issue here where only the first message is correct but it is creating duplicate malformed messages for the following 2.

{
  topic: 'FANUC Robot/Initial Information/$VERSION',
  payload: '"V8.30226      9/27/2017"',
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  topic: 'FANUC Robot/Initial Information/V8.30226      9/27/2017',
  payload: undefined,
  _msgid: 'c73dfa4091d6eae8'
}
3 Aug 11:25:36 - [info] [debug:debug 366]
{
  payload: [ '$VERSION', 'V8.30226      9/27/2017' ],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: { id: '424a0f96cb3665df', type: 'array', count: 4, len: 1, index: 2 },
  _msgid: 'c73dfa4091d6eae8'
}

You probably don't want the return msg on the end of the function. That will send the current message after it has completed your loop. Change it to just return. In fact I usually use return null so that when I come back to the code I know that it was intentional to return nothing.

In addition though, don't you need to increment i by two each time, so that it uses element 0 as the topic, then element 2. At the moment it uses element 0, then trie to use element 1 for the topic and element 2 (which does not exist) as the payload.

You sir, are the best.

This fixed my issue.

Thanks for your help!

Colin,

When applying this fix, it addressed the splitting of my payload, however, if my Topic has a '/' it is still splitting into further subtopics. Are you aware of a way to have it not split? I have attempted to use jsonStringify with no luck.

Appreciate your assistance.

Again, please, show us an example and explain what you want it to do.

This is the Publish Function Nodes code:

var config1 = msg.payload;
flow.set('config1', msg.payload);

for (var i = 0; i < config1.length; i += 2) {
    var topic = 'FANUC Robot/Configuration/' + config1[i];  // Append the index to make the topic unique
    var payload = config1[i+1]
    node.send({topic: topic, payload: payload});
}

return 

This is the data being input into the Publish node:

{
  payload: [ 'EtherNet/IP Safety', 'R713' ],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: {
    id: '4e8d888cbc753cb4',
    type: 'array',
    count: 197,
    len: 1,
    index: 101
  },
  _msgid: '3b88cb49e0335c6f'
}

{
  payload: [ 'Ethernet/IP Adapter', 'R784' ],
  topic: '',
  filename: 'C:\\Users\\PC\\Documents\\FANUC _ FANUC Robotics.html',
  parts: {
    id: '4e8d888cbc753cb4',
    type: 'array',
    count: 197,
    len: 1,
    index: 102
  },
  _msgid: '8d42415cdfcdb235'
}

Here is the message at the output of the Publish node:

{
  topic: 'FANUC Robot/Configuration/EtherNet/IP Safety',
  payload: 'R713',
  _msgid: '6d046d8ed801b5af'
}
4 Aug 09:12:15 - [info] [debug:debug 367]
{
  topic: 'FANUC Robot/Configuration/Ethernet/IP Adapter',
  payload: 'R784',
  _msgid: '2297eb13eea80268'
}

Which produces this result in MQTT:
image

I am looking for the data to be represented in the following form:
Ethernet/IP Safety = R713
Ethernet IP Adapter = R784

Where the bold above is the singular topic (not split into subtopics).

That is the way MQTT works. If you don't want the subtopics then don't use /

Unfortunately this is the way the data is extracted from the web server.

I was hoping for an easy fix however I believe it will require further tampering with. I was just wondering if there was a form I could represent the text in that it would not recognize the '/' operator as a new subtopic.

Thanks anyways.

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