In the emerging world of Internet of Things (IoT) we see more and more hardware manufacturers releasing development platforms to connect devices and sensors to the internet. Traditionally these kind of platforms are created around microcontrollers, and the Arduino platform can be considered as the standard in (consumer) physical computing, home automation, DIY and the ‘makers community’.

Most Arduinos come with an 8-bit AVR RISC-based microcontroller running at 16 MHz with the modest amount of 2 kilobytes of memory. These devices are perfectly capable of calling REST services with the Ethernet library and Arduino Ethernet shield. However, we do face some challenges when it comes to encrypting data, generating Azure shared access signatures and communicating over HTTPS due to a lack of processing power and memory. The Arduino platform has no SSL libraries and therefore cannot securely transmit data over HTTPS. This article shows a solution to this problem by using a secondary device as a bridge to the Internet.

Microsoft Azure allow us to store and process high volumes of sensor data through Event Hubs, currently still in preview. More information on Event Hubs, its architecture and how to publish and consume event data can be found here. In this article I focus on how to publish sensor data from these ‘things’ to an Azure Event Hub using a microcontroller with field gateway that is capable of communicating over HTTPS using the Azure Python SDK.

Azure Event Hubs

Before we start we need to create an Azure Service Bus Namespace and an Event Hub. This can be done in the Azure management portal:

Creating an Azure Event Hub

Creating an Azure Event Hub

When creating the event hub we need to specify the number of partitions. The link provided earlier will describe partitioning in detail, but in summary this will help us to spread the load of publishing devices across our event hub.

Event Hub Partitioning

Event Hub Partitioning

We can also define policies that can be used to generate a shared access signature on our devices that will be sending event data to the hub:

Event Hub Policies

Event Hub Policies

Arduino Yun

The Arduino Yun combines a microcontroller and ‘Wi-Fi System on Chip’ (WiSOC) on a single board. The microcontroller allows us to ‘sense’ the environment through its analogue input sensors, whereas the WiSOC runs a full Linux distribution with rich programming and connectivity capabilities. The WiSOC can be considered as the field gateway for the microcontroller and is able to send data to the Azure Event Hub. For other Arduino development boards that only have a microcontroller you can for example use a Raspberry Pi as the field gateway.
For the purpose of this demo we’ll keep the schematics simple and just use a simple temperature sensor and some LEDs to report back a status:

Yun temperature drawing_bb

The Arduino sketch reads the voltage signal from the temperature sensor and converts it to a temperature in Celsius degrees as our unit of measurement. The microcontroller communicates with the Yun Linux distribution via the bridge library, and blinks either the green or red LED depending on the HTTP status that is returned from the Linux distribution.
The complete Arduino sketch looks like this:

The Arduino bridge library is used to run a Python script within the Linux environment by executing a shell command to send the temperature to the Azure Event Hub. Next we’ll have a look at how this Python script actually works.

Python SDK

The Microsoft Azure Python SDK provides a set of Python packages for easy access to Azure storage services, service bus queues, topics and the service management APIs. Unfortunately there is no support for Event Hubs at this stage yet. Luckily Microsoft is embracing the open source community these days and is hosting the Python SDK on GitHub for public contribution, so hopefully this will be added soon. Details on how to install the Azure Python SDK in a Linux environment can be found on http://azure.microsoft.com/en-us/documentation/articles/python-how-to-install/. You can use a package manager like pip or easy_install to install the Python package ‘azure’.

The complete Python script to send event data to an Azure Event Hub is as follows:

The script can be called with a series of sensor values in the following format:

[sourcecode wraplines=”false”]python script.py temperature:22,humidity:20 deviceid[/sourcecode]

Multiple key-value pairs with a sensor type and sensor value can be provided, and these will be nicely stored in the JSON message.

By using the ServiceBusSASAuthentication class from the Python SDK we can easily generate the shared access signature token that is required by the various services in the Azure ServiceBus, including Event Hubs.
Sending the actual event data is done with a simple REST call. Although Event Hubs allow any arbitrary message to be sent, we send JSON data which is easy to parse by potential consumers. Event data will be sent to a specific partition in the Event Hub. The hostname of the Arduino Yun is used as the partition key. Azure is taking care of assigning an actual Event Hub partition, but by using the hostname as the partition key it’s more likely that traffic from different devices is spread across the Event Hub for better throughput. The Python script will create the appropriate REST HTTP request according to the Azure Event Hub REST API:

When we deploy the Arduino sketch it will start sending the temperature to the Azure Event Hub continuously with one second intervals. We can confirm successful transmission by consuming the Event Hub data with Service Bus Explorer:

Service Bus Explorer

Service Bus Explorer

Conclusion

I’ve demonstrated how we can combine the Arduino microcontroller platform to read sensor data with a more powerful computing environment that runs Python. These allow our ‘things’ to leverage Azure Event Hubs for event processing with the potential to scale to millions of devices.

Category:
Uncategorized

Join the conversation! 18 Comments

  1. Hi
    Very well written thanks. But I believe you have a minor bug in your python script.The else part of if first == True should be out of the if statement. Otherwise it is discarding first item in the payload comma delimited string.

  2. Check out my IoT Proof of Concept, that unlike this example has a small embedded device directly communicating with Azure Event Hubs. The Texas Instruments CC3200 Single Chip WiFi MCU has SSL/TLS support, which makes it possible to communicate directly with Azure Cloud, no need to use a gateway or broker service.

    http://ssmlwf.azurewebsites.net/

  3. Can we contribute this client back into the Azure SDK? If you issue a pull request for it I’ll make sure it’s merged in.

  4. Hi. Thanks for posting so detailed procedure to hook the sensor data on to the cloud.
    Could you please let me know, if we could follow the above mentioned steps works on the Arduino Uno (with Arduino Wi-Fi shield). We are worrying about the memory constraints.

    • Hi Siddu,
      I’m afraid not. Besides the memory constraints that you will be facing, the Uno doesn’t have TLS/HTTPS capabilities for securely transmitting sensor data to an Azure event hub. Azure event hubs require HTTPS. Alternatively you can look at Azure mobile services which allow plain HTTP, although this obviously isn’t recommended from a security point of view.

  5. Getting AssertionError at httpclient.perform_request(request). Any thoughts?

  6. I tried to repeat this and had problems with the Azure install. It throws various warnings and then, when I try to run the script, it tells me there is no servicebus python function.

    Are you sure this code still works with the latest Azure Python releases?

    Thanks in advance

    • Hi Daron,
      The code should still work with the latest release, which includes the servicebus functionality. What errors are you getting with the installation?

  7. It is not clear for me where sensors data will be saved in azure. I mean how azure will know the structure of sent data, number of sensors, which data related to which sensor.
    From where in azure I can access table that saved all sensors data.

    Also Python script that handle communication with IOT HUB, where this script should be located in raspberry

    Great work and thx in advance 🙂

  8. hey any idea if esp8266Nodemcu can be used independently with Azure Event Hub!!!

    • I’m not familiar with that particular device, but if it supports HTTPS and you can make REST calls you should be able to communicate with event hubs.

Comments are closed.