Sending messages

You can send the following types of messages:

  • Send data from a device to a registry using the $devices/<device ID>/events or $registries/<registry ID>/events topics.
  • Send registry commands to a device using the $devices/<device ID>/commands or $registries/<registry ID>/commands topics.

To receive messages, you need to subscribe to the sender. For information about how to do this, see Subscribing a device or registry to receive messages.

Warning

Registry and device topics are not interconnected. If a device sends data to the device topic for telemetry data, you can only receive it by subscribing to this topic. The same is true for registry topics.

Before you start

Prepare the resources you need to get started:

  1. (optional) Create registry and device certificates.
  2. Create a registry.
  3. Add a certificate to the registry.
  4. Create a device.
  5. Add a certificate to the device.

Authorization

To send messages, you need to log in. To do this, use an X.509 certificate or username and password. This section provides examples for both authorization methods.

Sending messages with data

Note

If you encounter an error while running the command, add the --debug flag to the command and try again. This flag outputs the debug log when running the command, which helps you diagnose the problem.

Send data from a device to a device topic

Registries subscribed to this topic will know which device sent the data, because the topic contains a unique device ID.

If you don't have the Yandex.Cloud command line interface yet, install it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

  • Send data using a certificate:

    $ yc iot mqtt publish \
    --cert device-cert.pem \
    --key device-key.pem \
    --topic '$devices/<device ID>/events' \
    --message 'Test data' \
    --qos 1
    

    Where:

    • --cert and --key: Parameters for authorization using a certificate.
    • --topic: Device topic for sending data.
    • --message: Message text.
    • --qos: Quality of service (QoS).
  • Send data using a username and password:

    $ yc iot mqtt publish \
    --username <device ID> \
    --password <device password> \
    --topic '$devices/<device ID>/events' \
    --message 'Test data' \
    --qos 1
    

    Where:

    • --username and --password: Parameters for authorization using a username and password.
    • --topic: Device topic for sending data.
    • --message: Message text.
    • --qos: Quality of service (QoS).

Send data from a device to a registry topic

Registries subscribed to this topic will not know which device sent the data, because the topic doesn't contain a unique device ID.

If you don't have the Yandex.Cloud command line interface yet, install it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

  • Send data using a certificate:

    $ yc iot mqtt publish \
    --cert device-cert.pem \
    --key device-key.pem \
    --topic '$registries/<registry ID>/events' \
    --message 'Test data' \
    --qos 1
    

    Where:

    • --cert and --key: Parameters for authorization using a certificate.
    • --topic: Registry topic for getting data.
    • --message: Message text.
    • --qos: Quality of service (QoS).
  • Send data using a username and password:

    $ yc iot mqtt publish \
    --username <device ID> \
    --password <device password> \
    --topic '$registries/<registry ID>/events' \
    --message 'Test data' \
    --qos 1
    

    Where:

    • --username and --password: Parameters for authorization using a username and password.
    • --topic: Registry topic for getting data.
    • --message: Message text.
    • --qos: Quality of service (QoS).

Sending messages with commands

A registry can send messages with commands to one, multiple, or all devices added to it. Let's look at all the options.

Note

If you encounter an error while running the command, add the --debug flag to the command and try again. This flag outputs the debug log when running the command, which helps you diagnose the problem.

Send a command to a single device

If you don't have the Yandex.Cloud command line interface yet, install it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

  • Send a command using a certificate:

    $ yc iot mqtt publish \
    --cert registry-cert.pem \
    --key registry-key.pem \
    --topic '$devices/<device ID>/commands' \
    --message 'Test command for first device' \
    --qos 1
    

    Where:

    • --cert and --key: Parameters for authorization using a certificate.
    • --topic: Device topic for getting commands.
    • --message: Message text.
    • --qos: Quality of service (QoS).
  • Send a command using a username and password:

    $ yc iot mqtt publish \
    --username <registry ID> \
    --password <registry password> \
    --topic '$devices/<device ID>/commands' \
    --message 'Test command for first device' \
    --qos 1
    

    Where:

    • --username and --password: Parameters for authorization using a username and password.
    • --topic: Device topic for getting commands.
    • --message: Message text.
    • --qos: Quality of service (QoS).

Send a command to all devices added to the registry

If you don't have the Yandex.Cloud command line interface yet, install it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

  • Send a command using a certificate:

    $ yc iot mqtt publish \
    --cert registry-cert.pem \
    --key registry-key.pem \
    --topic '$registries/<registry ID>/commands' \
    --message 'Test command for all devices' \
    --qos 1
    

    Where:

    • --cert and --key: Parameters for authorization using a certificate.
    • --topic: Registry topic for sending commands.
    • --message: Message text.
    • --qos: Quality of service (QoS).
  • Send a command using a username and password:

    $ yc iot mqtt publish \
    --username <registry ID> \
    --password <registry password> \
    --topic '$registries/<registry ID>/commands' \
    --message 'Test command for all devices' \
    --qos 1
    

    Where:

    • --username and --password: Parameters for authorization using a username and password.
    • --topic: Registry topic for sending commands.
    • --message: Message text.
    • --qos: Quality of service (QoS).