Using Azure Event Hubs on the Raspberry Pi 2

eventhubIn a lot of IoT scenarios Azure Event Hubs play a central role: the role of the highly scalable publish-subscribe event ingestor.  Event Hubs can collect millions of events per second, so that you can process and analyze the massive amounts of data produced by your connected devices and applications.

Getting started with these Event Hubs as a .NET developer is pretty easy, there is a NuGet Package that contains the SDK which will allow you to send an event in 3 lines of code. Unfortunately if you plan to run your application on a Raspberry Pi 2, you can’t use that SDK (or NuGet Package) directly. In the case your Raspberry Pi is running on Windows 10 IoT Core, and you are building a Universal Windows application you’ll already get an error when you try to add the Azure Service Bus Nuget Package: WindowsAzure.ServiceBus 3.0.7 is not compatible with UAP,Version=v10.0.

EventHubsPi1

In the case your Raspberry Pi is running Linux (e.g. Raspbian) and the Mono Framework (check this post to get started), you can add the Nuget Package to your Console Application, but when you try to run it on Linux, you’ll get the following exception:

Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for TimerManager —> System.EntryPointNotFoundException: CreateWaitableTimer

EventHubs2

So how can we use Azure Event Hubs on our Raspberry Pi 2? Well the trick is to not use the official SDK, but to use an alternative: AMQP.Net Lite. The AMQP.Net Lite library is a lightweight AMQP 1.0 client library that can be used pretty much everywhere where .NET can be used (in Windows Universal apps, on Linux with Mono, .NET Micro Framework …). Let’s see how we can use this library! For using the code samples below, I’m going to assume you already have an Event Hub set-up in Azure. If you don’t have on, follow the first steps of the Getting Started guide.

When you have an Event Hub, open Visual Studio 2015 and create a new Console application. (FYI: this code will work in a Windows Universal application too; in case you’d like to run it on Windows 10 IoT Core) Then add the AMQPNetLite NuGet package to the project.

EventHubsPi3

Open the Program.cs file and add the following code in the Main method.

string servicebusNamespace = "dummyhub-ns";
string eventHubName = "dummyhub";
string saPolicyName = "send";
string saKey = "X8kBWuLLtj+cNjEwQK68rNQh6NmdkSf/MaMTO5P8u/o=";
string partitionkey = "raspberryPi2";

string data = "{ \"sensor\":\"W10App\", \"value\":123}";

Amqp.Address address = new Amqp.Address(
    string.Format("{0}.servicebus.windows.net", servicebusNamespace),
    5671, saPolicyName, saKey);

Amqp.Connection connection = new Amqp.Connection(address);
Amqp.Session session = new Amqp.Session(connection);
Amqp.SenderLink senderlink = new Amqp.SenderLink(session,
    string.Format("send-link:{0}", eventHubName), eventHubName);

Amqp.Message message = new Amqp.Message()
{
    BodySection = new Amqp.Framing.Data()
    {
        Binary = System.Text.Encoding.UTF8.GetBytes(data)
    }
};

message.MessageAnnotations = new Amqp.Framing.MessageAnnotations();
message.MessageAnnotations[new Amqp.Types.Symbol("x-opt-partition-key")] =
   string.Format("pk:", partitionkey);

senderlink.Send(message);
The first 5 lines declare some variables for which you need fill out the details of your Event Hub:
  • serviceBusNamespace: the name of your Service Bus Namespace in which the Event Hub is created (can be found in the right bottom area of the Dashboard page of your Event Hub in the Azure Portal)
  • eventHubName: the name of your Event Hub, can also be found in the Dashboard page
  • saPolicyName: Shared Access Policy name, can be configured in the Configure section of your Event Hub in the Azure Portal (for this code to work, the Policy needs to have at least Send permissions)
  • saKey: Shared Access Key, the Primary or Secondary key, also to be found in the Event Hub Configure section of the Azure Portal.
  • partitionKey: can be any string value, a way to uniquely identify the sender of events that logically belong to each other (so they end up in the same Event Hub Partition)

When you run the app (either locally or on the Raspberry Pi 2), click on the button, an event will be sent to your Event Hub!

  1 comment for “Using Azure Event Hubs on the Raspberry Pi 2

Leave a Reply

Your email address will not be published. Required fields are marked *