Skip to content

Azure IoT Hub Demo

Infrastructure Overview

IoT application on Azure to connect a RaspberryPi to IoT Hub.
Sends device-to-cloud telemetry and allows cloud-to-device callbacks to the Raspberry Pi.

Github Issues Code Style LinkedIn Sander van Dorsten


Documentation: https://sandervandorsten.github.io/azure-iothub-demo/

Source Code: https://github.com/sandervandorsten/azure-iothub-demo


Do you want to have a fresh breeze in you home office, but are you to lazy to turn on the fan yourself? With this project you'll have your office cooled down in notime!

About The Project

This project helps you setup a relatively simple Azure Infrastructure for connecting your own Raspberry Pi to the cloud. You can use this as a starting point to develop small IoT applications.

Features

  • The possibility to simulate a Raspberry Pi from your computer to speed up development
  • Simple data storage in a container for 'cold' analysis
  • Data flowing through Stream Analytics to perform live analysis and send this along to other Azure Services
  • Infrastructure as code using ARM templates so you can deploy your own easily
    • Estimated costs: < 10EUR / Month (if you set IoT Hub to use a free account, limited to 1 per subscription)
  • An easily extendable infrastructure, for example if:

Background

Getting Hot it HereI started this project whilst working from home on different coding projects on Azure in the summer during the COVID-19 outbreak. Whilst working from home certainly has it's advantages, I did miss the AC that we have at the office at times.

Attaching a fan Whilst the temperature in my home-office sky-rocketed in June, I bought a ventilator to keep me cool. This was great, however having it turned on at all times is a bit of an overkill as it is certainly not hot all day-everyday here in the Netherlands. My ventilator however did not support temperature-based-control, hence I wanted to build this myself.

Raspberry Pi I had a Raspberry Pi lying around so wanted to use this as an interface for measuring temperature and controlling the ventilator. As I was working on a number of projects in Azure, I wondered if I could use their cloud services to connect this raspberry Pi to the cloud. Hence I asked myself:

Can I connect a Raspberry Pi to IoT Hub to 1) send device-to-cloud telemetry data and 2) trigger the ventilator on my desk with a cloud-to-device message?

In my mind, the application should contain a few components:

  • Raspberry Pi:
    • stream temperature data from device to the cloud.
    • listen to signal that triggers fan activation from the cloud.
  • 'arbitrary-cloud-services':
    • data should be stored somewhere
    • Once a certain temperature threshold has been exceded, a message should be returned to the Raspberry Pi to trigger the fan.

Infrastructure

I ended up selecting the following infrastructure based on this video:

  • Raspberry Pi to connect sensor and fan to. This project also includes a simulated Raspberry Pi device that you can run on your computer to speed up development.
  • Azure IoT Hub. Allows you to securely connect IoT devices to Azure cloud, from which you can further route messages to different services.
  • Stream Analytics. Performs streaming data analysis on your IoT input data and forwards the data to specified services.
  • Blob Storage. Cold storage for the data.
  • Service Bus Queue. Queue used as input/trigger for Azure Functions.
  • Functions. Serverless function that is triggered by the aforementioned Service Bus Queue, and sends a message via IoT Hub to the Connected Raspberry Pi device.

Infrastructure Overview

Control Flow

  1. A Raspberry Pi collects data from a connected temperature sensor. It sends this so-called telemetry data to it's designated IoT Hub.
  2. The IoT Hub processess incoming and outgoing requests from/to connected IoT devices. It has a Messaging Endpoint, from which Stream Analytics ingests the Raspberry Pi temperature telemetry for further processing.
  3. Stream Analytics writes all telemetry temperature data to a blob storage, and writes a subset of the temperatures to a Queue on the Service Bus. In this example, we send only the telemetry data temperature > 29 to the Queue.
  4. When a new message is stored on the Queue, a Azure Function is triggered.
  5. The function app sends a message to the IoT hub that the fan of the connected Raspberry Pi should turn on. Effectively in our example this means that the fan will turn on for a while if, and only if temperature > 29.
  6. The IoT Hub securely forwards the message to the Raspberry Pi to start the connected ventilator.

Getting Started

In this step-by-step guide I'll help you set up everything to get your own application up and running. The setup is going to cover the following parts:

  1. Deploying your own copy of the infrastructure to Azure using ARM templates
  2. Installing and configuring the individual services, specifically:
    1. IoT Hub: Registering a (simulated) Raspberry Pi device
    2. Stream Analytics: Starting our analysis job
    3. Azure Function: Deploying a function
      1. Downloading the repository
      2. Running our functions application locally
      3. Simulating a RaspberryPi Locally
      4. Deploying your function app to Azure