How to Send Raspberry Pi data to Azure

Learn how to send sensor data to the cloud

Posted: January 24, 2017
Tags: Azure  IoT  IIot  Raspberry Pi 
Share this on

In this tutorial we’ll show you have to send temperature and humidity data from a DHT11 sensor up to Azure with a Raspberry Pi, let’s get started:

Get your Raspberry Pi up and Running for projects

First set up to purchase the items you need an perform the a basic installation:

Wire up the Temperature Humidity Sensor for data collection

Raspberry GPIO

Raspberry DHT11

sudo apt-get update
sudo apt-get install build-essential python-dev
def getId():
    iD = "0000000000000000"
    try:
        f = open('/proc/cpuinfo','r')
        for line in f:
            if line[0:6]=='Serial':
                iD = line[10:26]
        f.close()
    except:
        iD = "ERROR00000000000"
        f.close()
    return iD
import ays, datetime, json
import Adafruit_DHT
from util import *
iD = getId()
while True:
    # get timestamp
    dt = str(datetime.datetime.now())
    # get sensor 11 data on GPIO 4
    h, t = Adafruit_DHT.read_retry(11, 4)
    # convert C to F
    f = t * 9. / 5. + 32 # from C to F
    # create json message
    d = {
    'DeviceID': iD,
    'Temperature': f,
    'Humidity': h,
    'Time': dt
    }
    msg = json.dumps(d)
    print(msg)

Saving Events to Azure

sudo pip install azure-servicebus
from azure.servicebus import ServiceBusService
from azure.servicebus import Message
def createSBS():
    service_namespace = 'service bus name here'
    key_name = 'event hub here' key_value = 'mKG+x1xmMHso/ZdL/nZ45retMqVXz+HQQjHp98='
    sbs = ServiceBusService(service_namespace, shared_access_key_name=key_name, shared_access_key_value=key_value)
   return sbs
sbs = createSBS()
sbs.send_event('event queue name here', msg)

The entire Python code is out on GitHub project here.

Posted: January 24, 2017
Tags: Azure  IoT  IIot  Raspberry Pi 
Share this on

I love delivering IoT solutions

Read more about what I do and our philosophy. Judge for yourself the work and results achieved for other clients.

Contact Me

back to top