Real-Time Air Quality Monitoring with Azure IoT Hub

This post emphasizes leveraging Azure IoT Hub for real-time air quality monitoring. It includes device management details, hardware components, Arduino IDE set-up, Azure configuration, and Power BI visualization instructions.

Air Quality Mobile Report

In this post, I want to emphasize leveraging Azure IoT Hub for quickly implementing a real-time air quality monitoring solution. Azure IoT Hub uses device twins to simplify device management as we expect the devices to move locations in the future. Device twins consist of JSON documents that store device state information, including metadata, configurations, and conditions.

By the end of this project, you will create a real-time air quality monitoring and reporting solution. Your IoT devices will communicate with IoT Hub for device-to-cloud and cloud-to-device messaging. You will use Azure Stream Analytics, which employs a SQL-like language, to process and transform the air quality stream. Power BI will empower you to create interactive visualizations against the stream. Optionally, you can harness Cosmos DB to store the IoT JSON documents for this project.

Azure IoT Air Quality Monitoring Architecture
Air Quality IoT Architecture

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).

Wiring Diagram

Conversion for a 3 or 4 pin DHT module.

NodeMCU Adafruit CC811DHT 11
D0Data
 D1 SCL
 D2 SDA
 3v3 VINVcc
 GND GND & WakeGND
Air Quality IoT Device

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’.

http://arduino.esp8266.com/stable/package_esp8266com_index.json
Arduino IDE Preferences – ESP8266

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.

Arduino Board Manager - ESP
Arduino Board Manager – ESP

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.

Arduino Adafruit CCS811 Library
Adafruit CCS811 Library
Arduino IDE Azure IoT Libraries
Azure IoT Libraries
Arduino IDE Simple DHT Library
SimpleDHT Library
Arduino IDE JSON Library
ArduinoJSON Library

Download the Code

The code is available on GitHub.

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.

ResourceUseRequired
Azure IoT HubIoT messaging
and management
Required
Stream AnalyticsProcess IoT data streamsRequired
Cosmos DBStored IoT messages for low latency
geo-distributed access (web apps)
Optional
Power BIInteractive Visualizations using
IoT Streams
Required for
Visualizations

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.

Create Azure IoT Hub
Azure IoT Air Quality Monitoring Basis IoT Hub Settings
Set-up IoT Hub
Azure IoT Air Quality Monitoring IoT Hub Size and Scale

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.

Azure IoT Air Quality Monitoring IoT Hub gett iothubowner connection string
Copy iothubowner Connection String

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

Azure IoT Air Quality Monitoring Device Explorer
Update with iothubowner Connection String
Generate Device Tokens

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

Azure IoT Air Quality Monitoring Device Explorer 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.

No spam with envelope. Spam Email Warning. Concept of virus, piracy, hacking and security. Envelope with spam. Vector stock illustration.
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>";
/*****************************************************************/

Connect your device and upload the code from the IDE. You will need to paste the connection string for the next device before uploading to code again, such that, air1 uses air1 connection string.

Connect to IoT Device

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 IoT device from you IoT Hub using the Azure Portal.

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

Azure IoT Air Quality Monitoring IoT Hub Iot Hub Device Page

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.

Azure IoT Air Quality Monitoring Device Twin JSON

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.

Azure IoT Air Quality Monitoring New Steam Analytics Job

After successfully creating a Stream Analytics job, we will add our Input and Output data sources.

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.

Add Stream Input
Azure IoT Air Quality Monitoring Add IoT Hub Stream input
IoT Hub Streaming 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.

Add Stream Output
Azure IoT Air Quality Monitoring Power BI Output
Power BI Streaming 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.

Adding 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)
Azure IoT Air Quality Monitoring Cosmos DB Basic Settings
Cosmos DB Basic Configurations
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 New Container
Cosmos DB Output

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

Add Cosmos DB Output

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.

Start Azure Stream Analytics
Azure Stream Analytics Running

Power BI

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

Create Power BI data set

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:

Simple Air Quality Power BI Report

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:

StepDetail
Azure IoT HubOn 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 AnalyticsOn Overview screen, is Start gray? If not than
click Start
Azure Stream AnalyticsAre there any Input or Output Events being
displayed under Monitoring on the Overview
screen?

Your feedback can help ensure the best content is delivered.

Best Regards,

Jonathan

2 responses to “Real-Time Air Quality Monitoring with Azure IoT Hub”

  1. This is a great step by step process. Thank you.

    Not feedback, but an issue I saw in general of data coming from Stream to Powerbi. When fields from Stream are datetime and they are down to the millisecond (like the example you gave). I experience that Powerbi is truncating the millisecond component, and since i cant alter the model or transform data due to Stream, any work around you can think of.

  2. If i want to replace Adafruit cc811 with mq-135 then how can i do the same…. and what changes should be done in the code as well… can you help me?

Leave a Reply

Discover more from Stochastic Coder

Subscribe now to keep reading and get access to the full archive.

Continue reading