You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Introduction

One of the many cloud services that you can use to monitor information that is sent by an MQTT publisher is Thingsboard. This article will demonstrate how to configure both Thingsboard and RouterOS to publish the data using the MQTT protocol. RouterOS, in this scenario, is going to act as a gateway and publish the data from the RouterBoard to the Thingsboard's server. Thingsboard, in this scenario, will act as an MQTT broker (server, where data will be posted).

Before we proceed with the settings, you need to create an account in the Thingsboard's system. You can do so following this link.

Thingsboard configuration

After you have created an account, go to the "Device Groups" menu and create a new group:

To do that, by being in the "Device Groups" section, click on the add button "+" and enter the "Name" of the group and "Description" (if required) and click on "Add".

As a result, a new group should be created:

Choose the newly created group by clicking on it and, then, navigate to the "Open entity group" section:

Create a new device for the group by clicking on the add button "+", and enter the "Name" for the device:

Double-check that the "Device Profile" selected for the device has MQTT support included:

By default, access token authentication is selected for the newly created device:

Access token scenario

You can change the token by clicking on the "Manage Credentials" button:

This token will be used as a "username" for the MQTT publisher (in RouterOS settings).

You can find more information following the link.

MQTT Basic scenario

You can change the credentials type in the "Device Credentials" section for the specific device:

MQTT Basic scenario allows you to specify Client ID, Username, and Password for the MQTT authentication.

You can find more information following the link.

X.509 scenario

You can change the credentials type in the "Device Credentials" section for the specific device:

X.509 scenario allows you to use the certificate for authentication.

You can find more information following the link. Once the certificate is generated (for example, using OPEN SSL), copy the RSA public key into the field and click on the "Save" button.

RouterOS configuration

note: In order to configure MQTT, make sure that iot package is installed beforehand.

In our example, we are using KNOT as a gateway and a script (created specifically for the KNOT) to publish the data. In your specific application scenario, you can use any RouterOS device (with iot package installed) to post any data that you need.

MQTT Broker

Access token scenario

Navigate to IoT>MQTT and add a new broker ("+" button):

  • Name the broker in the "Name" field.
  • Type in/paste Thingsboard server's hostname address into the "Address" field.
  • Configure the port used by the broker in the "Port" field.
  • Type in/paste the access token from the Thingsboard into the "Username" field ("Device Credentials" configuration).

The rest of the parameters are not required for the access token scenario.

Click on "Apply" and "OK" to finish setting up the broker.

MQTT Basic scenario

Navigate to IoT>MQTT and add a new broker ("+" button):

  • Name the broker in the "Name" field.
  • Type in/paste Thingsboard server's hostname address into the "Address" field.
  • Configure the port used by the broker in the "Port" field.
  • Type in/paste the username from the Thingsboard into the "Username" field ("Device Credentials" configuration).
  • Type in/paste the password from the Thingsboard into the "Password" field ("Device Credentials" configuration).
  • Type in/paste the Client ID from the Thingsboard into the "Client Id" field ("Device Credentials" configuration).

Click on "Apply" and "OK" to finish setting up the broker.

X.509 scenario

Before we configure the MQTT broker, we need to import the certificates generated for this scenario.

You need to get and import the certificate chain (as per the Thingsboard guide - certificate chain for mqtt.thingsboard.cloud is located here) and generate the certificate with the key (you can use OPEN SSL tool for this task).

Go to the "Files" menu and add the certificates there - downloaded chain, previously generated certificate, and its private key.

Go to System>Certificates and import all 3 files (via the "Import" button - one by one):


Make sure that the certificate is trusted (T) and that the private key (K) was added.

Navigate to IoT>MQTT and add a new broker ("+" button):

  • Name the broker in the "Name" field.
  • Type in/paste Thingsboard server's hostname address into the "Address" field.
  • Configure the port used by the broker in the "Port" field (for SSL connection, the "8883" port should be used).
  • Enable the "SSL" checkbox.
  • Select the certificate in the "Certificate" field.

Click on "Apply" and "OK" to finish setting up the broker.

MQTT Publish

In order to publish the data from the RouterOS to the Thingsboard, we will be using the script shown below. The script collects the data from the RouterOS device (model name, serial number, RouterOS version, current CPU, used memory, free memory, and uptime) and publishes the message (the data) to the broker in the JSON format:

# Required packages: iot

################################ Configuration ################################
# Name of an existing MQTT broker that should be used for publishing
:local broker "mqtt.thingsboard.cloud"

# MQTT topic where the message should be published
:local topic "v1/devices/me/telemetry"

#################################### System ###################################
:put ("[*] Gathering system info...")
:local cpuLoad [/system resource get cpu-load]
:local freeMemory [/system resource get free-memory]
:local usedMemory ([/system resource get total-memory] - $freeMemory)
:local rosVersion [/system package get value-name=version \
    [/system package find where name ~ "^routeros"]]
:local model [/system routerboard get value-name=model]
:local serialNumber [/system routerboard get value-name=serial-number]
:local upTime [/system resource get uptime]

#################################### MQTT #####################################
:local message \
    "{\"model\":\"$model\",\
                \"sn\":\"$serialNumber\",\
                \"ros\":\"$rosVersion\",\
                \"cpu\":$cpuLoad,\
                \"umem\":$usedMemory,\
                \"fmem\":$freeMemory,\
                \"uptime\":\"$upTime\"}"

:log info "$message";
:put ("[*] Total message size: $[:len $message] bytes")
:put ("[*] Sending message to MQTT broker...")
/iot mqtt publish broker=$broker topic=$topic message=$message
:put ("[*] Done")

2 script lines should be taken into account.

:local broker "mqtt.thingsboard.cloud"

line, where you should specify the broker's name within the quotation marks "" (check Thingsboard's documentation for the exact hostname that needs to be used).

:local topic "v1/devices/me/telemetry"

line, where you should specify the correct topic within the quotation marks "" (check Thingsboard's documentation for the exact topic that needs to be used).

The rest of the script configuration depends on the overall requirements.

Navigate to System>Scripts, add a new script there, and paste the script that is shown above (name it, for example, script1).

To run the script, you can use the command line:

/system script run script1

Alternatively, if you require to post, for example, Bluetooth tag payloads to the broker, you can use the scripts shown in our other MQTT guide following the link. Just make sure you change the 2 lines mentioned above.

Verification

You can check the received/published data for the device under the "Latest telemetry" section:

  • No labels