Receive Commands via MQTT

This page shows you how to receive commands via a MQTT client from the Bosch IoT Hub. As exemplary device the Eclipse Mosquitto MQTT client is used.

Preconditions

  • Bosch IoT Hub service was subscribed.
  • A test device has been registered in Bosch IoT Hub.
  • Credentials for the given device have been created in Bosch IoT Hub.
  • The mosquitto_sub command is installed. See Eclipse Mosquitto
  • The mosquitto_pub command is installed. See Eclipse Mosquitto

Create command subscriber

  1. Download the server certificate for MQTT TLS encryption:

    curl -o iothub.crt https://docs.bosch-iot-hub.com/cert/iothub.crt
    
  2. Start and subscribe to MQTT command topic with Mosquitto MQTT client:

    mosquitto_sub -d -h mqtt.bosch-iot-hub.com -p 8883 -u {auth-id}@{tenant-id} -P {secret} --cafile iothub.crt -k 30 -t control/+/+/req/#
    

    Make sure you replace all parameters correctly:

    • {tenant-id} the ID of your tenant
    • {auth-id} the ID of the credential
    • {secret} the secret that was registered with the credential (plaintext)
      Note: Please use the plaintext password you have created during the credential registration. Do not use hashed or base64 encoded when sending messages. The data transfer is secured by TLS encryption.
    • The -k parameter is to overwrite the default keep alive period. Here we set the keep alive period to 30 seconds. If no data flow over an open connection for longer than 30 seconds then the client will generate a PINGREQ to indicate the server that the device is still active.
    • The -t parameter is the name of the mqtt topic to subscribe to. In this example the topic control/+/+/req/# is defined using mqtt topic wildcard format, to subscribe to all possible commands.
  3. The mqtt client is now subscribed to the Bosch IoT Hub and is ready to receive commands

Receive one-way command

  1. Perform the above described steps to register to the command topic.

  2. Follow the instructions on the Connect command sending IoT Application page to send a one-way command to the device.

  3. You should see the command message printed out on the command line:

    Client mosqsub|XXXXXX-XXXXXXS received PUBLISH (d0, q0, r0, m0, 'control///req//sayGreeting', ... (33 bytes))
    {"greeting" : "Hi, How are you?"}
    

    The topic the command was published to contains in this example the following information:

    • sayGreeting the name of the command to execute on the device.

    The body of the message contains the payload of the command.

Receive Request/Response Command

  1. Perform the above described steps to register to the command topic.

  2. Follow the instructions on the Connect command sending IoT Application page to send a request/response command to the device.

  3. You should see the command message printed out on the command line:

    Client mosqsub|XXXXXX-XXXXXXS received PUBLISH (d0, q0, r0, m0, 'control///req/1010be56669a-3fe9-41e5-af46-42f1ce6f911d/sayGreeting', ... (33 bytes))
    {"greeting" : "Hi, How are you?"}
    

    The topic the command was published to contains in this example the following information:

    • 1010be56669a-3fe9-41e5-af46-42f1ce6f911d the unique ID of this command. It will be used to send back the answer of the command.
    • sayGreeting the name of the command to execute on the device.

    The body of the message contains the payload of the command.

  4. Send back the response of the command using the mosquitto_pub command:

    mosquitto_pub -d -h mqtt.bosch-iot-hub.com -p 8883 -u {auth-id}@{tenant-id} -P {secret} --cafile iothub.crt -t control///res/1010be56669a-3fe9-41e5-af46-42f1ce6f911d/200 -m '{"greetingDurationInSec": 23}'
    

    The answer topic contains in this example the following information:

    • 1010be56669a-3fe9-41e5-af46-42f1ce6f911d the unique ID of the command for which this answer is for.
    • 200 the return status code of the command.

    The body of the answer can contain arbitrary data and is not required.