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:
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
Figure 1: Contents of the qpid-proton directory
The first thing to logically do is read the README file.
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
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:
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.
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.