MQTT Client

MQTTMQTT Client - 图1 (opens new window) is a messaging protocol designed for IoT devices and applications operating on a publish/subscribe model. It’s lightweight, efficient, reliable, and allows for real-time communication. MQTT is well-suited for environments with limited resources, where efficient use of power and bandwidth is necessary.

Neuron supports MQTT as one of its communication protocols. The Neuron MQTT plugin allows users to quickly build IoT applications that use MQTT communication between devices and the cloud. Using the MQTT plugin, developers can also publish messages back to IoT devices, triggering device actions such as turning on or off lights, motors, and other equipments. The plugin also supports secure communication with devices, using authentication and encrypted communication protocols to ensure data safety and privacy.

Parameters

These are the available parameters when configuring a node using the MQTT plugin.

ParameterDescription
Client IDMQTT client id for communication, required.
QoS LevelMQTT QoS level for message delivery, optional, default QoS 0. (since 2.4.0)
upload-topicSubscription data reporting topic, required. (removed in 2.4.0)
Upload FormatJSON format of reported data, required. There are values-format and tags-format. In values-format, data are split into values and errors sub objects. In tags-format, tag data are put in a single array.
Write Request TopicMQTT topic to which the plugin subscribes for write requests. See MQTT API (since 2.4.5)
Write Response TopicMQTT topic to which the plugin sends write responses. (since 2.4.5)
Offline Data CachingOffline data caching switch. Cache MQTT messages when offline, and sync cached messages when back online. (Since 2.4.3)
Cache Memory SizeIn-memory cache limit (MB) in case of communication failure, required. Range in [0, 1024]. Should not be larger than cache-disk-size.
Cache Disk SizeIn-disk cache limit (MB) in case of communication failure, required. Range in [0, 10240]. If nonzero, cache-mem-size should also be nonzero.
Broker HostMQTT Broker host, required.
Broker PortMQTT Broker port number, required.
UsernameUsername to use when connecting to the broker, optional.
PasswordThe password to use when connecting to the broker, optional.
SSLWhether to enable MQTT SSL, default false.
CACA certificate, required when SSL enabled and using self signed certificates.
Client Certclient certificate, required when using SSL two way authentication.
Client Private Keyclient key, required when using SSL two way authentication.
Client Private Key Passwordclient key password, optional when using SSL two way authentication.

Data upload

Before Neuron version 2.4.0, the Neuron MQTT plugin will publish collected data in JSON to the topic designated by the upload-topic parameter.

Neuron version 2.3.0 upload topic setting

Fig.1 - Setting upload topic in Neuron version 2.3.0

Neuron version 2.4.0 removes the upload-topic parameter. Instead, users should provide the intended topic through the group subscription page. Click your MQTT node to enter the group subscription page, and click Add Subscription to add a subscription.

Neuron version 2.4.0 MQTT subscribe interface

Fig.2 - Setting upload topic in Neuron version 2.4.0

The exact format of the data reported is controlled by the Upload Format parameter. There are two formats, tags-format and values-format.

For more detail information, see MQTT API

Offline data caching

Offline data caching is a feature that allows data to be stored locally on the device running Neuron during network outage. When a network connection becomes available, the MQTT plugin can synchronize data with the broker. This feature is useful in scenarios where network connectivity may be limited, unstable, or intermittent. And provides a critical functionality that can enhance the robustness and reliability of applications built on Neuron.

Offline data caching is controlled by the Offline Data Caching, Cache Memory Size, and Cache Disk Size parameters. Setting the Offline Data Caching parameter to true switches on offline data caching, in which case, users should also set the Cache Memory Size and Cache Disk size parameters. The Cache Memory Size parameter specified the memory cache size in megabytes, and the max allowed memory cache size is 1GB. The Cache Disk Size parameter specified the disk cache size in megabytes, and the max allowed disk cache size is 10GB. When network disruption occurs, the MQTT plugin will store data in the memory cache first, and only flush data to the disk cache when memory cache is full. When network connection restores, the MQTT plugin will publish the cached data to the broker in FIFO (Fist In First Out) order.

TIP

The Offline Data Caching parameter is added since Neuron version 2.4.3. For For older versions, you may disable offline data caching by setting both Cache Memory Size and Cache Disk Size to zero.

The following table gives some statistics on disk space usage for offline caching (using the Modbus TCP plugin). The first column is the number of tags in each MQTT message, and the second column is the MQTT message payload size in bytes for that many tags. The third column is the number of cached messages in disk, and the fourth column is the resulting disk space usage in kilobytes.

Tags / MessageMessage Payload (Bytes)Cached MessagesUsed Disk Size (KB)
1021910064
102191000349
1001284100228
100128410002077
1000129931001401
100012993100013427

Data security

SSL/TLS (Secure Sockets Layer/Transport Layer Security) is a security protocol used to encrypt communication channels between networked devices. It enables secure communication over an insecure network, such as the internet. MQTT over SSL/TLS is a secure method for transmitting MQTT messages between client and the MQTT broker by encrypting the data being transmitted with SSL/TLS encryption. This ensures that all data passed between the clients and the broker are encrypted and secure.

The Neuron MQTT plugin supports running MQTT over SSL. To enable SSL encryption, turn on the SSL parameter when configuring the node. If using self signed certificates, the CA parameter should be provided with the server-side CA certificate. The certificate of the broker you are connecting to should be issued by the provided CA. And if you are using two-way authentication, you should also provide the client certificate, key file, and key file password through the Client Cert, Client Private key, and Client Private Key Password parameters respectively.

Setting Parameters of MQTT client.

Configure the parameters required for MQTT client.

Click the Application Configuration icon on the application card to enter the application configuration interface to set MQTT connection, as shown in the figure below.

Connect to MQTT

  • Client-id:Note that each ID should be independent of each other and cannot be repeated. Use the default value of mqtt;
  • Qos level:Quality of Service, divided into 0, 1, 2;
  • Host:Use the default public EMQX Broker (broker.emqx.io);
  • Port:Use MQTT broker port(1883);
  • Click Submit to complete the configuration of northbound application, and the application card will automatically enter the working state of Running.

Check The Data By MQTTX.

After the subscription is completed, you can use the MQTT client (MQTTX is recommended and can be downloaded from official websiteMQTT Client - 图5 (opens new window)) to connect to the public emqx proxy to view the reported data, as shown in the following figure.

mqttx

After subscribing to the corresponding topic in MQTTX, you can see that the data reported by Neuron can be continuously received in this topic.

  • open MQTTX to add a new connection, correctly fill in the name and the Host and Port of the public EMQX Broker, and complete the connection;
  • Add a new subscription, and the Topic should be consistent with the Upload topic in setting northbound application parameters, for example, fill in /neuron/mqtt/upload.

TIP

The default topic format for uploading topic is /neuron/{node_name}/upload, where {node_name} is the name of the created northbound application. Users can also customize the reporting theme.