How to store Raspberry Pi SenseHat data to Azure table storage

I will be showcasing a couple of Azure IoT tutorials, I will be starting the series with utilizing Azure Table storage for IoT. All my tutorials will be based on RaspberryPi SenseHat module.

 

image

 

You need to have Python package to be installed on Raspberry Pi for the SenseHat module and Azure storage for your script. The following commands sample below illustrates how this can be achieved on Raspberry Pi.

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install sense-hat ( https://pythonhosted.org/sense-hat/api/ )
sudo pip install azure-storage ( https://github.com/Azure/azure-storage-python )

sudo reboot

Next, we will create a storage account on Azure Portal.
You will require the generated access key for your application to store data to the provisioned Azure storage.

 

image

Once you are done with the pre-requisites (including rebooting Raspberry Pi), we shall start working on running a couple of Raspberry Pi functions. Please copy and paste the code below with vi editor and save it with python file extension. In my case I saved it as “j.py” in Raspberry Pi.

Please do update the Account Name and Access Key information specific to your information.

After run the script, you should see blue led displayed in the middle of the LED matrix. At this point, the SenseHat is gathering information on temperature, pressure and humidity. All this information are stored in the configured Azure table storage. The interval is set to 1 minutes for this tutorial.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import sys
import time
from sense_hat import SenseHat
from datetime import datetime
from azure.storage.table import TableService

sense = SenseHat()

table_service = TableService(account_name='<<storage accout namered box>>‘, account_key='<<storage account access key value;blue box>>‘ )

#define table name
table_name = ‘SenseHat’
#define partition key
partition_key = ‘pisensehat’
#create table
table_service.create_table(table_name, False)

while True:
          #blue LED indicate to start this script
         
sense.set_pixel(4, 4, [0, 0, 255])


         
date = time.strftime(“%Y-%m-%d %H:%M:%S”)
         
raw_temp = sense.temp
         
calctemp = 0.0071 * raw_temp * raw_temp + 0.86 * raw_temp – 10.0
         
temp = “{0:.2f}”.format(calctemp)
         
humidity = “{0:.2f}”.format(sense.humidity)
          pressure = “{0:.2f}”.format(sense.pressure)

          entry = {‘PartitionKey’: partition_key, ‘RowKey’: date, ‘Temperature’: temp, ‘Humidity’:humidity, ‘Pressure’:pressure}

           #insert data to the table
          
table_service.insert_entity(table_name, entry)

           time.sleep(5)
          
sense.clear()
          
time.sleep(55)

You should be able to view the table storage from Azure portal, the application will create a table based on the table name provided in the script. All the information related to temperature, pressure and humidity will be stored in this table.

image

You need to install Azure Storage explorer to view the content of Azure tables. You can download Azure Storage explore from the following URL : https://azurestorageexplorer.codeplex.com/.
The contents of the table is as shown in the screenshot below.

image

You can export the table data to csv file and import the file in Power BI desktop edition like below.

image

In this tutorial, I’ve briefly illustrate how Azure table storage can be integrated with IoT device(s).
In my next tutorial, I will be covering Azure Event hub, Stream Analytics with IoT.

Leave a Reply