{"id":101,"date":"2016-02-26T05:59:42","date_gmt":"2016-02-26T05:59:42","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/holgerkenn\/2016\/02\/26\/azureiothubproxy\/"},"modified":"2016-02-26T05:59:42","modified_gmt":"2016-02-26T05:59:42","slug":"azureiothubproxy","status":"publish","type":"post","link":"https:\/\/www.cubeos.org\/blog\/2016\/02\/azureiothubproxy\/","title":{"rendered":"AzureIotHubProxy"},"content":{"rendered":"<p>Today, I uploaded a project to github that I wrote in the last weeks in order to simplify things with the Azure IoT hub for demos, makers etc.<\/p>\n<p>If you haven&#8217;t heard\u00a0about Azure IoT hub, this is a very nice service you can use to hook up your IoT devices to a central service that you can use to receive data, send commands and, in general, manage your devices.<\/p>\n<p><a href=\"https:\/\/azure.microsoft.com\/en-us\/documentation\/services\/iot-hub\/\">https:\/\/azure.microsoft.com\/en-us\/documentation\/services\/iot-hub\/<\/a> is the official starting point for the documentation, but basically, the Azure IoT hub has a device and a service API. Through the device api, you can basically send messages to the cloud and receive messages from the cloud. The cool thing about this is that the device side only does outbound connections (e.g. this works through firewalls, through NAT devices such as DSL routers\u00a0and even through IP connections provided by mobile phone providers. Read this again: Back channel <span style=\"text-decoration: underline\">to<\/span> your device works through <strong>mobile phone network<\/strong>!<\/p>\n<p>And the best thing: This service incudes a free tier that allows you to register 500 devices and send 8.000 Messages of\u00a00.5k per day. See here <a href=\"https:\/\/azure.microsoft.com\/en-us\/pricing\/details\/iot-hub\/\">https:\/\/azure.microsoft.com\/en-us\/pricing\/details\/iot-hub\/<\/a> for details.<\/p>\n<p>But in order to get to all this goodness, you need to manage the IOT hub via its service API. You can do that through the Device Explorer tool (see <a href=\"https:\/\/github.com\/Azure\/azure-iot-sdks\/tree\/master\/tools\/DeviceExplorer\">https:\/\/github.com\/Azure\/azure-iot-sdks\/tree\/master\/tools\/DeviceExplorer<\/a> ) but that&#8217;s a manual process that involves creating devices on the hub and then copying the device connection strings manually into the device configuration. Or you can deal with the standard management API which is a bit tricky to use and actually would require you to keep the management keys where ever you would like to manage it.<\/p>\n<p>Wouldn&#8217;t it be nice if the devices could actually manage themselves?<\/p>\n<p>So I wrote a little API Proxy service that the device can query to get a connection string. The service just implements four calls.<\/p>\n<p>GET \/api\/Device get just returns the list of devices configured in a JSON form<\/p>\n<p>GET \/api\/Device\/(id) returns the\u00a0JSON just for this device<\/p>\n<p>POST \/api\/Device\/(id) creates a new device in the IOT hub and returns a JSON that includes a connection string<\/p>\n<p>DELETE \/api\/Device\/(id) deletes the device in the IOT hub.<\/p>\n<p>In order to secure these, they all require an API key send in the query string.<\/p>\n<p>The implementation I made is really simple and not very secure. But it can be used as a starting point to think about more complex authentication schemes, e.g. one could implement a one-time token mechanism that would only allow a single device registration for each token.<\/p>\n<p>To try out the implementation, I added a swagger interface, so if you go to \/swagger\/ you can play around with the API yourself. You should disable that for production use.<\/p>\n<p>The service can easily be run in an Azure Web App. And again, there is a free tier that is sufficient to run this service. See here <a href=\"https:\/\/azure.microsoft.com\/en-us\/pricing\/details\/app-service\/\">https:\/\/azure.microsoft.com\/en-us\/pricing\/details\/app-service\/<\/a> Azure app services also support SSL that you should use in order to protect your API key.\u00a0(SSL is\u00a0not supported\u00a0for custom domains, in the free tier\u00a0so your website will all end on &#8220;azurewebsites.net&#8221;)<\/p>\n<p>To get started, clone the project from github <a href=\"https:\/\/github.com\/holgerkenn\/AzureIotHubProxy\">https:\/\/github.com\/holgerkenn\/AzureIotHubProxy<\/a> and then go to <a href=\"https:\/\/azure.microsoft.com\/free\/\">https:\/\/azure.microsoft.com\/free\/<\/a> to start a free trial on azure in case you don&#8217;t have a subscription yet. Through this link, you will also get some free credit to use the paid azure services for a limited time, but since everything presented here also works on the free tiers of the services, you can actually run all this even after the free trial credits expire.<\/p>\n<p>Go to <a href=\"https:\/\/azure.microsoft.com\/en-us\/develop\/iot\/\">https:\/\/azure.microsoft.com\/en-us\/develop\/iot\/<\/a> to see how to create your first IoT hub, then get its connection string from the Azure Portal and add it to the web.config file in the repository. Then create a web app on Azure as explained here <a href=\"https:\/\/azure.microsoft.com\/en-us\/documentation\/services\/app-service\/web\/\">https:\/\/azure.microsoft.com\/en-us\/documentation\/services\/app-service\/web\/<\/a> and publish the service to this app. In Visual Studio, this is as simple as right-clicking the project, selecting publish and then &#8220;Microsoft Azure App Service&#8221;. This will then guide you to select\u00a0or create a new\u00a0Azure web app\u00a0for\u00a0your service. After the publish, your service should be up and running. And since the swagger api is enabled, you will find the trial api on &#8220;<a href=\"https:\/\/&lt;yourservicename&gt;.azurewebsites.net\">https:\/\/&lt;yourservicename&gt;.azurewebsites.net<\/a>&#8221;<\/p>\n<p>Then you can go and compile the test client. Enter the name of your web app in program.cs. When you run it, it will connect to the service, create a device named &#8220;1234567&#8221; and send a few messages to the IoT hub. If you have device explorer connected, you can receive those messages and send a few back.<\/p>\n<p><strong>And now you should probably change\u00a0that default API key (&#8220;1234&#8221;) and republish.<\/strong><\/p>\n<p>Hope this helps,<\/p>\n<p>H.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I uploaded a project to github that I wrote in the last weeks in order to simplify things with the Azure IoT hub for demos, makers etc. If you haven&#8217;t heard\u00a0about Azure IoT hub, this is a very nice &hellip; <a href=\"https:\/\/www.cubeos.org\/blog\/2016\/02\/azureiothubproxy\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[31,32],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure","tag-internet-of-things"],"_links":{"self":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":0,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}