Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
Yandex IoT Core
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Sending messages
    • Subscribing a device or registry to receive messages
    • Viewing the connection log
    • Creating a trigger for a topic
    • Managing registries
      • Getting information about registries
      • Creating a registry
      • Updating a registry
      • Deleting a registry
    • Managing devices
      • Managing topic aliases
        • Creating an alias
        • Getting a list of aliases
        • Updating an alias
        • Deleting an alias
      • Getting information about devices
      • Creating a device
      • Updating a device
      • Deleting a device
    • Managing brokers
      • Getting information about a broker
      • Creating a broker
      • Updating a broker
      • Deleting a broker
    • Managing certificates
      • Create a certificate
      • Managing registry certificates
      • Managing device certificates
      • Managing broker certificates
    • Managing passwords
      • Managing registry passwords
      • Managing device passwords
      • Managing broker passwords
  • Concepts
    • Authorization
    • Relationship between service resources
    • MQTT server connection parameters
    • Sending messages to an MQTT server via gRPC
    • Topic
      • Overview
      • Device topics
      • Registry topics
      • Subtopic
      • Using topics
    • Backups
    • Quotas and limits
  • Practical guidelines
    • All tutorials
    • Working with Mosquitto
      • Sending messages
      • Subscribing a device or registry to receive messages
    • Working with Yandex IoT Core from an Android device in Java
    • Working with Yandex IoT Core in C#
    • Working with Yandex IoT Core in Java
    • Writing data from a device to Managed Service for PostgreSQL
  • Access management
  • Pricing policy
  • Devices API reference
    • Authentication in the API
    • gRPC
      • Overview
      • DeviceDataService
      • DeviceService
      • RegistryDataService
      • RegistryService
      • OperationService
    • REST
      • Overview
      • DeviceData
        • Overview
        • publish
      • Device
        • Overview
        • addCertificate
        • addPassword
        • create
        • delete
        • deleteCertificate
        • deletePassword
        • get
        • getByName
        • list
        • listCertificates
        • listOperations
        • listPasswords
        • update
      • RegistryData
        • Overview
        • publish
      • Registry
        • Overview
        • addCertificate
        • addPassword
        • create
        • delete
        • deleteCertificate
        • deletePassword
        • get
        • getByName
        • list
        • listCertificates
        • listDeviceTopicAliases
        • listOperations
        • listPasswords
        • update
  • Broker API reference
    • Authentication in the API
    • gRPC
      • Overview
      • BrokerDataService
      • BrokerService
      • OperationService
    • REST
      • Overview
      • BrokerData
        • Overview
        • publish
      • Broker
        • Overview
        • addCertificate
        • addPassword
        • create
        • delete
        • deleteCertificate
        • deletePassword
        • get
        • list
        • listCertificates
        • listOperations
        • listPasswords
        • update
  • Questions and answers
  1. Step-by-step instructions
  2. Sending messages

Sending messages

Written by
Yandex Cloud
  • Before you start
    • Authorization
  • Sending a message with data
    • Send data from a device to a device topic
    • Send data from a device to a registry topic
  • Sending messages with commands
    • Send a command to a single device
    • Send a command to all devices added to the registry
  • Sending a message in a broker

The service provides two resource management models:

  • When using a broker, you can send and receive messages in random topics that do not start with $.
  • When registries and devices are used, the selection of topics is limited.

For more information about the differences between brokers and device registries, see Relationships between resources in Yandex IoT Core.

When using registries and devices, you can send messages of the following types:

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

To receive messages, you need to subscribe to the sender. For information about how to do this, see Subscribing to 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 a message 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.

CLI
API

If you don't have the Yandex Cloud command line interface yet, install and initialize 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 to a device topic using certificate-based authorization:

    yc iot mqtt publish \
      --cert device-cert.pem \
      --key device-key.pem \
      --topic '$devices/<device_ID>/events' \
      --message 'Test data' \
      --qos 1
    
  • Send data to the permanent topic of a device using certificate-based authorization:

    yc iot mqtt publish \
      --cert device-cert.pem \
      --key device-key.pem \
      --topic '$devices/<device_ID>/state' \
      --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 to a device topic using username and password authorization:

    yc iot mqtt publish \
      --username <device ID> \
      --password <device password> \
      --topic '$devices/<device ID>/events' \
      --message 'Test data' \
      --qos 1
    
  • Send data to the permanent topic of a device using username and password authorization:

    yc iot mqtt publish \
      --username <device ID> \
      --password <device password> \
      --topic '$devices/<device ID>/state' \
      --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).

