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
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.
-
-
- Get Event Hub-compatible endpoint by clicking Endpoints –> Events, we need use it in next step
-
-
- 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
- If having no registered device, register one device by clicking Device Explorer –> Add
- How to get it
-
-
- Copy device connection string by clicking Device Explorer –> Your device name
-
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
-
- You may follow official IoT Hub document to create IoT Hub if you don’t have one yet
- Navigate to your IoT Hub
- Copy Event Hub-compatible name by clicking Endpoints –> Events, we need use it when configuring Azure Function later
- Generate Event Hub-compatible connection string per Event Hub-compatible connection string
Create Azure Functions
-
- In Azure portal, create a Function App
-
- Click ‘+’ right after Functions, then click link create your own custom function
-
- 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
-
- 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
-
- Click Create to confirm function creation
-
- 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:
If you want to learn more about Azure IoT development, please refer to A “Happy Path” to learn IoT development on Azure.
0 comments