How to Read Data from an MQTT Broker and Parse Information within vNode

Created by Jose Fabian Solano, Modified on Fri, 12 Jul at 11:52 PM by Jose Fabian Solano

To begin integrating MQTT in vNode, first log in and navigate to the "Config" option in the System panel. Then, click on "Modules" in the explorer panel. Create a new module by clicking the three lines in the model panel and naming it appropriately. In the new menu, select "mqttclient" as the module type under "Module Type."



After creating our module in vNode, locate it under the "Modules" option in the explorer panel, and proceed to configure it by establishing a communication channel to our MQTT broker. Configure the communication parameters by selecting the MQTT protocol version supported by your broker, ranging from version 3.X to version 5.

Choose the destination broker; predefined templates are available for connections like Azure, AWS, or Google IoT Core, or select "Custom" to configure your own connection type. Automatically generate a Client ID, which serves as a unique identifier for the broker.

Authentication options depend on the broker's security settings. For connecting with Mosquitto, leave it unsecured as Mosquitto is a free broker without security measures. Finally, select the MQTT protocol connection options, whether it's "mqtts" for MQTT over TLS or "mqtt" for plain MQTT. In the URL field, enter the address where the MQTT broker is hosted, along with the port it uses.


To complete the setup, we will proceed to create an agent in vNode that acts as a subscriber to interact with the MQTT broker and process the received data in JSON format.

Subscriber Agent Configuration

Definition of Example JSON: We will use a simple JSON to illustrate the process:


{
"timestamp": "2024-02-12T08:30:00",
"sensor": "T1",
"value": 25.5,
"unit": "Celsius"
}

The goal is to extract the timestamp and the value of sensor T1.

Configuration of the Subscriber Agent in vNode:

Topic: Specify the MQTT topic where the data in JSON format is published. For example, "sensors/temperature".

Quality of Service (QoS): Generally, this is left at its default value, which depends on the MQTT broker configuration.

Message Encoding: Define how the received message is encoded. By default, UTF-8 is used.

Compression: In this case, compression is not applied to the MQTT message.

Deserialization: Select JSON as the deserialization method since the received data is in JSON format.

Data Parser: Choose "custom" to enable the use of custom JavaScript code. This facilitates the extraction and processing of the timestamp and sensor T1 value from the received JSON.



In the JavaScript code section, you can create your own script to parse information. We provide an example that you can adapt or modify as needed. This example illustrates how to use the JavaScript code option for parsing operations, helping you understand its functionality and facilitating integration into vNode or other applications. 




code JS:

// Receive input data from the vNode environment
var data = $.input;

// Convert data to a JSON string
let jsonString = JSON.stringify(data);
// Log a debug message indicating the JSON content being read
$.logger.debug("Reading JSON content: " + jsonString);

// Parse the JSON string into an object
let jsonObject = JSON.parse(jsonString);

// Extract the 'value' and 'timestamp' properties from the JSON object
let sensorValue = jsonObject.value;
let timestamp = jsonObject.timestamp;

// Log debug messages for the extracted values
$.logger.debug("Reading sensor value: " + sensorValue);
$.logger.debug("Timestamp value is: " + timestamp);

// Push the extracted data to the output with specified tag, value, quality, and timestamp
$.output.push({tag: "/mqtt/Sensor", value: sensorValue, quality: 192, ts: timestamp});


After creating the agent, you need to ensure that a tag is configured with the MQTT module. In the last line of the JavaScript code, you are writing to the path /mqtt/Sensor. This path corresponds to the tag within vNode, so it must be created beforehand. You can create any tag, but you need to specify the tag's path in the code accordingly.


Ensure that the tag has "Client Access" configured with read and write permissions. Also, verify that the "Source" is correctly set up as depicted in the image. For the "Subscriber" field, specify either the connection name to the MQTT broker or the name of the agent containing the JavaScript code for parsing.

Finally, you can verify in real-time that the tag is receiving information from the MQTT broker. For the code to execute, ensure that the broker is publishing data so that vNode can update accordingly.



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article