Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Solutions
  • Pricing
  • Documentation
  • Contact us
Get started
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 certificates
      • Create a certificate
      • Managing registry certificates
      • Managing device certificates
    • Managing passwords
      • Managing registry passwords
      • Managing device passwords
  • Concepts
    • Authorization
    • Relationship between service resources
    • MQTT broker connection parameters
    • Sending messages to an MQTT broker via gRPC
    • Topic
    • Backups
    • Quotas and limits
  • Use cases
    • All use cases
    • 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
  • API reference
    • Authentication in the API
    • gRPC
      • Overview
      • DeviceDataService
      • DeviceService
      • RegistryDataService
      • RegistryService
      • OperationService
    • REST
      • Overview
      • Device
        • Overview
        • addCertificate
        • addPassword
        • create
        • delete
        • deleteCertificate
        • deletePassword
        • get
        • list
        • listCertificates
        • listOperations
        • listPasswords
        • update
      • DeviceData
        • Overview
        • publish
      • Registry
        • Overview
        • addCertificate
        • addPassword
        • create
        • delete
        • deleteCertificate
        • deletePassword
        • get
        • list
        • listCertificates
        • listDeviceTopicAliases
        • listOperations
        • listPasswords
        • update
      • RegistryData
        • Overview
        • publish
  • Questions and answers
  1. Use cases
  2. Working with Mosquitto
  3. Sending messages

Sending a message using Mosquitto

  • Before you start
  • Connecting to an MQTT broker
  • Sending a message with data
  • Sending messages with commands

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 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 a device or registry to receive messages using Mosquitto.

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

To get started, you will need:

  1. A registry.
  2. A registry certificate.
  3. A device.
  4. A device certificate.
  5. Mosquitto, an open source MQTT message broker. It's used in instructions for sending messages and subscribing to devices. Download and install it to work with commands given in instructions.

Connecting to an MQTT broker

Connect to the MQTT broker using the following parameters:

  • Certification authority certificate.
  • Broker address: mqtt.cloud.yandex.net
  • Broker port: 8883
  • Protocol: TLSv1.2

Sending a message with data

Send a message with data using the following parameters:

  • -h: MQTT broker address.
  • -p: MQTT broker port.
  • --cafile: Path to the certificate from the certificate authority (CA).
  • --cert: Path to the public part of the device certificate.
  • --key: Path to the private part of the device certificate.
  • -t: Device topic.
  • -m: Message text.
  • -q: Quality of service (QoS).

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.

Mosquitto
  • Send data to the device topic:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile rootCA.crt \
    --cert device-cert.pem \
    --key device-key.pem \
    -t '$devices/<device ID>/events' \
    -m 'Test data' \
    -q 1
    
  • Send data to the permanent device topic:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile rootCA.crt \
    --cert device-cert.pem \
    --key device-key.pem \
    -t '$devices/<device ID>/state' \
    -m 'Test data' \
    -q 1
    

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

  • Send data to the registry topic:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile rootCA.crt \
    --cert device-cert.pem \
    --key device-key.pem \
    -t '$registries/<registry ID>/events' \
    -m 'Test data' \
    -q 1
    
  • Send data to the permanent registry topic:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile rootCA.crt \
    --cert device-cert.pem \
    --key device-key.pem \
    -t '$registries/<registry ID>/state' \
    -m 'Test data' \
    -q 1
    

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

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.

Send a message with a command using the following parameters:

  • -h: MQTT broker address.
  • -p: MQTT broker port.
  • --cafile: Path to the certificate from the certificate authority (CA).
  • --cert: Path to the public part of the registry certificate.
  • --key: Path to the private part of the registry certificate.
  • -t: Device topic.
  • -m: Message text.
  • -q: Quality of service (QoS).

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.

Mosquitto
  • Send a command to a single device:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile rootCA.crt \
    --cert registry-cert.pem \
    --key registry-key.pem \
    -t '$devices/<device ID>/commands' \
    -m 'Test command for first device' \
    -q 1
    
  • Send a command to a single device using the permanent topic:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile rootCA.crt \
    --cert registry-cert.pem \
    --key registry-key.pem \
    -t '$devices/<device ID>/config' \
    -m 'Test command for first device via permanent topic' \
    -q 1
    
  • Send a command to two devices:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile cert.pem \
    --cert registry-cert.pem \
    --key registry-key.pem \
    -t '$devices/<ID of the first device>/commands' \
    -t '$devices/<ID of the second device>/commands' \
    -m 'Test command for first and second device' \
    -q 1 # QoS 1.
    
  • Send a command to two devices using the permanent topic:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile cert.pem \
    --cert registry-cert.pem \
    --key registry-key.pem \
    -t '$devices/<ID of the first device>/config' \
    -t '$devices/<ID of the second device>/config' \
    -m 'Test command for first and second devices via permanent topic' \
    -q 1 # QoS 1.
    
  • Send a command to all devices added to the registry:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile cert.pem \
    --cert registry-cert.pem \
    --key registry-key.pem \
    -t '$registries/<registry ID>/commands' \
    -m 'Test command for all devices' \
    -q 1
    
  • Send a command to all devices added to the registry using the permanent topic:

    $ mosquitto_pub -h mqtt.cloud.yandex.net \
    -p 8883 \
    --cafile cert.pem \
    --cert registry-cert.pem \
    --key registry-key.pem \
    -t '$registries/<registry ID>/config' \
    -m 'Test command for all devices via permanent topic' \
    -q 1
    
In this article:
  • Before you start
  • Connecting to an MQTT broker
  • Sending a message with data
  • Sending messages with commands
Language / Region
Careers
Privacy policy
Terms of use
Brandbook
© 2021 Yandex.Cloud LLC