How To Configure ThingsBoard with MQTT/HTTP Integrations via WisGateOS2

The Built-in Network server of the WisGate Edge V2 gateways provides MQTT and HTTP integrations. Integration helps the user to post-process the data and implement solutions based on their needs. This article will show you how to create an MQTT or HTTP integration to ThingsBoard using the built-in network server.

MQTT Integration

MQTT is a publish/subscribe protocol that allows data transfer to the desired MQTT broker. In this article, two examples will be shown:

  • Connecting to the local MQTT broker on the gateway
  • Connecting to an external broker

In the MQTT Integration, the ThingsBoard acts as an MQTT client.

Configure ThingsBoard

Login to your ThingsBoard account. If you do not have an account yet, then create one first.

KHM-015-01.png

After a successful login, you will see the ThingsBoard Overview page.

KHM-015-02.png

The first thing you need is a data converter for the uplinks. To create one, head to the Data converters menu.

KHM-015-03.png

To add a data converter, click the Add Data Converter icon and choose to Create new converter.

KHM-015-04.png

KHM-015-05.png

In the Name field, type a name for the decoder (e.g. Uplink decoder). Leave the Type as Uplink. You can also use the default decoder code provided by ThingsBoard.

/** Decoder **/

// decode payload to string
var payloadStr = decodeToString(payload);
var data = JSON.parse(payloadStr);

var deviceName =  'RAK7204';
var result = {
   deviceName: deviceName,
   attributes: {
       integrationName: metadata['integrationName'],
       temperature: data.temperature,
       humidity: data.humidity,
       barometer: data.barometer,
       gas_resistance: data.analogInput,
   },
};

/** Helper functions **/

function decodeToString(payload) {
   return String.fromCharCode.apply(String, payload);
}

function decodeToJson(payload) {
   // convert payload to string.
   var str = decodeToString(payload);

   // parse string to JSON
   var data = JSON.parse(str);
   return data;
}
return result;

Click Add to add the uplink decoder.

Now that the uplink data converter is ready, you can proceed to add the MQTT integration. Click the Integrations menu on the left tab.

KHM-015-06.png

Click the Add Integration button to add one.

KHM-015-07.png

In the Name field, give a name of the integration (e.g. MQTT Integration). In the Type field, select MQTT from the drop-down menu.

📝 NOTE: Selecting the type of integration pops up settings for the specified type.

KHM-015-08.png

For the Uplink data converter, choose the decoder created earlier. The Host is the address of the MQTT broker which will be used for the messages.

📝 NOTE: Check the Debug mode to track events while configuring the integration.

External MQTT Broker

The first example will be with an external MQTT broker. If you want to use the local broker, proceed to the Local MQTT broker section.

The external broker used in this example is broker.hivemq.com. It is a free public MQTT broker, ideal for testing. You can use other ones or your own broker, then click the Topic filters to set the subscription topics to which the ThingsBoard client must subscribe.

For WisGate Edge gateways, the topics are as follows:

  • application/{{application_ID}}/device/{{device_EUI}}/join
  • application/{{application_ID}}/device/{{device_EUI}}/rx
  • application/{{application_ID}}/device/{{device_EUI}}/tx
  • application/{{application_ID}}/device/{{device_EUI}}/ack
  • application/{{application_ID}}/device/{{device_EUI}}/status
  • mcast_group/{{mcast_ID}}/tx

Where application_ID, device_EUI, and mcast_ID are the ID of the application, the EUI of the device, and the ID of the multicast group, respectively.

KHM-015-09.png

📝 NOTE: You need to change the values in the topics in the ThingsBoard. For example, application/1/device/0123456789ABCDEF/join.

Configure the Gateway

Now that the ThingsBoard is configured, you need to configure the gateway. Start by accessing the gateway. You can see how to do it in the WisGateOS V2 user manual.

KHM-015-010.png

Once you log in, head to the LoRa menu.

KHM-015-011.png

By default, the gateway works as a built-in network server. If that is not the case, check the Built-in Network Server Mode Settings in the WisGateOS V2 User manual to switch the mode to Built-in network server.

Once the gateway is in Built-in network server mode, head to the Applications tab.

KHM-015-012.png

Click the Add application button or add one now link to add a new application. On the new page, fill in the following information:

  • Application name – type a name for the application.
  • Application description – optionally, you can write a description of the application.
  • Application Type – from the drop-down menu select the type of application.
    • Unified Application key – all devices will use the same application key. Selecting this option pops up an Application Key field. You can type your application key there or use the Autogenerate button to generate one.

