Azure IoT Hub (HTTP)

In this example, we configure an HTTP connection that publishes live messages as direct method invocations to Azure IoT Hub.

Prerequisites

To follow this example you will need:

  • A Bosch IoT Things service subscription
  • An Azure IoT Hub instance
  • An Azure IoT Hub device

Create the connection

  • Click Go to Dashboard in the row of your Things instance, which will manage the connection.
  • On the Things dashboard open the tab Connections/Integrations.
  • Click Create your first connection, or Add in case you have one already.
    • Select HTTP from the categories.
    • Give the connection a name. Setting a name is required.
    • Click the Continue button.
create-http-connection

Configure credentials

  • Scroll to the Coordinates section.
  • Choose hmac as the credential type.
  • Choose az-sasl as the algorithm.
  • Click the button Paste Azure IoT Hub connection string.
    Then paste the connection string of a shared access key with the service connect permission.
  • Enter the hostname of your Azure IoT Hub instance <iot-hub-name>.azure-devices.net.

az-iot-hub-coordinates

Add a target

  • Scroll to the Targets section.
  • Click the Add target button.
  • Choose POST as the HTTP verb.
  • Enter /twins/{{ thing:id }}/methods?api-version=2018-06-30 as the HTTP path
  • Add an authorization subject, e.g. integration:<solution-id>:aih
  • Check Thing live messages under Topics to publish on address.
  • Click Save new target.

az-iot-hub-target

Configure payload mapping

  • Scroll to the Payload mapping section.
  • Check Enable custom JavaScript-based payload mapping
  • Change the outgoing payload mapping to compute the payload for Azure IoT Hub direct method invocation from the subject, payload and timeout of a live message
function mapFromDittoProtocolMsg(namespace, name, group, channel, criterion,
                                 action, path, dittoHeaders, value, status, extra) {

  let headers = dittoHeaders;
  let payload = {
      "methodName": action,
      "responseTimeoutInSeconds": parseInt(dittoHeaders.timeout),
      "payload": value
  };
  let textPayload = JSON.stringify(payload);
  let bytePayload = null;
  let contentType = 'application/json';

  return Ditto.buildExternalMsg(headers, textPayload, bytePayload, contentType);
}
  • Click the Create button at the top right to save the connection.

The connection is ready.

Any live message the connection is authorized to receive is published to Azure IoT Hub as direct method invocation.
Note that the thing ID of the live message should be equal to the device ID in Azure IoT Hub.

Authorize the connection for messages

Follow the steps “Create a Hello World thing” and “Add the authorization subject of the connection to the policy” on the Hub-to-Things connection tutorial to authorize the connection to receive live messages.

Remember to use the authorization subject of this connection in the policy, e.g. integration:<solution-id>:aih.

Corporate information Data protection notice Legal information Support Free plans