I’ve been working on a little project at home recently and I needed to get data from some remote sensors in to the cloud. I also happened to have a couple of Raspberry Pi units begging me to do something fun.

I decided that before I got to deep in the weeds, I should do a couple of small POCs up front. This article outlines my very first POC – Hello World with Azure in the middle.

 

Installing Node on Raspbian

For a number of reasons I chose to use Node.js for this project; mostly because I know JS and we have an Azure SDK available.  It turned out that getting Node installed was a bit more confusing than I expected!

Here is the problem; while there is a package for Node available on Raspbian, it is very much out of date. I was able to compile the latest build from GitHub but this was a “little slow” (as in 20-30 minutes while I sat wondering why I tried to compile this thing from source). Thankfully one of the good folks at AdaFruit had a much better solution and in less than 2 minutes I had Node v0.10.28 up and running.

[sourcecode language=”bash”]
sudo wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
node -v
[/sourcecode]

Additional Packages

Node has its own package managed called “npm” (Node Package Manager). There are a ton of packages for doing all sorts of things, including Azure.

[sourcecode language=”bash”]
npm install azure
npm install node-uuid
npm install async
[/sourcecode]

This will install the following packages:

  • Azure – The Microsoft Azure SDK for Node.js.
  • UUID – A library for generating unique identifiers. We’ll use this to generate record IDs.
  • Async – One amazing library that makes JS async code joyous (how’s that for en endorsement?).

Adding a storage account

Before we can create our table, we need to create a Storage Account in Azure to hold our data. This process is described very clearly in the documentation at http://azure.microsoft.com/en-us/documentation/articles/storage-create-storage-account/.

Hello World

Now that we have our components installed, lets sling some Node goodness at Azure. You can use any editor you like but for here I’ll use nano since it is already installed (I actually tend to use Sublime 2 on Windows over a Samba connection but that require a lot more configuration).

Back at our command prompt, lets tell nano to create and open a new file named helloazure.js:

[sourcecode language=”bash”]
nano helloazure.js
[/sourcecode]

Now we have the ever so lovely nano UI in front of us. We’ll start by telling Node about the libraries we are referencing:

[sourcecode language=”javascript”]
var azure = require(‘azure’); // Azure SDK
var uuid = require(‘node-uuid’); // Unique ID Library
var async = require(‘async’); // Aync code helper
[/sourcecode]

Next we need to tell the script our Storage Account and the secret Access Key. These can be found in your Azure Management Portal; simply go to your Storage Account and select “Manage Keys”. Once you are there, copy and paste the values in to the code below.

[sourcecode language=”javascript”]
var storageAccount = "YourStorageAccountName";
var storageAccessKey = "YourStorageAccountKey";
[/sourcecode]

The next step is to reference the TableService. This is part of the Azure SDK and provides the functions for interacting with data tables.

[sourcecode language=”javascript”]
var tableService = azure.createTableService(storageAccount, storageAccessKey);
[/sourcecode]

Finally we’ll use the Async library’s Waterfall feature to create our table (if needed) and the records (if needed). Waterfall allows us to run multiple functions in sequence while passing data from one function to the next. Note that the first variable returned is always null, this is because the first value is always an error code when using waterfall. If you callback with something else the waterfall stops and an error is returned.

This code is doing a couple of things. First it is creating a table called TestTable1 and then it is adding two records with the values ‘Hello’ and ‘World’ in the property TextLabel. Once those are complete we are reading the records back out of Azure and displaying the results to the console. In essence, this is Hello World with Azure in the middle.

The following is the complete script.

https://gist.github.com/mlafleur/060394f0562beb87c430