Back

Topic

[KB1109]How to get JSON Objects from Array using Node-RED

Tags: MQTT, SV Manager Toolkit, Svmgr

4 years ago
By AT
Options
Print

 

Applies to:

PcVue 15.0


Summary:

When using Gateways with Node-RED intergrated in it, different manufacturers will have different versions of Node-RED installed on the Gateway and there would be times where certain functions of the gateway will be locked. As such, users are unable to install npm* on the linux part of the gateway, thus unable to download any nodes from the npm.

Hence, in this KB, it will be discussed in an event where an input which is read by the Gateway from a source (e.g Modbus) returns an array instead of a JSON object.
The nodes used in Diagram 1 will be available to download as a reference and can be used by Importing by Clipboard. 

*npm is the world’s largest Software Library. It is also a software Package Manager and Installer. 


Details:

 

Going with the following Diagram 1, there are two Modbus inputs where it is reading the registers of a PLC.

 Image1

Diagram 1

 

 By directly tapping the message output from the PCD3_1 and PCD3_2, it can be seen that the outputs which are obtained from the nodes are in the form of an Array as shown in Image 1.

 

Image5

Image 1

In this case, PcVue would not be able to read it as a Register Variable using the MQTT format while it can setill be read by using a Text Variable.
Thus, a custom function would be needed to written to serve as as a translator to convert the incoming array object as an JSON object. 
A general template for the function would be as follows:

Image3

Image 2

in cases where certain elements would need to be displayed in as decimal points, due to the fact that it is not possible to change the scaling of the values of Internal Variables, then, the value would need to be processd as such before getting Published by the MQTT.

Image4

Image 3

This would ensure that the values which are read by PcVue are of the right scaling without doing any additional expressions on PcVue’s side of things. 
With the function properly in place, the output from Debug 2 and Debug 4 would also change showing the following output:

Image6

Image 4

And when checked using MQTTfx or any equivalent MQTT Explorer, the follow results should be obtained.

Image7

Image 5

 

Then, depending on the configuration which was done in the svmgrMQTT.ini, the general configuration for the variable would be as the following

Text Attribute 15: JsonGetValue(value, ‘0’)
Text Attribute 16: MQTT|Generic|BrokerName|SUB|0|TopicName|TATT15

And by following the example output from Image 5, the value 26.27 should be ontained by the Register Variable.

Dowloadable File:

  1. Node-RED – JSON Object Output

 

 

Created on: 06 Apr 2021 Last update: 30 May 2024