How to receive events from an Microsoft Azure Event Hub on a Raspberry Pi

I received a question on my “How to send events from a Raspberry Pi to Microsoft Azure Event Hub” post:

“I want to ask about do you know “how to receive events from azure event hub to raspberry pi””

After a little research I found an article outlining how to do so , via the AMQP protocol using Proton C.  However, I found myself wondering about the use case.

Typically, we are sending events from our IoT devices to some cloud application for analytics, machine learning, or the like.  Event Hubs are great for those high volume cases.

However, if you need it for a messaging scenario, queues or topics might be more appropriate, and they are directly supported by the Azure Python SDK.  In my own case, I elected to use a simple long-polling strategy to have my raspberry pi listen to my cloud app for instructions.

So, can it be done?  Yes, here’s how.  But it may not be the right tool.

“Alexa, raise my desk to 15 inches”

Last year, I built a DIY Raspberry Pi controlled electric standing desk.  It was a great project and I’ve gotten a ton of use from it.

To move the desk, I simply had to:

  1. Launch PuTTY on my computer
  2. PuTTY to the raspberry pi
  3. Log in
  4. Navigate to the directory where the program was running
  5. type sudo python3 robotdesk.py
  6. Tell it what height to move to

It works and is definitely better than sitting all day… but we can do better than that, right?

Yes, we can.  My desk is now voice-activated via Amazon Alexa!

Isn’t that better?

How does it work?

There are 3 parts to the integration:

  1. Alexa Skill
  2. Azure-hosted API
  3. Desk Controller

voicesolution

Alexa Skill

Amazon has an SDK for the Amazon Echo family devices called the “Alexa Skills Kit“.  In short, the Alexa skill contains 3 things:

  1. The things your skill can do (called intents)
  2. Sample ‘utterances’ and how they map to the intents
  3. Instructions on how to call your program

Your program can either be an Amazon Lambda program, or it can be a HTTPS API that the Alexa service will post a message to.

It’s easy to set up a simple skill to get started.  Check out the Amazon developers site for how tos, tutorials, etc.  Or if you want to learn in a bit more structured way, check out “Developing Alexa Skills for Amazon Echo” on Pluralsight.

The speech assets for my desk are on github.

API

The API is simple and does 2 things:

  1. Receives commands from the Alexa service and translates the intent and parameters to a desk command
  2. Responds to the desk controller’s requests for command.  The desk is using a long-polling mechanism to get commands.

The code is here (disclaimer: this is hack-it-together code, not production quality!)

Desk Controller

The desk controller uses the python program I had previously created to control the desk, but adds long-polling to get commands, rather than waiting for someone to type into the console.

What’s Next?

The program has lots of room for improvement, the Amazon process for certifying skills is rigorous, and the interactions with the desk will get better as I find the terms that I want to use to control it.

Have feedback?  I’d love to hear it.  Leave a comment or reach me @_brentonc on twitter.

How to change the name of an Azure Subscription

A colleague and I both have Visual Studio Ultimate MSDN subscriptions, with corresponding Azure subscriptions, and have given one another access for various little things as we’ve explored Azure.

Unfortunately, this got confusing with our subscriptions having the same name, and has occasionally resulted in us creating things under the wrong subscriptions.  In the Azure portal it’s not too bad because you are also filtered by directory, but in Visual Studio and other places it can be tough to figure out.

For example, Get-AzureSubscription returns two subscriptions with the same name!  Which one is mine, and which one is Jack’s?

subscriptions

Thankfully, you can change the name of a subscription:

  1. Login to https://account.windowsazure.com/Subscriptions
  2. Click on the Subscription you’d like to renameportal-before
  3. On the right side of the page, click “Edit subscription details”edit
  4. Choose your new name & savemakeityours

Voila!  Subscription names are now much more clear:

Subscriptions

How to send events from a Raspberry Pi to Microsoft Azure Event Hubs

Sending data from a Raspberry Pi to a Microsoft Azure Event Hub is remarkably easy.  Here are the steps:

1) Configure your Event Hub in Azure

This article on MSDN shows the steps to set up an Event Hub.  It’s very easy and takes just a minute.  But when you get to the part about how to send messages, come see #2 and #3 below!

2) Install the Azure SDK on your Pi

This should be pretty easy, but I had a little bit of trouble getting pip 3.2 running on my Raspberry Pi 2 B.  Once I did get pip-3.2 installed (because Python 3.2 is what comes on the Raspberry Pi), it’s easy:

pip-3.2 install azure

3) Write a little code

Once the Azure SDK is installed, you can write your “Hello Event Hub” program.  Here’s what mine looked like:

azure poc

You can download this sample on github: https://github.com/brentonc/IoTSmoker/blob/master/samples/azurepoc.py

Note, this sample is largely based on this article in the Azure SDK documentation.

The only things that weren’t immediately obvious to me from the official SDK documentation was what the shared_access_key_name,shared_access_key_value, service namespace, and hub names were.  Here are a couple screenshots to help you visually map the data points:

Namespace and Event Hub names:

Namespace and Event Hub Names

Shared Access:

Shared Access

4) Glory! 🙂

Once you get this set up, you’re ready to event!  The Azure portal gives you a handy dashboard to see the rate a which messages are coming through (but, there’s a bit of a delay, so don’t worry if there is a delay between when you start sending messages and there is something to see in the dashboard):

Event Hub Dashboard