Skip to main content
Version: 2.6

MQTT Payload

This examples shows how to generate a MQTT payload.

Preconditions

  1. The example is based on the Mazak Connector Example. Ensure that the IoT Gateway for Mazak is ready to run.
  2. Install a MQTT Client for verification. The example uses MQTT Explorer.

Step 1: Prepare the IoT Gateway for MQTT

  1. Select the Plugins node in the project view and click [+] button.

  2. Add the MqttServer (broker) to the project.

    NOTE

    This step can be skipped, if you want to use an external broker, such as mosquitto.

  3. Open the configuration of the MQTT server - double click on the HumanOS.PeSeL.MqttServer plugin.

    Select the Plugin Configuration on the document view (to the right), and click [+] button on the document.

    Use the default settings:

Step 2: Add MQTT Client as DataLogger

  1. Select the Plugins node in the project view and click [+] button.

  2. Add the MqttClient to the project.

  3. Open the configuration of the MQTT client - double click on the HumanOS.PeSeL.MQTTClient plugin.

    Select the Plugin Configuration on the document view (to the right), and click [+] button on the document.

  4. Click on the added Publisher and set the payload script name to MqttPayloadScript.cs.

    Additionally, change

    • the Sending to Topic to mazak/data.
    • Sampling rate to 0

Step 3: Prepare Mazak Device Template for MQTT

  1. Open the device template and select the data node OperationMode and PartCounter.

    Add the property EnableMqtt to the data nodes. Set them to true.

    NOTE

    This is used to filter the relevant nodes for MQTT.

    Hints

    • Reassign the changed device template to your Heidenhain device.

Step 3: Write C# Script

Next step is to write the processing script.

  1. Select the Plugin HumanOS.PeSeL.MQTTClient node in the project view and click [+] button.

  2. Add the blank data logger script named MqttPayloadScript.cs to the project.

  3. Open the script file. There are three methods:

  • initialize() is called when the plugin is initialized (first time only)
  • processPayload() is called each time a data package must be sent to the MQTT server
  • postProcess() is called after successfully sending.

For this example we use only the processPayload() method.

  1. Implement a simple JSON payload processing

    Usings:

    using HumanOS.Kernel;
    using HumanOS.Kernel.PeSeL.DataLogger;
    using HumanOS.Kernel.PeSeL.Script;
    using HumanOS.Kernel.Utils;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    Code:

    public override byte[][] processPayload(IKernelAccess Kernel, TPayloadProcessingContext Context, List<TDataSet> lstData)
    {
    List<byte[]> lstRetval = new List<byte[]>();

    foreach(TDataSet DataSet in lstData)
    {
    JObject jRoot = new JObject();
    jRoot.Add("DeviceId", DataSet.getFieldValue<Guid>("DeviceId"));
    jRoot.Add("Id", DataSet.getFieldValue<Guid>("Id"));
    jRoot.Add("State", DataSet.getFieldValue<int>("State"));
    jRoot.Add("TimeStamp", DataSet.getFieldValue<DateTime>("TimeStamp").ToBinary());
    jRoot.Add("DataType", DataSet.getFieldValue<Type>("DataType").FullName);
    jRoot.Add("Name", DataSet.getFieldValue<string>("Name"));
    jRoot.Add("Value", TValueConverter.convertToString(DataSet.Fields.First(n => n.Name == "Value").Value));
    lstRetval.Add(Encoding.UTF8.GetBytes(jRoot.ToString()));
    }

    return lstRetval.ToArray();
    }

Step 4: Deploy and Test

  1. Deploy and run HumanOS IoT Gateway

  2. Start the MQTT Explorer and connect to the gateway localhost and port 1883. Check the topic mazak/data

Extension Step 1

This example shows how to use multiple topics for data nodes within the same MQTT data logger.

NOTE

Currently, all data nodes are published to one topic called mazak/data.

  1. Open the script file MqttPayloadScript.cs and add the lines above the foreach.

     if (lstData.Count > 0)
    {
    string strTopicName = $"mazak/data/{lstData[0].getFieldValue<string>("Name")}";
    Context.setValue("Topic", strTopicName);
    }

    This allows to change the topic dynamically.

  2. Deploy and run HumanOS IoT Gateway