Real-Time Air Quality Monitoring with Azure IoT Hub – Stochastic Coder

In this post I want to focus on leveraging Azure IoT Hub to quickly implement a real-time air quality monitoring solution. Azure IoT Hub device twins is used simplify device management as the device are expected to move locations in the future. Device twins are JSON documents that store device state information including metadata, configurations, and conditions.

By the end of this project you will have a real-time air quality monitoring and reporting solution. Your IoT devices will communicate to IoT Hub for device-to-cloud and cloud-to-device messaging. Azure Stream Analytics uses a SQL like language to process and transform the air quality stream. Power BI enables interactive visualizations against the stream. Cosmos DB (optional for this project) can be employed to store the IoT JSON documents.

IoT Device for Air Quality

Similar to the Azure Event Hubs post, NodeMCU 8266 from HiLetgo was used to build the IoT devices. Their small form-factor, low cost and use of Arduino libraries make them the perfect fit for the project. The air quality monitoring device will measure temperature, humidity, CO2 and VOCs. (As a side note, I am not loving the CCS811 sensor after months of use and am open to any recommendations/comments).


Adafruit CC811

DHT 11

Set-up Arduino IDE

Next we will want to download the Arduino IDE.

With the IDE installed we need to add the esp8266 board.

Go to File -> Preferences. Add the URL below to the section called: ‘Additional Boards Manager URLs’.

With the URL added, we can add the board.

Add the board by going to Tools -> Board -> Board Manager and search for ‘esp’. Install esp8266 by ESP8266 Community.

Add Libraries

Dependent libraries are required for the code to execute. To add the libraries in the IDE select Sketch -> Include Libraries -> Manage Libraries.

From the Library Manager window, search for the libraries listed below and install.

Download the Code

You will need to add your WiFi SSID and WiFi Password to the code in AirQuality_ESP8266.ino.

 //***** Change SSID and Pass **********************
  status = WiFi.begin("<WIFI SSID","WIFI PASS");

Setting Up Azure

If you don’t already have an Azure account, you can create a free account today.

Azure resources that I created for this post.

Resource Use Required

Azure IoT Hub

IoT messaging
and management


Cosmos DB

Stored IoT messages for low latency
geo-distributed access (web apps)


Power BI

Interactive Visualizations using
IoT Streams

Required for

Azure IoT Hub

The tutorial Connect Adafruit Feather HUZZAH ESP8266 to Azure IoT Hub in the cloud is a perfect place to start if you are new to IoT Hub. I used it as a launching pad for this project and will touch on some key elements to get you set-up with Azure IoT Hub.

First, create a new Azure IoT Hub Resource. The Free Tier can be used for this project.

Add your devices when the IoT Hub Resouce is created by going to IoT devices. In this example I am using two devices to monitor air quality: air1 and air2. Click New to add your device.

Connecting devices to Azure IoT Hub

After adding the devices we are ready to get then connected.

We will need the iothubowner connection string to generate the device SAS tokens with Device Explorer. Copy the connection string from IoT Hub in the Azure Portal under Shared access policies. The steps below use Device Explorer to generate the device SAS tokens; please install if you want to follow along.

Paste the iothubowner connection string from the Azure Portal into Device Explorer and click Update.

Generate Device Tokens

After clicking update, barring any errors with the connection string, you will be able to manage your devices.

Click the SAS Token button for a device. Set the TTL (Days) and click Generate. Copy the generated SAS token as we will need it for our device to connect to IoT Hub.

Modify Device Code

Paste the generate device connection string into AirQuality_ESP8266.ino

/************ Change Connection String*************/
char* conStr  = "<Add you device SAS Tocken from the Device Explorer>";

Device Twins

The connection interval and device location can be modified from IoT Hub by updating the device twin. You may want to set-up air quality monitoring in a basement, bedroom, warehouse, etc. for an interval of 1 minute or every 30 minutes. To modify the default settings go back to IoT devices in IoT Hub using the Azure portal. Select IoT devices

Select a device and click Device twin (this needs to be done for all devices).

Add location and interval to desired properties to overwrite the default values from the code we uploaded using the Arduino IDE. As you can see the interval will update to 1 minute (60000 milliseconds) and the location will be called ‘lower_level’ for device ‘air1’. You can modify these to fit your needs.

Many more devices?

