The Best Way to Arrange MQTT Payload with data from Multiple Peripheral devices

Hi! I am new to JSON/Javascript and I am optimizing an exchange between Edge Agent on Gateway and Cloud. I would appreciate if you help me to fine the easiest way for task.
That’s where I am now:
I have a few peripherial pre-defined devices that I talked to in Modbus RTU and REST API. I process raw data from devices with FUNCTION node filtering out errors and do on-edge convertion to a floating point format. Each FUNCTION NODE generates an array of values with error status and values from a particular device and puts it to “payload.device1”.
I use JSON Encode later for each device to generage encoded payload notes as device1_data;

I send MQTT payload as the following then:

In the reception workflow, I use FUNTION or STRING node after MQTT RECEIVE node to split encoded JSON messages from each device/config data and then only use JSON: Decode node to read finally values from Perifirial devices in the cloud.

This process does not seem to be optimised at all. I tried to save results of Encode function in Edge Agent as output.device1_data, output.device2_data in order to send only {{output}} in MQTT, but it didn’t succeed. Could you suggest me the better way to do so? Thanks

Hi @Alexander_Kondrov,

I believe this process could be optimized by using JSON: Encode after putting all of your processed Device Data (from the Function Nodes) on the payload. Then you can use one JSON: Encode Node to encode the entire object. Here is an example:

I have a workflow that puts three random numbers on my payload all in different locations, but are nested within the working object, like so:

My payload for working will now look like this:

So I then use a JSON: Encode Node and can see the result on my payload:

Hopefully this helps,

1 Like

Hi Julia,
Thank you for you reply. I’ve been using JSON already, but was struggling to gather all my results to one array inside of workflow on Edge Agent. I finally got around pre-initialising output array (var output = (,)) in the beginning of the workflow and then it becomes possible to write inside of it later in FUNCTION nodes like payload.output[1][3] = result.

One more follow-up question. What’s the best way to loop a workflow on the edge agent, so it runs as fast as it can? Currently I trigger a workflow with 1 second TIMER node. The workflow pulls data from peripheral devices and I’d like to do it as fast as possible in the loop.

Good afternoon @Alexander_Kondrov,

One way to loop more quickly is to continue with your timer node that fires once per second, but also implement a Loop and Delay Node. The number of seconds on the Delay Node can be anywhere between 0 seconds (no pause) and 60 seconds, so 100ms, or .1, is supported as well. Thus, you could have your Loop Node run and include the .1 second delay within the Loop, pulling data ten times per second.

Here is a workflow created by one of our engineers that is built similarly:


1 Like

Julia, loop feature seems to be working fine. Thank you.