Send accurate temperature data from Raspberry Pi to Azure Event Hub

This is a follow-up post to my previous one which is about sending accurate temperature data from the MCP9808 temperature sensor board to an Azure Event Hub. This is done through the MCP9808 Python library provided by Adafruit, and also one which I have repurposed from the Xtrinsic sensor board. This is the updated version. Inside the ~/Adafruit_Python_MCP9808/examples directory, I made a copy of the script as

First step is to import eventhubms and socket.

import socket
import eventhubms

The parts which I had modified are the following:

# Loop printing measurements every second.
print 'Press Ctrl-C to quit.'
hubClient = eventhubms.EventHubClient()
parser = eventhubms.EventDataParser()
hostname = socket.gethostname()

while True:
temp = sensor.readTempC()
message = 'Temperature: {0:0.3F}'.format(temp)
body = parser.getMessage(message,"MCP9808")
print "\n"+body+""
hubStatus = hubClient.sendMessage(body,hostname)
print "[RPi->EventHub]\t[Data]"+message+"\t[Status]"+str(hubStatus)

The event hub client is no different that the one I described in my previous post for sending data from the Xtrinsic sensor board. The event data is sent to the event hub via its REST API. It’s pretty simple.

Then to make sure that the event data is sent correctly and can be consumed from the event hub, I made use of Azure Stream Analytics which is the simplest way to set the event hub as an input. Otherwise you would have to write code to either directly receive events from the event hub or use the EventProcessorHost like how I used in my scalable event hub processor in an Azure worker role.

Since this is a separate topic by itself, I will be writing another post about how to create a new Stream Analytics job to add an event hub as the data stream from which events data will be consumed and transformed by the Stream Analytics job.

Categorized as IoT