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

Compare with Current View Page History

« Previous Version 46 Next »

Summary

MQTT is an open OASIS and ISO standard lightweight, publish-subscribe network protocol that transports messages between devices. You can find more information about the MQTT protocol following the link.

Configuration

Sub-menu: /iot mqtt

noteiot package is required.

IoT package is available with RouterOS version 6.48.3. You can get it from our download page - under "Extra packages".

You can find more application examples for MQTT publish scenarios and our KNOT product following the links below:

a) MQTT/HTTPS example with AWS cloud platform

b) MQTT example with Azure cloud platform

The settings shown in the examples above apply to any RouterOS device. The only thing that needs to be changed/altered is the script because the script (in those examples) collects Bluetooth information. Currently, only the KNOT supports Bluetooth. For RouterOS devices other than KNOT - you can use the script shown in this article.

Broker

To add a new broker, run the following command:

[admin@device] /iot mqtt brokers add

Configurable properties are shown below:

PropertyDescription
address (IP|hostname; Default: )IP address or hostname of the broker
certificate (string; Default: )
The certificate that is going to be used for the SSL connection

client-id (string; Default: )

A unique ID used for the connection. The broker uses this ID to identify the client.

name (string; Default: )

Descriptive name of the broker

password (string; Default: )

Password for the broker (if required by the broker)

port (integer:0..4294967295; Default: 1883)

Network port used by the broker

ssl (yes | no; Default: no)

Secure Socket Layer configuration
username (string; Default: )Username for the broker (if required by the broker)

Publish

PropertyDescription
broker (string; Default: )
Select the broker, where to publish the message

message (string; Default: )

The message that you wish to publish to the broker

qos (integer:0..4294967295; Default: 0)

Quality of service parameter, as defined by the broker

retain (yes | no; Default: no)

Whether to retain the message or to discard it if no one is subscribed to the topic. This parameter is defined by the broker.

topic (string; Default: )

Topic, as defined by the broker

An example of MQTT publish would look like this:

[admin@device] /iot mqtt> publish broker=AWS topic=my/test/topic message="{\"temperature\":15}"    

In this case, AWS is a broker's name that was configured in the broker section, my/test/topic is a topic (as it is configured on the server-side/on the broker itself) and "{\"temperature\":15}" is the message you wish to publish (in this specific example, in the JSON format). Retain and QoS parameters are optional - both are defined by the broker.

In this scenario, our broker is AWS.

In order to see the displayed message, you need to subscribe to the topic beforehand (in our case, my/test/topic).

Once you are subscribed to the topic, you can publish the message. AWS (or any other broker) should display the message:


You can also use scripts (to automate the process). For example, you can run a script like this:

# Required packages: iot

################################ Configuration ################################
# Name of an existing MQTT broker that should be used for publishing
:local broker "AWS"

# MQTT topic where the message should be published
:local topic "my/test/topic"

#################################### 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")

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:

Do not forget to change the "Configuration" part of the script based on your settings.

  • No labels