In this exercise, we will use a JSON file that we will publish to the free Mosquitto MQTT broker. To do this, we will use a tool like MQTT Explorer, which allows you to subscribe to and publish messages on the Mosquitto broker installed on your computer.
Steps to configure and use the tools:
Install Mosquitto
First, you need to have the Mosquitto broker installed on your machine. You can download it from the following link:
Mosquitto: https://mosquitto.org/download/
Install MQTT Explorer
MQTT Explorer is a tool that allows you to easily interact with the MQTT broker. You can download it from here:
MQTT Explorer: http://mqtt-explorer.com/
Publish the JSON to the Broker
Once you have both Mosquitto and MQTT Explorer installed, open MQTT Explorer and connect to the Mosquitto broker (which will be running on your local computer). You can then subscribe to a specific topic and publish the JSON file.
What we are going to do:
Publish: We will use MQTT Explorer to send the JSON to the Mosquitto broker.
Example JSON:
{
"timestamp": "2024-02-12T08:30:00",
"sensor": "T1",
"value": 25.5,
"unit": "Celsius"
}
Now, we will publish the example JSON using MQTT Explorer. You need to open the software and enter the IP address of the Mosquitto broker in the Host option. If it’s running locally, you can simply use localhost or 127.0.0.1 and click Connect, as there is no security enabled.
Within MQTT Explorer, we will publish the JSON to the topic we want. In this exercise, we will use MosquittoPUB as the topic. After entering the JSON, click the Publish button to send the message.
Once you have completed the installation and configuration of the necessary tools, the next step is to access **vNode** and configure the **mqttClient** module to establish a communication channel with the Mosquitto broker.
The configuration in **vNode** should match the parameters of the Mosquitto broker. It uses version **MQTT 3.1.1** and does not require additional security measures, so the **authentication mode** does not need to be enabled. Make sure to select the **MQTT** protocol and correctly configure the **broker URL**, which in this case will be **local**, as Mosquitto is running on your local machine.
Finally, use the **default MQTT port (1883)** to complete the configuration. This setup will ensure that your vNode communicates effectively and securely with the Mosquitto broker, allowing you to send and receive messages without complications.
Once the **mqttClient** module configuration in **vNode** is completed, the next step is to create an **agent** that will act as a **subscriber**. This is because our goal is to extract information from the broker and parse it into a vNode tag.
In the agent configuration, you need to specify the **topic** to which it will subscribe, which in this case will be **MosquittoPUB**. The Quality of Service (**QoS**) is left by default at **QoS 0**, indicating that the message is delivered once without delivery guarantees.
For the **data format**, we select **UTF8** for text interpretation. If the data is not compressed, we set the compression type to **NONE**. Additionally, **deserialization** is done in **JSON** format, as the broker messages will be in this format.
Finally, the **parsing** of the data should be done through **JavaScript code**. At the end of this article, you will find the code documented line by line, which will guide you through the correct implementation and understanding of each step.
// Variable to receive the JSON data from the broker
var data = $.input;
// Convert the JSON object to a string format for debugging purposes
let jsonString = JSON.stringify(data);
$.logger.debug("Reading the JSON data: " + jsonString);
// Parse the JSON string back into an object for easier manipulation
let jsonObject = JSON.parse(jsonString);
// Extract the "sensor" field from the JSON object
let sensor = jsonObject.sensor;
// Extract the "value" field from the JSON object
let value = jsonObject.value;
// Extract the "timestamp" field from the JSON object
let timestamp = jsonObject.timestamp;
// Log the extracted "sensor" value for debugging
$.logger.debug("Sensor name extracted: " + sensor);
// Log the extracted "value" for debugging
$.logger.debug("Value extracted: " + value);
// Log the extracted "timestamp" for debugging
$.logger.debug("Timestamp extracted: " + timestamp);
// Send the extracted data to the output, formatting it as required for the system
$.output.push({
tag: "/mqtt/" + sensor, // Construct the tag using the sensor name
value: value, // Assign the sensor's value
quality: 192, // Set the quality of the data (192 generally means "good")
ts: timestamp // Assign the timestamp of the data
});
Finally, the configuration, result, and processing within a **vNode** tag are carried out as follows:
Created by JF
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article