May 9th, 2017

Understand Different Connection Strings in Azure IoT Hub

Erich(Renyong) Wang
Senior Software Engineer

This post first explains the different connection strings in Azure IoT Hub, then gives a simple IoT Hub solution Integrate Azure Functions with Azure IoT Hub using all three connection strings.

Connection Strings

There’re three types of connection strings in Azure IoT Hub:

  • IoT Hub connection string
  • IoT Hub’s Event Hub-compatible connection string
  • IoT Hub device connection string

IoT Hub Connection String

    • Usage

Used mainly for device registration/un-registration.

    • Value format

HostName=<Host Name>;SharedAccessKeyName=<Key Name>;SharedAccessKey=<SAS Key>

    • How to get it

1-iothubconnecction

IoT Hub’s Event Hub-compatible connection string

    • Usage

IoT Hub messages are saved in built-in endpoint Events by default, you may use this endpoint as event hub with the event hub-compatible connection string

    • Value format

Endpoint=<ENDPOINT>;SharedAccessKeyName=<Key Name>;SharedAccessKey=<KEYVALUE> Pleast note <ENDPOINT> starts with sb://

    • How to get it

The value is similar as IoT Hub connection string except the first part is endpoint instead of host name.

      1. Get Event Hub-compatible endpoint by clicking Endpoints –> Events, we need use it in next step

2-eventhubendpoint

    1. Use connection string got in IoT Hub Connection String, replace HostName=<Host Name> with Endpoint=<ENDPOINT> got in step 1

IoT Hub device connection string

    • Usage

Mainly used by devices to

      • Send Device to Cloud messages
      • Receive Cloud to Device messages
      • Response direct method
    • Value format

HostName=<Host Name>;DeviceId=<Device Name>;SharedAccessKey=<Device Key>

    • How to get it
      1. If having no registered device, register one device by clicking Device Explorer –> Add

3-newdevice

      1. Copy device connection string by clicking Device Explorer –> Your device name

4-deviceconnectionstring

Integrate Azure Functions with Azure IoT Hub

With understanding these connection strings, below provides a simple IoT Hub solution about how to integrate Azure Functions with Azure IoT Hub:

Create IoT Hub

    1. You may follow official IoT Hub document to create IoT Hub if you don’t have one yet
    2. Navigate to your IoT Hub
    3. Copy Event Hub-compatible name by clicking Endpoints –> Events, we need use it when configuring Azure Function later

5-eventhubname

  1. Generate Event Hub-compatible connection string per Event Hub-compatible connection string

Create Azure Functions

    1. In Azure portal, create a Function App

6-newfuncapp

    1. Click ‘+’ right after Functions, then click link create your own custom function

7-createcustomfunc

    1. Select EventHubTrigger-CSharp, fill in Event Hub-compatible name value got in Get Event Hub-compatible name, then click new to add a connection string

8-newfunc

    1. Click Add a connection string, fill in connection name as you like and Event Hub connection string got in Generate Event Hub-compatible connection string, then click OK

9-newconnectionstring

    1. Click Create to confirm function creation

10-confirmfunccreation

    1. Edit the function to display event related info in log

#r "Microsoft.ServiceBus"

using System; using System.Net; using System.Reflection; using System.Text; using Microsoft.ServiceBus.Common; using Microsoft.ServiceBus.Messaging;

public static void Run(EventData myEventHubMessage, TraceWriter log) { var message = Encoding.UTF8.GetString(myEventHubMessage.GetBytes()); log.Info($”Processed a message: {message}; message enqueued to IoT Hub endpoint utc time:{myEventHubMessage.EnqueuedTimeUtc}”); }

Register Device and Send D2C Messages

You could simply use your computer as device for registration and sending D2C messages, you may follow sections Create a device identity and Create a simulated device app in official document Connect your simulated device to your IoT hub using .NET. Once sending D2C messages successfully, you should see log message in Azure Functions Logs window:

11-funclog

If you want to learn more about Azure IoT development, please refer to A “Happy Path” to learn IoT development on Azure.

Author

Erich(Renyong) Wang
Senior Software Engineer

Erich Wang is a Senior Software Engineer at Microsoft, he focuses on IoT related tooling.

0 comments

Discussion are closed.