Solution Name: JSON Tags MQTT
Software Version: v10
Keywords: Tags. JSON. DataExplorer. MQTT.
Summary
This solution example demonstrates how to establish an MQTT connection between two devices and how to convert objects to JSON files and back.
Technical Information
MQTT is a lightweight communication protocol designed for reliable data transfer between devices. Being data-agnostic, MQTT can send information in any format, including JSON. This example demonstrates how to send JSON messages using MQTT with two components: a publisher (JsonMQTTPublisher) and a subscriber (JsonTagsMqtt).
To send data, we must first set up the publisher following the steps below:
Go to Runtime → Startup and check the “Publish to Built-in MQTT Broker“ checkbox in the Execution Environment section. Click on the gear icon next to the checkbox, go to the Local/Remote tab and select a Listening Port.
Go to Unified Namespace → Tags and create two text tags, JObject and JArray.
Go to Devices → Channels and create an MQTT channel.
Go to Devices → Nodes and create an MQTT node. Select the PrimaryStation field, set BrokerURL to localhost, and use the same port number as in the first step.
In Devices → Points, assign the JObject and JArray tags to the MQTT node. Set the AccessType for both points to Write. Set the JObject Address to MQTT/Object, and the JArray Address to MQTT/Array.
Now the data in JObject and JArray can be published to topics MQTT/Object and MQTT/Array. Next, we need to serialize the tags. In Scripts → Classes, we have a class called ValueT. This class contains the following property, which serves as a template for our JSON:
private class JsonTemplate { public int Id { get; set; } public string Name { get; set; } public float Value { get; set; } }
Class ValueT’s Main function generates two random JsonTemplates (obj1 and obj2), followed by this piece of code:
// Creates JSON objects from obj1 and obj2 and creates a JSON array JArray jsonArray = new JArray { JObject.FromObject(obj1), JObject.FromObject(obj2) }; // Serializes obj1 to JSON string jsonString = JsonConvert.SerializeObject(obj1); // Assigns the generated values to the MQTT tags @Tag.JObject = jsonString; @Tag.JArray = jsonArray.ToString();
The JObject and JArray classes are defined in the Newtonsoft.Json.Linq namespace, and they allow you to represent JSON objects and arrays. Finally, in Scripts → Tasks, we create script TaskValues, which calls ValueT.Main every second, updating our tags and publishing their new values.
To retrieve the data published by JsonMQTTPublisher, we do the following in JsonTagsMqtt:
Go to Unified Namespace → Tags and create two Json tags, MQTTJson and MQTTJArray.
Go to Devices → Channels and create an MQTT channel.
Go to Devices → Nodes and create an MQTT node. Select the PrimaryStation field, set BrokerURL to localhost, and choose the same port number as the one used by the publisher.
In Devices → Points, assign the MQTTJson and MQTTJArray tags to the MQTT node. Set the AccessType for both points to Read. Set the MQTTJson Address to MQTT/Object, and the MQTTJArray Address to MQTT/Array.
Now the tags are subscribed to MQTT/Object and MQTT/Array, and the setup is done. To test the communication, run both solutions. Their JSON strings should appear in the cards under “Objects in Real Time.“ The six buttons on the right side of the display allow you to modify the saved JSON objects. Access the display’s CodeBehind to find out how each button works.
Reference Information
→ Check out MQTT Integration to find out more about the platform's MQTT functionalities.
→ Check out UNS Tags for more information about Tags and types.
In this section: