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
  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


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.


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.

Standing Desk: Wiring 6 relays to 2 GPIO pins

A comment on a video about my desk recently asked,

I’d like to know how you wired up everything (the pi to the relayboard) using 2 GPIO pins. Could you please take a picture and upload it to your blog?

Good question, Jake.  Here’s a picture.



But that’s not too clear, let’s break it down.

  1. This gray band connects the RPi to the breadboard to make it easy to make multiple connections.  But you can connect directly from the pins to the breadboard; just make sure you bring power and ground as well.  And you’ll beed male/female connecting wires to make the connection.
  2. Connect the 5v power (red wire above) and ground (black wire above) to the power/gnd pins on the relay.
  3. The breadboard lets us connect multiple wires to a single pin.  When that pin is ‘on’ all three connected wires are signaled.  In the picture above, the 3 green wires on one pin; the 3 blue wires are connected to a second pin.
  4. The green and blue wires are connected to the relays in alternating pairs.  Each one turns on one relay when signaled, so each time the pin on the RPi is on it triggers 3 relays in my case (one for each actuator in my desk).
  5. For each of my actuators I use two relays.  This allows the signal to be reversed by flipping which switch is on, thereby telling the actuator to extend or contract.

So, after all this, the code to extend, retract, and stop is easy!


Good luck!

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:

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

Getting Robogaia Raspberry Pi Temperature Controller to work

I recently got a Robogaia Raspberry Pi Temperature Controller and tried to fire it up.  Their website has the configuration steps as well as sample code.

However, after running through the install steps and attempting to run the samples, I received the following error:

Traceback (most recent call last):
File "", line 9, in
bus = smbus.SMBus(0)
IOError: [Errno 2] No such file or directory

Thankfully, the answer was found on the raspberry pi forums, though about a different device. you simply need to enable i2c via raspi-config -> Advanced

After a reboot, it works great!

Thanks to the Robogaia guys for saving me some work with their handy plate!

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?

DIY Motorized Desk Powered by Raspberry Pi is Operational!

It’s alive!

This has been a long time in the making, and I’ve posted a number of times during the process:

This has been an incredibly fun project.  It definitely would have been faster and probably more cost effective to just buy a desk or table lift, but the journey was well worth it.

DIY Motorized Standing Desk Part V: Test with 3 actuators controlled from Raspberry Pi

Edit: here is the final operational desk, utilizing the 3 actuators

I have the control board for my DIY motorized standing desk wired up, and ran a test of the board controlling 3 linear actuators at the same time.  I knew the wiring and software worked for 1 actuator, but this was the first time that I connected more than one actuator up.  The desk will be lifted by 3 actuators so I needed to be sure this worked.

Here it is, worked on the first try and nothing caught on fire!

One thing of note – I had initially planned to control each actuator independently from the Raspberry Pi, with each relay signaled from a different GPIO pin.

However, in the midst of wiring the control board up I realized that it was possible that a software bug, crash, or other unforeseen scenario on the Pi could theoretically result in actuators moving in different directions, and that would be a Very Bad Thing (images of broken desks, shattered monitors, and crushed babies come to mind).

So they are all wired now on 2 GPIO pins (one for the “A” relays, and one for the “B” relays).

So now the leg apparatus’ are built, and the control board is working.  Next step: put it all together!