KHM-015-013.png

📝 NOTE: The Auto Add Device switch pops up an Application EUI field. The device will be automatically added to the application after application EUI and key verification.

KHM-015-014.png

    • Separate Application keys – each device will have its own application key. The key is added when registering the device.
  • Payload type – from the drop-down select CayenneLPP payload type and turn on the Only forward data object feature.

KHM-015-015.png

Once done setting up, click Save Application to add the application.

After the application is added, head to the End devices tab. If you are using the Auto Add Device feature, the devices should automatically register upon join request.

KHM-015-016.png

If that’s not the case, click the Add end device button. On the End device information page, fill in the following information:

KHM-015-017.png

  • Activation Mode – choose the activation mode of your device – OTAA or ABP. ABP mode pops up two additional fields for the Application Session Key and Network Session Key.
  • End device (group) name – name of the device.
  • End device description (optional) – optionally, you can add description for the device.
  • Class – the class of the device.
  • Frame Counter width – the width of the frame counter. Leave as default.
  • LoRaWAN MAC Version – the LoRaWAN MAC version. V1.0.2 pops up a LoRaWAN Regional Parameters reversion field where you need to select the reversion of the device.

KHM-015-018.png

Once everything is set, click Add end devices to go the page to add the device.

KHM-015-019.png

In the Adding end devices page, type the device EUI at the End Device EUI (main) and click Add to “End Devices list”.

📝 NOTE:
  • If the EUI is correct, the device will show in the End devices list.
  • If the EUI is not correct, the devices will show in the End devices with an error. :::

  • Once the device is added to the End devices list, click Add end devices to add the device. Confirm you are adding the device.

KHM-015-020.png

You will be redirected to the End devices page, where the device is added.

Set the MQTT Integration

After your device joins, head to LoRa -> Integration Interface Parameters section.

Turn on the integration by clicking Enable Integration Interface switch. By default, the Integration mode is Generic MQTT. If that is not the case, select the Generic MQTT as the integration mode.

KHM-015-021.png

The default MQTT Broker is the built-in one (127.0.0.1). To point the MQTT client to the hivemq broker, you need to type broker.hivemq.com in the MQTT Broker Address field and then click Save changes.

After your device joins and sends uplink data, you can see it in ThingsBoard -> Integrations -> Your Integration -> Events.

KHM-015-022.png

The MQTT integration is ready. You can now proceed with processing the data.

Local MQTT Broker

The gateway comes with a built-in broker. You can connect the ThingsBoard client to the local broker.

📝 NOTE:
  • For the connection to be successful, you need to put the gateway on DMZ or set a static IP address on the gateway and forward the 1883 port.
  • When setting a DMZ, the gateway is exposed to the public network, so make sure you set a secured password to protect your device.

To connect to the broker, find your public IP and paste it into the Host field.

Click Topic filters to set the subscription topics to which the ThingsBoard client must subscribe. For WisGate Edge gateways the topics are as follows:

  • application/{{application_ID}}/device/{{device_EUI}}/join

  • application/{{application_ID}}/device/{{device_EUI}}/rx
  • application/{{application_ID}}/device/{{device_EUI}}/tx
  • application/{{application_ID}}/device/{{device_EUI}}/ack
  • application/{{application_ID}}/device/{{device_EUI}}/status
  • mcast_group/{{mcast_ID}}/tx

Where application_ID, device_EUI, and mcast_ID are the ID of the application, EUI of the device, and the ID of the multicast group, respectively.

KHM-015-023.png

Configure the Gateway (Local MQTT)

To configure the gateway, follow the steps on the Configure the Gateway section.

Set the MQTT Integration (Local MQTT)

After your device joins, head to LoRa -> Integration Interface Parameters section.

Turn on the integration by clicking Enable Integration Interface switch. By default, the Integration mode is Generic MQTT. If that is not the case, select it manually.

KHM-015-024.png

The default MQTT Broker is the built-in one (127.0.0.1). Check if the device default broker is selected. Leave everything else to default, then click Save changes.

After your device joins and sends uplink data, you can see it in ThingsBoard -> Integrations -> Your Integration -> Events.

KHM-015-025.png

The MQTT integration is successful. You can now proceed with processing the data.

HTTP Integration

The built-in network server provides HTTP integration. The HTTP integration converts existing protocols and payload formats to the ThingsBoard message format.

Configure ThingsBoard (HTTP Integration)

Login to your ThingsBoard account. If you do not have an account yet, then create one first.