It is fairly easy managing a couple devices in this manner. However, check out Azure IoT Hub Device Provisioning Service when managing many more devices.

Azure Stream Analytics

We use Azure Stream Analytics to transform and apply analytics to our air quality messages streaming into Azure IoT Hub. Steam Analytics employs SQL and JavaScript to provide an easy to use platform. With it we will ingest our IoT data and write to Power BI and Cosmos DB (a great low latency geo-distributed option for storing the JSON documents for additional application integration).

Add a new resource, Stream Analytics job.

Stream Analytics – Inputs and Outputs.

Stream Analytics Inputs

The air quality data we want to access is streaming into our Azure IoT Hub. Click Inputs and Add Streaming Input. Add IoT Hub as an input.

Stream Analytics Outputs

Power BI

At minimum we will want to stream data to Power BI for real-time dashboards and reports. Additional output for Stream Analytics can be found using this link: Understand outputs from Azure Stream Analytics.

Click on Outputs (similar to adding inputs), click Add and add a Power BI output.

Cosmos DB

Cosmos DB is a great document store for JSON data that Azure Stream Analytics can write to. It is a low latency, multi model, geo-distributed manages service that will scale to accommodate your needs. Cosmos DB is one of many output options that can be found using this link: Understand outputs from Azure Stream Analytics.

To store your messages in Cosmos DB you will need to add the Cosmos DB resource.

To Configuring Cosmos DB; add the following:

  • resource group
  • subscription
  • location
  • unique account name
  • and API (this project uses the SQL API)
Cosmos DB Container

A database and container will need to be added to the recently created Cosmos DB resource.

Using the Azure Portal, go to the Cosmos DB resource and go to Data Explorer. Select New Container from the New Container drop down.

Create a database and container for the AirQuality messages. We can set the RU/s to 400 (learn more about Cosmos DB Request Units). Location would appear to be the logic partition choice given the message attributes.

It is important to note that the partition key can not change and an incorrect partition key may lead to less optimal performance – please put some time behind choosing the best partition key.

Cosmos DB Output

To add Cosmos DB as an output, click Add and select Cosmos DB.

The Cosmos DB database and container settings created earlier in this post are required to configure the Output.

** Epoch is added in the query to act as a document id **

Azure Steam Analytics Query

We can write our query to select from the defined inputs and write to the defined outputs.

On the Overview page, click Edit Query.

Add a simple query to select from your Input(s) and insert into your Output(s).

Query used for this post:

SELECT *, Datediff(s, '1970-01-01',EventEnqueuedUtcTime) as epoch, dateadd(hour,-5,EventEnqueuedUtcTime) as adjustedtime 
INTO    AirQualityCosmosOutput 
FROM    AirQualityIoTInput; 
Select *,dateadd(hour,-5,EventEnqueuedUtcTime) as adjustedtime 
INTO     AirQualityPowerBi 
FROM     AirQualityIoTInput;

Save the query and Start the job from the Overview blade.

Power BI

Stream Analytics will create and populate a new dataset in Power BI called AirQulity as IoT messages begin flowing in.

Power BI can be used to visualize real-time streaming data, or you can build a report off of the created data set to produce an interactive visualization. Power BI is an extremely powerful analytics and BI tool that goes beyond the scope of this post.

To started learning Power BI please check out one of these links:

Real-Time Air Quality Monitoring Complete

You are now up-and-running with your own real-time air quality monitoring solution. However, below are are a few troubleshooting suggestions if you are having problems:



Azure IoT Hub

On Overview screen check to ensure your
devices are writing messages and/or
performing twin operations

‘device to twin operations’ or
‘device to cloud messages’ ??

could be a networking issue or SAS token issue
validate with Arduino Serial Port

Azure Stream Analytics

On Overview screen, is Start gray? If not than
click Start

Azure Stream Analytics

Are there any Input or Output Events being
displayed under Monitoring on the Overview

Your feedback can help ensure the best content is delivered.

Best Regards,


Submit Rating

Average rating / 5. Vote count:

Let us improve this post!

Categories: Azure IoT Hub

Tags: Air Quality Monitoring, Arduino, Azure, Azure IoT Hub, Azure Stream Analytics, BI, ESP8266, Internet of Things, IoT, Power BI


Article by channel:

Read more articles tagged: Internet of Things