KB-109501: Collecting values and timestamp from a JSON message via Driver MQTT.

Question:

I receive messages in JSON format in my MQTT devices, so I would like to collect the values Time (timestamp), var1, and var2. How should I set up the Elipse MQTT driver template to do so?

{
"FormatId":"TagValues",
"ApiVersion":1,
"CollectionId":1,
"TagData":[{
                    "Time":"2020-05-25T21:11:07.337-3:00",
                    "Values":{"var1":110,"var2":true}
                    }]
}

Solution:

JSON (JavaScript Object Notation) is a lightweight data-interchange format. To extract the contents in a JSON message and collect the Elipse MQTT driver’s timestamp value, you will need to declare the template. Templates allow reporting not only about the messages format but also which parts should be turned into data. They can come in different types, such as JSON, CSV, or XML, and each template must use keywords, in order to replace the values you need to extract. A list of keywords is available at the Elipse MQTT driver’s manual.

On Templates tab, you will be able to set up different patterns for the MQTT message data, whose elements can then be automatically extracted.

In this case, for the message specifically discussed in this article, we can use the following template:

{"FormatId":"DUMMY",
"ApiVersion":"DUMMY",
"CollectionId":"DUMMY",
"TagData":[{"Time":"TS_TEXT(%Y-%m-%dT%H:%M:%S.%f%+)",
"Values":{"var1":"E3VAL","var2":"E3VAL"}}]}

In order to receive data from the variables, you must first create an I/O Block tag with two elements; then, at the tag’s Item parameter (ParamItem property), you will need to indicate both the topic and the template.

The value of Time variable will automatically be attributed to the tag’s timestamp.

NOTE: The demo application attached to this article was developed with Elipse E3 version 5.1 build 175.

Related Article:


Attachment:

App_MQTT.zip

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Leave a Reply

Your email address will not be published.Required fields are marked *