{"id":2791,"date":"2015-06-08T20:40:06","date_gmt":"2015-06-08T18:40:06","guid":{"rendered":"https:\/\/blog.redbaronofazure.com\/?p=2791"},"modified":"2015-10-29T12:39:19","modified_gmt":"2015-10-29T11:39:19","slug":"playing-with-raspberry-pi-and-the-azure-event-hub","status":"publish","type":"post","link":"https:\/\/blog.redbaronofazure.com\/?p=2791","title":{"rendered":"Playing with Raspberry Pi and the Azure Event Hub"},"content":{"rendered":"<p>Azure Service Bus started with Relay services and then grew with Queues and Topics. The latest feature is the Event Hub, which is designed to handle million of devices sending and enormous amout of messages concurrently. This is known as the Internet of Things\u00a0when little or big devices out there are connected and ingest data for telemetry purposes. The idea is naturally that you at the end increase value of you products somehow based on the insights you get from the data.<\/p>\n<p>One &#8220;thing&#8221; that recently has caught much attention is Raspberry Pi. It&#8217;s <img loading=\"lazy\" class=\"alignright size-medium wp-image-2811\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/InstagramCapture_8d1f9e9d-764a-41a3-bf79-9d2a6ea04560-300x300.jpg\" alt=\"InstagramCapture_8d1f9e9d-764a-41a3-bf79-9d2a6ea04560\" width=\"300\" height=\"300\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/InstagramCapture_8d1f9e9d-764a-41a3-bf79-9d2a6ea04560-300x300.jpg 300w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/InstagramCapture_8d1f9e9d-764a-41a3-bf79-9d2a6ea04560-150x150.jpg 150w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/InstagramCapture_8d1f9e9d-764a-41a3-bf79-9d2a6ea04560.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/>a ~35 USD computing device in the size of a credit card that you can attach USB-devices to, screens via HDMI and an ethernet cable. On a MicroSD card you can have the OS to boot the tiny machine. It is simply really cool and brings back memories of how companies like Microsoft and Apple got started and the stories about Altair 8800 and Homebrew Computer Club. What could be a more perfect combination than sending messages to Azure Event Hub from\u00a0Raspberry Pi?<\/p>\n<p>One common OS that runs on Raspberry Pi i the Linux distro Raspbian. You download\u00a0Raspbian and copy it to your MicroSD card and boot the board. Included on the Linux distro is the java compiler, so writing an Azure Event Hub client is perfectly possible with that. In the AzureSDK for java, there is support for Service Bus if you are using Queues or Topics, but there is no support yet for the Event Hub. So, writing a client program would require using the raw\u00a0REST API\u00a0for the Event Hub.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/AzureEventHubsOverview.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-3062\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/AzureEventHubsOverview.png\" alt=\"AzureEventHubsOverview\" width=\"624\" height=\"259\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/AzureEventHubsOverview.png 624w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/AzureEventHubsOverview-300x125.png 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>In the Azure documentation there is sample code for the Event Hub with one eventhub processor (receiver) and two types of clients, where one is using the REST apis in C#. My plan was to convert that sample into java.<\/p>\n<p><strong>Authentication using SAS tokens<\/strong><\/p>\n<p>Service Bus authentication is nowadays about\u00a0Shared Access Signatures which is a\u00a0token you attach in the\u00a0http header in the REST call.\u00a0You create the SAS Key in the management portal, give it a name and specify what permissions\u00a0it has on the event hub namespace. For an eventhub client, the send permission\u00a0is\u00a0all that is\u00a0needed.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/SAS-policy.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-2841\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/SAS-policy.png\" alt=\"SAS-policy\" width=\"673\" height=\"576\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/SAS-policy.png 673w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/SAS-policy-300x257.png 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>In code, generating the SAS token is\u00a0about Hash-based Message Authentication Code and Secure Hash Algorithm, where the url to the eventhub is encrypted with HMACSHA256 using the SAS key.\u00a0Finding code samples to do this in C# on the internet isn&#8217;t\u00a0hard. Translating it to java was a more difficult task to complete and after multiple attempts following suggestions on different java programming forums, I only got\u00a0HTTP status code of 401 &#8211; Access Denied &#8211; from Azure Event Hub. My SAS token was malformed.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-SAS-token.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-2871\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-SAS-token.png\" alt=\"dotnet-SAS-token\" width=\"683\" height=\"213\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-SAS-token.png 683w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-SAS-token-300x94.png 300w\" sizes=\"(max-width: 683px) 100vw, 683px\" \/><\/a><\/p>\n<p>Since SAS tokens are needed for Service Bus Queues and Topics, the guys as MS OpenTech had already solved the problem and the answer was somewhere inside AzureSDK. So\u00a0with\u00a0a little use of a java decompiler, my problem with the 401&#8217;s was solved. The missing piece was the use of the <em>DatatypeConverter.printBase64Binary<\/em>.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-SAS-token.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-2891\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-SAS-token.png\" alt=\"java-SAS-token\" width=\"700\" height=\"303\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-SAS-token.png 700w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-SAS-token-300x130.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>The token created then need to be set in the HTTP headers of the request with the name of <em>Authorization<\/em>. Content-type of the request must be set to <em>application\/atom+xml;type=entry;charset=utf-8<\/em>.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-headers.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-2901\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-headers.png\" alt=\"java-headers\" width=\"637\" height=\"258\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-headers.png 637w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-headers-300x122.png 300w\" sizes=\"(max-width: 637px) 100vw, 637px\" \/><\/a><\/p>\n<p><strong>Sending messages\u00a0to Azure Event Hub from the Raspberry Pi device<\/strong><\/p>\n<p>Besides following the installation instructions of NOOBS on Raspberry Pi, including getting it&#8217;s wlan to work, was really just the below, ie two simple steps<\/p>\n<ul>\n<li>Downloading the Azure Java SDK using WGET and unzipping it to it&#8217;s own directory<\/li>\n<li>Downloading my\u00a0java source file and the accompanying config file\u00a0(both which are available at the bottom of this page)<\/li>\n<\/ul>\n<p>With that in place, I could compile and run the little java program and send a message to the event hub.\u00a0It\u00a0looked like this<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-running.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-2911\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-running.png\" alt=\"java-running\" width=\"859\" height=\"283\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-running.png 859w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/java-running-300x99.png 300w\" sizes=\"(max-width: 859px) 100vw, 859px\" \/><\/a><\/p>\n<p>On my laptop and in Visual Studio, I ran the\u00a0Azure sample program doing the event processing. That program picks up the message sent from the little Raspberry Pi device from the event hub. As you can see I sent a few more messages.<\/p>\n<p><a href=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-running.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-2931\" src=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-running.png\" alt=\"dotnet-running\" width=\"671\" height=\"316\" srcset=\"https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-running.png 671w, https:\/\/blog.redbaronofazure.com\/wp-content\/uploads\/2015\/06\/dotnet-running-300x141.png 300w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>Using Raspberry Pi as an example, I showed you that you can write programs that only use the public REST APIs\u00a0and integrate with Azure Event Hub. The program is written with &#8220;basic&#8221; java, that is, I used nothing from Microsoft and no jar-files from the AzureSDK. I can now add sensors or cameras to my Raspberry Pi device and start feeding in some real data. The architecture would scale up to million of devices ingesting whatever telemetry data\u00a0they capture. Microsoft has a hands-on lab website where you can get ideas of all sorts of IoT projects you can build in the Pi.<\/p>\n<p><strong>Source Code<\/strong><\/p>\n<p><a href=\"http:\/\/data.redbaronofazure.com\/public\/AzSbEvtHub.zip\" target=\"_blank\">http:\/\/data.redbaronofazure.com\/public\/AzSbEvtHub.zip<\/a><\/p>\n<p><strong>References<\/strong><\/p>\n<p>Get Started with Event Hub &#8211; Microsoft Documentation and sample code<br \/>\n<a href=\"https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/service-bus-event-hubs-csharp-ephcs-getstarted\/\" target=\"_blank\">https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/service-bus-event-hubs-csharp-ephcs-getstarted\/<\/a><\/p>\n<p>MSDN &#8211; Event Hubs Overview<br \/>\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/azure\/dn836025.aspx\" target=\"_blank\">https:\/\/msdn.microsoft.com\/en-us\/library\/azure\/dn836025.aspx<\/a><\/p>\n<p>TechEd 2014 &#8211; Clemens Vasters session on Hyper-Scale with Event Hub (a must see)<br \/>\n<a href=\"http:\/\/channel9.msdn.com\/Events\/TechEd\/Europe\/2014\/CDP-B307\" target=\"_blank\">http:\/\/channel9.msdn.com\/Events\/TechEd\/Europe\/2014\/CDP-B307<\/a><\/p>\n<p>Weather station project on Raspberry Pi<br \/>\n<a href=\"https:\/\/microsoft.hackster.io\/windowsiot\/build-hands-on-lab-iot-weather-station-using-windows-10\" target=\"_blank\">https:\/\/microsoft.hackster.io\/windowsiot\/build-hands-on-lab-iot-weather-station-using-windows-10<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure Service Bus started with Relay services and then grew with Queues and Topics. The latest feature is the Event Hub, which is designed to handle million of devices sending and enormous amout of messages concurrently. This is known as the Internet of Things\u00a0when little or big devices out there are connected and ingest data [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[151,101],"tags":[281],"_links":{"self":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/2791"}],"collection":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2791"}],"version-history":[{"count":0,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=\/wp\/v2\/posts\/2791\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.redbaronofazure.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}