UBJSON extension

I'm using MQTT to get some data from PLC by robust way. MQTT protocol is light way enough itself, but data preparation consumes huge resources of PLC. JS didn't supports PlC's data types at all. I.e. Simatic S7 and JS have not any common data types except of string. Because of that I have to form JSON-string on PLC's side.
I looked around the issue and find out the UBJSON extension of JSON can help me. But I can't understand how add UBSJON to Node-RED.
So, any suggestions are kindly appreciated.

Welcome to the forum @zova

Can you give an example of data types that are not supported?
Please also explain your system architecture. Is the PLC connecting directly to MQTT?

My architecture is PLC -> MQTT -> Node-RED.
Only one way robust transmission nedeed. Reverse direction transmission uses Hilscher S7 Node-RED driver.
For PLC -> MQTT -> Node-RED all data types are unsupported except String.
Yes, my PLC is connected directly to MQTT.

It works, but consumes huge resources on PLC side for preparing JSON string.

data preparation consumes huge resources of PLC

It works, but consumes huge resources on PLC side for preparing JSON string.

I think you are trying to say that it consumes huge resources on the PLC preparing a JSON string.

But the question is, why prepare a JSON string on the PLC?
JSON formatted data is nice to have but it's not mandatory.
Why can't you send whatever the PLC has and consume (more plentiful) resources on the Node-red machine?

As You can see, english is not my native language, however You got right main thought. Thanks.
It's not so much the JSON itself, but converting number to string on PLC side.
I think UBJSON will help me escapes the convertion.

So, what is your suggestion?

Show us an example of the data which your PLC can easily send to Node-red, and the format you want it to be in.

No doubt someone will be able to construct a flow to convert one to the other.

Note, if it helps, that mqtt will accept a binary buffer and node red has nodes that can interpret the data in a buffer.

There is a ubjson npm module you could use

Add @shelacek/ubjson in the "setup" tab then in the "on message" tab, something like this should do it...

msg.payload = ubjson.decode(msg.payload)
return msg
1 Like

I already installed that npm and works with it. Not all are clear but it is just beginning.

npm had been installed without any error but Node-Red didn't recognize it. What is wrong?


I realized that npm should been installed directly from Node-RED. By default in my Node-RED installation external npm modules was switched off. Also here wasn't settings.js file. I downloaded settings.js from Node-RED site, placed it to .node-red folder, edit and reboot - the button allow loading external npm appears. But now I can't find out the tgz packed ubjson npm. Ok, it possible to create tgz packed npm using package.json file from npm download directory... I need a break.

Read again :point_up:

You simply enter that on the setup tab on the function node. Then use it (without the require)

1 Like

Yes. I did that. And I got "myHomeTemperatures/50875 : msg.payload : undefined" error.


Fortunately it's simple enough to made the tgz, here:
npm pack @shelacek/ubjson

After that I got "shelacek-ubjson-1.1.1.tgz".

When I try load the "shelacek-ubjson-1.1.1.tgz" from Node-RED I got error "Module not found". Remote or local I do it it doesn't matter the same result.

I'm in deadlock.

Change the MQTT node to deliver a buffer.

There is no need to make a tgz, adding it to the function node is all you need to do.

Ps. Uploading TGZ modules is for Node-RED node packages - I.e. node-red-contrib-xyz. This module is simply a library - not a full on Node-RED node package - so that won't work here

1 Like

Thanks, it works!

So, I tested all ways: JSON, UBJSON and finally binary. (Simatic -?-> MQTT -?-> Node-RED -> MySQL)
As for me, the most graceful is binary way with help of node-red-contrib-buffer-parser.
Thanks!

1 Like

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