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.

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

New DIY/IoT/Raspberry Pi Project: Smoker Temperature Controller, or “My Smoker is an Internet of Things Thing”

For my next project I am going to build a wifi enabled, Raspberry Pi controlled temperature controller for my Weber Smokey Mountain (WSM) smoker.

The temperature of a charcoal smoker is controlled by the flow of oxygen.  More oxygen, more heat.  This is usually done by checking the temperature of the smoker every now and then and adjusting the vents – more open to get it hotter, and less open to make it cooler.

The idea is simple – thermometer connected to a Raspberry Pi, a fan/blower plugged into the WSM, and a small python program that tells the blower to blow if the temp gets too low.  It’ll be interesting to see what other adjustments need to be made once the base solution is in place.  And once it’s going it becomes simple software to push the temperature readings and other data to my phone or other devices.

The WSM actually keeps its temperature quite well, and doesn’t require much babysitting most of the time, so this project is very much in the ‘because I can’ column. But, with something like this it will be much more feasible to smoke overnight, for example (om nom brisket!).

Also – yes, I know I can buy something that is ready made for not a lot of money.  But what fun would that be?