Send Device Data via MQTT

This page shows you how to send data via a mqtt client to 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.
  • A consumer application is connected to the Bosch IoT Hub.

Send telemetry messages

  1. Make sure that you have a connected consumer.

  2. Download the server certificate for MQTT TLS encryption:

    curl -o iothub.crt https://docs.bosch-iot-hub.com/cert/iothub.crt
    
  3. Send a MQTT message with Mosquitto MQTT broker:

    mosquitto_pub -d -h mqtt.bosch-iot-hub.com -p 8883 -u {auth-id}@{tenant-id} -P {secret} --cafile iothub.crt -t telemetry -m '{"temperature":5}'
    

    Make sure you set 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.
  4. You should then get a response like this:

    Client 477111 sending CONNECT
    Client 477111 received CONNACK
    Client 477111 sending PUBLISH (d0, q0, r0, m1, 'telemetry', ... (17 bytes))
    Client 477111 sending DISCONNECT
    

Send event messages

To send event messages the same example command can be used. The same device credentials and protocol endpoint can be used. The only difference is the different topic (event compared to telemetry):

mosquitto_pub -d -h mqtt.bosch-iot-hub.com -p 8883 -u {auth-id}@{tenant-id} -P {secret} --cafile iothub.crt -t event -m '{"temperature":5}' -q 1