{"id":246,"date":"2017-05-09T00:00:48","date_gmt":"2017-05-08T16:00:48","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/iotdev\/?p=246"},"modified":"2019-02-15T10:27:15","modified_gmt":"2019-02-15T02:27:15","slug":"understand-different-connection-strings-in-azure-iot-hub","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/iotdev\/understand-different-connection-strings-in-azure-iot-hub\/","title":{"rendered":"Understand Different Connection Strings in Azure IoT Hub"},"content":{"rendered":"<p><span>This post first explains the different connection strings in Azure IoT Hub, then gives a simple IoT Hub solution <a href=\"#azurefunc\">Integrate Azure Functions with Azure IoT Hub<\/a> using all three connection strings.<\/span><\/p>\n<h2 id=\"conn\">Connection Strings<\/h2>\n<p><span>There&#8217;re three types of connection strings in Azure IoT Hub:<\/span><\/p>\n<ul>\n<li>IoT Hub connection string<\/li>\n<li>IoT Hub&#8217;s Event Hub-compatible connection string<\/li>\n<li>IoT Hub device connection string<\/li>\n<\/ul>\n<h3 id=\"iothubconn\">IoT Hub Connection String<\/h3>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>Usage<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Used mainly for device registration\/un-registration.<\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>Value format<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>HostName=<i>&lt;Host Name&gt;<\/i>;SharedAccessKeyName=<i>&lt;Key Name&gt;<\/i>;SharedAccessKey=<i>&lt;SAS Key&gt;<\/i><\/b><\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>How to get it<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"938\" height=\"423\" class=\"alignnone size-full wp-image-395\" alt=\"1-iothubconnecction\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/1-IoTHubConnecction.png\" \/><\/p>\n<h3 id=\"eventhubconn\">IoT Hub&#8217;s Event Hub-compatible connection string<\/h3>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>Usage<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>IoT Hub messages are saved in built-in endpoint <i>Events<\/i> by default, you may use this endpoint as event hub with the event hub-compatible connection string<\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>Value format<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>Endpoint=<i>&lt;ENDPOINT&gt;<\/i>;SharedAccessKeyName=<i>&lt;Key Name&gt;<\/i>;SharedAccessKey=<i>&lt;KEYVALUE&gt;<\/i><\/b>\nPleast note <b><i>&lt;ENDPOINT&gt;<\/i><\/b> starts with <b>sb:\/\/<\/b><\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>How to get it<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The value is similar as IoT Hub connection string except the first part is endpoint instead of host name.<\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Get Event Hub-compatible endpoint by clicking <b>Endpoints &#8211;&gt; Events<\/b>, we need use it in next step<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"938\" height=\"401\" class=\"alignnone size-full wp-image-405\" alt=\"2-eventhubendpoint\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/2-EventHubEndpoint.png\" \/><\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Use connection string got in <a href=\"#iothubconn\">IoT Hub Connection String<\/a>, replace <b>HostName=<i>&lt;Host Name&gt;<\/i><\/b> with <b>Endpoint=<i>&lt;ENDPOINT&gt;<\/i><\/b> got in step 1<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h3 id=\"iothubdeviceconn\">IoT Hub device connection string<\/h3>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>Usage<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Mainly used by devices to<\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>Send Device to Cloud messages<\/li>\n<li>Receive Cloud to Device messages<\/li>\n<li>Response direct method<\/li>\n<\/ul>\n<\/li>\n<li>Value format<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>HostName=<i>&lt;Host Name&gt;<\/i>;DeviceId=<i>&lt;Device Name&gt;<\/i>;SharedAccessKey=<i>&lt;Device Key&gt;<\/i><\/b><\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li>How to get it\n<ol>\n<li>If having no registered device, register one device by clicking <b>Device Explorer &#8211;&gt; Add<\/b><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"929\" height=\"420\" class=\"alignnone size-full wp-image-415\" alt=\"3-newdevice\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/3-NewDevice.png\" \/><\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Copy device connection string by clicking <b>Device Explorer &#8211;&gt; <i>Your device name<\/i><\/b><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"909\" height=\"424\" class=\"alignnone size-full wp-image-425\" alt=\"4-deviceconnectionstring\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/4-DeviceConnectionString.png\" \/><\/p>\n<h2 id=\"azurefunc\">Integrate Azure Functions with Azure IoT Hub<\/h2>\n<p>With understanding these connection strings, below provides a simple IoT Hub solution about how to integrate Azure Functions with Azure IoT Hub:<\/p>\n<h3 id=\"createiothub\">Create IoT Hub<\/h3>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li>You may follow\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-csharp-csharp-getstarted#create-an-iot-hub\">official IoT Hub document <\/a>to create IoT Hub if you don&#8217;t have one yet<\/li>\n<li>Navigate to your IoT Hub<\/li>\n<li id=\"geteventhubname\">Copy <b>Event Hub-compatible name<\/b> by clicking <b>Endpoints &#8211;&gt; Events<\/b>, we need use it when configuring Azure Function later<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"910\" height=\"395\" class=\"alignnone size-full wp-image-435\" alt=\"5-eventhubname\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/5-EventHubName.png\" \/><\/p>\n<ol>\n<li id=\"geteventhubconnstring\">Generate Event Hub-compatible connection string per <a href=\"#eventhubconn\">Event Hub-compatible connection string<\/a><\/li>\n<\/ol>\n<h3 id=\"createazurefunc\">Create Azure Functions<\/h3>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li>In Azure portal, create a Function App<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"371\" height=\"606\" class=\"alignnone size-full wp-image-445\" alt=\"6-newfuncapp\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/6-NewFuncApp.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Click &#8216;+&#8217; right after Functions, then click link <i>create your own custom function<\/i><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"836\" height=\"517\" class=\"alignnone size-full wp-image-455\" alt=\"7-createcustomfunc\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/7-CreateCustomFunc.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Select <b>EventHubTrigger-CSharp<\/b>, fill in <i>Event Hub-compatible name<\/i> value got in <a href=\"#geteventhubname\">Get Event Hub-compatible name<\/a>, then click <b><i>new<\/i><\/b> to add a connection string<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"767\" height=\"681\" class=\"alignnone size-full wp-image-465\" alt=\"8-newfunc\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/8-NewFunc.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Click <i>Add a connection string<\/i>, fill in connection name as you like and Event Hub connection string got in <a href=\"#geteventhubconnstring\">Generate Event Hub-compatible connection string<\/a>, then click <i>OK<\/i><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"613\" height=\"243\" class=\"alignnone size-full wp-image-466\" alt=\"9-newconnectionstring\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/9-NewConnectionString.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Click <i>Create<\/i> to confirm function creation<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" width=\"756\" height=\"771\" class=\"alignnone size-full wp-image-476\" alt=\"10-confirmfunccreation\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/10-ConfirmFuncCreation.png\" \/><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li>Edit the function to display event related info in log<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><code>\n#r \"Microsoft.ServiceBus\"<\/code><\/p>\n<p>using System;\nusing System.Net;\nusing System.Reflection;\nusing System.Text;\nusing Microsoft.ServiceBus.Common;\nusing Microsoft.ServiceBus.Messaging;<\/p>\n<p>public static void Run(EventData myEventHubMessage, TraceWriter log)\n{\nvar message = Encoding.UTF8.GetString(myEventHubMessage.GetBytes());\nlog.Info($&#8221;Processed a message: {message}; message enqueued to IoT Hub endpoint utc time:{myEventHubMessage.EnqueuedTimeUtc}&#8221;);\n}<\/p>\n<h3>Register Device and Send D2C Messages<\/h3>\n<p>You could simply use your computer as device for registration and sending D2C messages, you may follow sections <i>Create a device identity<\/i> and <i>Create a simulated device app<\/i> in official document <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/iot-hub\/iot-hub-csharp-csharp-getstarted\"> Connect your simulated device to your IoT hub using .NET<\/a>.\nOnce sending D2C messages successfully, you should see log message in Azure Functions Logs window:<\/p>\n<p><img decoding=\"async\" width=\"1041\" height=\"538\" class=\"alignnone size-full wp-image-486\" alt=\"11-funclog\" src=\"https:\/\/devblogs.microsoft.com\/iotdev\/wp-content\/uploads\/sites\/24\/2017\/05\/11-FuncLog.png\" \/><\/p>\n<p>If you want to learn more about Azure IoT development, please refer to <a href=\"https:\/\/blogs.msdn.microsoft.com\/iotdev\/2017\/04\/10\/a-happy-path-to-learn-iot-development-on-azure\/\">A \u201cHappy Path\u201d to learn IoT development on Azure<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;re three types of connection strings in Azure IoT Hub: IoT Hub connection string IoT Hub&#8217;s Event Hub-compatible connection string IoT Hub [&hellip;]<\/p>\n","protected":false},"author":529,"featured_media":1836,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[5,7,16,17,21],"class_list":["post-246","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iot-dev","tag-azure","tag-azure-iot-hub","tag-internet-of-things","tag-iot","tag-iot-hub-connection-string"],"acf":[],"blog_post_summary":"<p>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&#8217;re three types of connection strings in Azure IoT Hub: IoT Hub connection string IoT Hub&#8217;s Event Hub-compatible connection string IoT Hub [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/posts\/246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/users\/529"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/comments?post=246"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/media\/1836"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/iotdev\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}