Connecting Raspberry Pi to Microsoft Azure with Python3
Introduction
One of the best uses for the Raspberry Pi is in embedded IoT projects. For simple, linear data logging projects, all you need is to add new lines to a Google Sheet. However, there are times when you need the power of a remote database to do the hard work for you. One of the most robust options is Microsoft Azure. I'll show you how to create a new SQL database, install and configure the needed Python modules, and then test to see if everything worked. This tutorial is running on a fresh install of Raspbian-2015-02 with all updates installed as of 2015-04-02.
One note before we begin: This tutorial is for Python3, and we will use the Python3 version of pip to install the modules. I'll mention how to install the Python2 version of the modules, but I don't guarantee everything else will work the same. If you don't have Python3 pip installed, you will need to install it via apt-get with the following line:
sudo apt-get install python-dev python3-dev python3-setuptools python3-pip
Azure SDK
Microsoft offers an official open-source Python module which you can use to tie into Azure tables and blobs among other things. If it's not necessary to have a full SQL database for your project, this is going to be the easiest way to tie into Azure. Read the reference docs in the Azure Python Dev Center before moving on. The great thing about this method is that it's already configured once installed. If you are using Python3, these two lines will install everything:
sudo apt-get install libffi-dev
sudo pip-3.2 install azure
There's only one dependency to install before we do the pip install. Unlike the Python2 version of pip, pip is customized for each new Python version. As of the Feb2015 version of Raspbian, Python3.2 is the default, so we use pip-3.2 to install. You can install the Azure module for Python2 with apt-get:
sudo apt-get install libffi-dev azure
One of the downsides of the azure module is that it doesn't include a way to access our Azure SQL databases directly. For that, continue reading.
Create SQL Database
For the sake of clarity during this tutorial, I'm going to make a new SQL database and use the actual server name, user name, and password in plain text during module setup. By the time you read this, the server will have been deleted, so make sure you replace the tutorial data with your own.
The first thing we need to do is create a new SQL database. If you already have a database you want to tie into, you can do that too. If you already know your existing server name, database name, database user name, and database password, skip to the next section.
At this point, we have everything we need to continue. You might want to take the time to add some data to the database using the Management portal you can access by clicking the "Management" button on the black bar at the bottom of the page. I added a new table Trivia with questions and answers.
Install pyodbc
Now that we have a server ready to go, it's time to install and configure the Python modules. In order to do SQL queries, we're going to use pyodbc. It will give us a Python wrapper to use two other UNIX database utilities, each of which need to be individually configured. If you are unfamiliar with pyodbc, check out its "Getting Started" guide. The dependency installation is via apt-get with this line:
sudo apt-get install unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
Now that the dependencies are installed, they need to be configured.
After all of that, it's now time to install pyodbc. As with the Azure SDK module, we'll install it via pip for Python3.
sudo pip-3.2 install pyodbc
To install it on Python2, use apt-get.
sudo apt-get install pyodbc
Testing
Now it is time to test our installation. Open up a python3 shell and following along with the code below. It shows me connecting to the SQL database and reading data from the table I created earlier.
pi@dupRasPi:~$ python3
Python 3.2.3 (default, Mar 1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "help", "copyright" or "license" for more information.
>>> import pyodbc #If anything went wrong during installation, it will happen here
>>> dsn = 'rpitestsqlserverdatasource'
>>> user = 'myrpiuser@qcezk07lpu'
>>> password = 'Raspbian#1'
>>> database = 'rpitutorial'
>>>
>>> connString = 'DSN={0};UID={1};PWD={2};DATABASE={3};'.format(dsn,user,password,database)
>>> conn = pyodbc.connect(connString) #If anything went wrong during configuration, it will happen here
>>> cursor = conn.cursor()
>>> cursor.execute('select question, answer from [Trivia]')
<pyodbc.Cursor object at 0xb67bc9f8>
>>> row = cursor.fetchone()
>>> if row: print(row)
...
('What is my favorite IoT platform?', 'The Raspberry Pi, of course!')
conn.close()
quit()
Now everything is working. We create the connection string, connect to the server, perform a query, read the response, and finally close the connection. Of course you can do more than just read database contents. The pyodbc module makes it easy to do anything with standard SQL queries. It will hold response data so that it's easy for you to manage whether your query returns ten rows or ten thousand.
What will you make with Azure?