KHM-015-026.png

After a successful login, you will see the ThingsBoard Overview page.

KHM-015-027.png

The first thing you need is a data converter for the uplinks. To create one, head to the Data converters menu.

KHM-015-028.png

To add a data converter, click the Add Data Converter icon () and choose to Create new converter.

KHM-015-029.png

KHM-015-030.png

📝 NOTE: Check the Debug mode to track events while configuring the integration.

In the Name field, type a name for the decoder (e.g. Uplink decoder). Leave the Type as Uplink. You can also use the default decoder code provided by ThingsBoard.

/** Decoder **/

// decode payload to string
var payloadStr = decodeToString(payload);
var data = JSON.parse(payloadStr);

var deviceName =  'RAK7204';
var result = {
   deviceName: deviceName,
   attributes: {
       integrationName: metadata['integrationName'],
       temperature: data.temperature,
       humidity: data.humidity,
       barometer: data.barometer,
       gas_resistance: data.analogInput,
   },
};

/** Helper functions **/

function decodeToString(payload) {
   return String.fromCharCode.apply(String, payload);
}

function decodeToJson(payload) {
   // convert payload to string.
   var str = decodeToString(payload);

   // parse string to JSON
   var data = JSON.parse(str);
   return data;
}
return result;

Click Add to add the uplink decoder.

Now that the uplink data converter is ready, you can proceed to add the HTTP integration. Click the Integrations menu on the left tab.

KHM-015-031.png

Click the Add Integration button to add one.

KHM-015-032.png

In the Name field, give a name of the integration (e.g. HTTP Integration). In the Type field, select HTTP from the drop-down menu.

📝 NOTE: Selecting the type of integration pops up settings for the specified type.

KHM-015-033.png

Enable the Debug mode. The debug mode is useful for development and troubleshooting.

For Uplink data converter, choose the created decoder earlier.

While creating the integration, you can copy the HTTP endpoint URL, as it will be needed later.

Click Add to create the integration.

Configure the Gateway (HTTP Integration)

To configure the gateway, follow the steps on the Configure the Gateway section.

Set the HTTP Integration

After your device joins, head to LoRa Network -> Applications -> Select your application -> Configuration -> Integration Parameters.

Turn on the integration by clicking Enable HTTP/HTTPS Integration switch. This will pop up the HTTP/S integration field settings.

KHM-015-034.png

In the Uplink data, Join notification, Ack notification, and Device-status notification URLs fields, paste the end point URL copied above.

📝 NOTE:
  • Remove the ‘s’ from the HTTPS link: http://thingsboard.cloud/api/v1/integrations/http/3eb7f351-7b7c-XXXX-XXXX-XXXXXXXXXXXX

If you want to see only the uplink messages, you can place the endpoint URL only in the Uplink data field.

KHM-015-035.png

Click Save & Apply to save the changes. You should see the uplink messages in the Event log.

KHM-015-036.png

Data Visualization From the End-Device

In this example, the RAK7204 WisNode Sense is the device used. The method to visualize the data for other devices is the same.

After creating the data converter, integration and getting some data in the Events tab, this automatically creates a device in the default device group based on the decoder. Head to the Device group menu.

KHM-015-037.png

By default, in the Device groups menu, you will find only one group named All. Click the Open icon.

KHM-015-038.png

You can find the created device. Just click on the device and head to the Attributes tab, and you will see the data from the node.

KHM-015-039.png

To visualize the data, simply select the desired value you want to visualize and then click the Show on widget button. In this example, choose temperature.

KHM-015-040.png

On the next page, from Current bundle drop-down menu, choose a desired widget for the data. For this example, choose Analogue gauges as the analog gauges have a widget for the temperature.

KHM-015-041.png

After choosing a widget, click Add to dashboard.

KHM-015-042.png

By default, the profile does not have a dashboard. You will need to create a new one. Choose Create new dashboard and type a name for the dashboard in the New dashboard title field. For example, RAK7204.

In that dashboard, you can add more widgets for the device. When you set the name for the dashboard, click Add button to add the widget. Optionally, you can check the Open dashboard option. This will automatically open the created dashboard after the widget is added.

📝 NOTE: If you have not checked the Open dashboard option, you can easily navigate to Dashboard groups menu -> All -> <Name_of_your_group> to see the added widget.

KHM-015-043.png

You can do the steps above for the other data of the device. The only difference is when adding a widget. Click Select existing dashboard and select the already created dashboard (in this case, RAK7204).

KHM-015-044.png

Updated