Setting up Microsoft Azure Service Bus on the Raspberry Pi

The concept of the Internet of Things revolves around connecting everyday (household) objects to the internet. The important thing to consider is that all this data is being generated, but is only useful if something is being done with it. The Microsoft Azure Service bus allows you to process queues of such data and make it more useful.

In the previous post I showed you how I utilised a USB Webcam and a Raspberry Pi as a motion detection device. The next step in this process is to connect the Raspberry Pi to the Microsoft Azure Service Bus. Ordinarily you would use a Windows PC with Visual Studio and C#, but in this case we want to use Python since this is easily supported on the Rpi. We will utilise Qpid Proton which is a messaging library that has support for a Python API.

Once again, I’ll assume that you have a basic understanding of Linux terminal commands, and will be controlling the Rpi via PuTTY (SSH Client, see http://www.chiark.greenend.org.uk/~sgtatham/putty/). I have also previously set up a Service Bus queue, which I will use to send and receive sample messages.

The first step is to install Qpid Proton:

wget http://mirror.catn.com/pub/apache/qpid/proton/0.7/qpid-proton-0.7.tar.gz

Then unpack it:

tar zxfv qpid-proton-0.7.tar.gz

Navigate to the directory, and display it’s contents:

cd qpid-proton-0.7/
ls-l

Blog-SB-1

Figure 1: Contents of the qpid-proton directory

The first thing to logically do is read the README file.

cat README

Blog-SB-2

Figure 2: Contents of the README file

Figure 2 above shows the steps needed to build the API. Note that it says yum but we will use apt-get as that is the package manager for Ubuntu. Due to this, we will also have to look for the appropriate library names for the package manager. It is also worth noting that some of the packages are already installed by default so we shall not install them. We will also skip installation of files needed for Ruby and Java as we are only interested in using Python.

sudo apt-get install cmake
sudo apt-get install uuid-dev
sudo apt-get install openssl-dev
sudo apt-get install swig
sudo apt-get install python-dev

Once this is done, we will create the build directory for cmake and change the working directory to it.

mkdir build
cd build

Now run cmake, as specified in the README file.

cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DSYSINSTALL_BINDINGS=ON

As it builds, check that no errors are spotted, and that it finds the required files. The next step is to run:

sudo make install

This will take a couple of minutes to run as it compiles the necessary files.

After this step is completed, we are ready to test the new set up. There are a couple of example python scripts provided to allow you to send and receive messages. Let’s first go to the correct directory:

cd ..
cd examples/messenger/py

Blog-SB-3

Figure 3: Contents of the Python examples directory

We will first attempt to send a file. Open the send.py file by:

sudo nano send.py

The only change I make is change the default address from amqp://0.0.0.0 to the address of my previously set up Service Bus queue. Save and close the file.

Now we send a sample message “TestingforRpiblog1” using the send.py script by:

python send.py TestingforRpiblog1

I get a confirmation message: “sent: TestingforRpiblog1”. Success!

Now to receive the message. Open recv.py:

sudo nano recv.py

Again, the only change I make is altering the address from the default amqp://0.0.0.0  to the address of my working Service Bus queue. Save and close the file.

Now run recv.py:

python recv.py

Blog-SB-4

Figure 4: Sent and received the “TestingforRpiblog1” message

You should receive the message alongside some other information. Once the message is received, you will also run into an error-this is most likely since the receive file has reached the end of the queue and there are no more messages to receive. We won’t worry too much about this right now. Use ‘Cntrl+C’ to end the process.

If you run recv.py again, you should see that you receive the message again. This means that the message has not been taken out of the queue on the Service Bus. We need to make a few changes to ensure this is done correctly.

Open up the recv.py file again:

sudo nano recv.py

We will make 2 changes:

1. Add the following 2 lines after ‘mng.start ()’ :’’

mng.incoming_window = 1024
mng.outgoing_window = 1024

2. Change the commands under ‘try:’ to the following:

tkr=mng.get(msg)
      print tkr
      mng.accept(tkr)

Save and close the file. Check that this works by receiving the message again. you should receive the same “TestingforRpiblog1” message, and have no errors. If you try again after a minute or so, it should not receive the same message again. Test a couple of times by sending and receiving messages, ensuring that no messages are received twice.

Blog-SB-5

Figure 5: Sent and received test messages

Congratulations! That completed the set-up required to run the Service Bus from your Raspberry Pi. There are several other things you can do from here, such as send properties, carry out asynchronous sending and receiving, etc. Experiment and see how far you can get your devices on the Internet of Things!

Article by: Umang Rajdev

External Links/Further Reading: These are some of the links I referred to while carrying out this project.

http://qpid.apache.org/proton/
http://qpid.apache.org/releases/qpid-proton-0.4/protocol-engine/python/api/proton-pysrc.html
http://qpid.apache.org/releases/qpid-proton-0.7/protocol-engine/python/api/proton.Messenger-class.html
http://osdir.com/ml/users-qpid.apache.org/2013-12/msg00082.html

One thought on “Setting up Microsoft Azure Service Bus on the Raspberry Pi

  1. Amir

    Hi Steve,

    I tried to follow your steps but struggling with the correct URL for the send test. As far as I could find out, ampq requires a username and password ( ampq://[username]:[password]@mysb.servicebus.windows.net ) and that you should use the default issuer as the user ( owner ) and the default key as password. There are different articles some telling me to url encode the default key (I tried with both) but I always receive an error:

    pi@(none) ~/qpid-proton-0.7/examples/messenger/py $ python send.py TestingforRpiblog1
    [0x1b00890]:ERROR[-2] AMQP header mismatch: ‘AMQP\x02\x01\x00\x00’

    recv: Connection reset by peer
    [0x1b00890]:ERROR[-2] AMQP header mismatch: ” (connection aborted)

    CONNECTION ERROR connection aborted (remote)
    sent: TestingforRpiblog1

    I also tried to add /queuename to the url without any success. Could you give me some more details how you built the url or maybe u got an idea where the problem sits.

    Thanks.

    Amir

    Reply

Leave a Reply

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