You can send data from a device to a device topic using the API publish method.

Send data from a device to a registry topic

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

CLI
API
  • Send data to a registry topic using certificate-based authorization:

    yc iot mqtt publish \
      --cert device-cert.pem \
      --key device-key.pem \
      --topic '$registries/<registry_ID>/events' \
      --message 'Test data' \
      --qos 1
    
  • Send data to the permanent topic of a registry using certificate-based authorization:

    yc iot mqtt publish \
      --cert device-cert.pem \
      --key device-key.pem \
      --topic '$registries/<registry_ID>/state' \
      --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 to a registry topic using username and password authorization:

    yc iot mqtt publish \
      --username <device ID> \
      --password <device password> \
      --topic '$registries/<registry ID>/events' \
      --message 'Test data' \
      --qos 1
    
  • Send data to the permanent topic of a registry using username and password authorization:

    yc iot mqtt publish \
      --username <device ID> \
      --password <device password> \
      --topic '$registries/<registry ID>/state' \
      --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).

You can send data from a device to a registry topic using the API publish method.

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

CLI
API
  • Send a command using certificate-based authorization:

    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
    
  • Send a command using a permanent topic and certificate-based authorization:

    yc iot mqtt publish \
      --cert registry-cert.pem \
      --key registry-key.pem \
      --topic '$devices/<device ID>/config' \
      --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 username and password authorization:

    yc iot mqtt publish \
      --username <registry ID> \
      --password <registry password> \
      --topic '$devices/<device ID>/commands' \
      --message 'Test command for first device' \
      --qos 1
    
  • Send a command using a permanent topic and username and password authorization:

    yc iot mqtt publish \
      --username <registry ID> \
      --password <registry password> \
      --topic '$devices/<device ID>/config' \
      --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).

You can send a command to a single device using the API publish method.

Send a command to all devices added to the registry

CLI
API
  • Send a command to all devices using certificate-based authorization:

    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
    
  • Send a command to all devices using a permanent topic and certificate-based authorization:

    yc iot mqtt publish \
      --cert registry-cert.pem \
      --key registry-key.pem \
      --topic '$registries/<registry ID>/config' \
      --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 to all devices using username and password authorization:

    yc iot mqtt publish \
      --username <registry ID> \
      --password <registry password> \
      --topic '$registries/<registry ID>/commands' \
      --message 'Test command for all devices' \
      --qos 1
    
  • Send a command to all devices using a permanent topic and username and password authorization:

    yc iot mqtt publish \
      --username <registry ID> \
      --password <registry password> \
      --topic '$registries/<registry ID>/config' \
      --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).

You can send a command to all devices added to a registry using the API publish method.

Sending a message in a broker

When using a broker, you can send a message to a random topic using your username and password or any certificate assigned to the broker for authorization.

CLI
API
  • Send a message using your username and password for authorization:

    yc iot mqtt publish \
      --username <broker ID> \
      --password <broker password> \
      --topic /my/custom/topic \
      --message 'Test broker message'
      --qos 1
    

    Where:

    • --username and --password: Parameters for authorization using a username and password.
    • --topic: A random topic name that conforms to the MQTT specification and does not start with $.
    • --message: Message text.
    • --qos: Quality of service (QoS).
  • Send a message using certificate-based authorization:

    yc iot mqtt publish \
      --cert broker-cert.pem \
      --key broker-key.pem \
      --topic /my/custom/topic \
      --message 'Test broker message'
      --qos 1
    

    Where:

    • --cert and --key: Parameters for authorization using a certificate.
    • --topic: A random topic name that conforms to the MQTT specification and does not start with $.
    • --message: Message text.
    • --qos: Quality of service (QoS).

You can send a message in a broker using the API publish method.

Was the article helpful?

Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
In this article:
  • Before you start
  • Authorization
  • Sending a message with data
  • Send data from a device to a device topic
  • Send data from a device to a registry topic
  • Sending messages with commands
  • Send a command to a single device
  • Send a command to all devices added to the registry
  • Sending a message in a broker