Creating Air Pollution Notifier with Azure Logic App and Azure Function.

Guide Vorapat
4 min readApr 13, 2019

--

*Cough*

It has been ages since t̶h̶e̶ ̶l̶a̶s̶t̶ ̶t̶i̶m̶e̶ ̶t̶h̶a̶t̶ ̶I̶ ̶w̶r̶o̶t̶e̶ ̶a̶ ̶b̶l̶o̶g̶ ̶p̶o̶s̶t̶ the air pollution in Thailand gone bad, and there’s a few signs of it getting better. The main culprit is the PM2.5, an airborne particle so small that your average face mask can’t 100% block it, which can cause many respiratory symptoms if inhaled for a certain period of time. People were advised to wear an N95 mask or a mask that can at least prevent PM2.5 from entering your nostrils.

So, how do we know that we should wear a mask? People normally use their phone to lookup the Air Quality Index(AQI) via mobile or web application which they have to do every morning so as to determine whether a mask is needed.

The AQI is actually an aggregation of “things” that could cause air pollution ingested from air pollution measurement devices across the cities, so it’s not only PM2.5.

As a(was) software engineer and a lazy person, I love to automate things. So instead of spending around 1 minute every morning to lookup the AQI from an application, I create a notifier that send an email to me if the AQI is higher than a standard threshold which take under 5 seconds for me to skim the push notification on the lock-screen.

Here’s how it works:

  • Call an API to get AQI value
  • If the value surpasses threshold, send and email

Pretty basic right? So, there’s a lot of ways to code this up. I could write a web-job that do exactly that and deployed it which means I have to write both the HTTP client call and write an email sending logic in the web app. And yes, there are tons of library for all languages you can think of for the web app to do that two tasks, but I want to abstract it out further.

Azure Logic App and Azure Function to the rescue! (no product placement intended)

Logic App is an integration service that has “connectors” to bind multiple workflows together. It requires almost-zero coding. These connectors are very diverse, they even have SAP connector!.(see: https://docs.microsoft.com/en-us/azure/logic-apps/)

Hence, I can just use Logic App designer in the portal to:

  • Call an Azure Function that has HTTP client which call AQI API.
  • Determine the AQI value using “Control connector”
  • Send an email using “Email connector”

It looks something like this:

And the output looks like this:

This is on Outlook for Windows

I personally find that the figure above clearly explains itself, apart from Function2 that should be renamed to GetAqiValue 😆. This is Azure Function. A serverless service that let you deploy just your code into it (Logic app is also serverless!). The hardware, scalability will be handled by Azure, unlike Azure App Service that you have to pick an SKU as well. You’ll also get charged per transaction not for hardware up-time.

For the code, I simply code the API call and deserialization in C# and deploy directly to Azure Funtion via Visual Studio(see: https://docs.microsoft.com/en-us/azure/azure-functions/functions-develop-vs)

You can also write the code to Azure Function directly from the portal but I find local development a bit better, because we can bundle app-dependencies and also keep it in source control which can be integrated to CI/CD pipeline as well. Not that I want to do source control and CI/CD for this notifier app though because it’s still very simple.

Quick note => Logic App also has “HTTP client connector” but I would like to do deserialization in the code because the AQI value returned from the API provider is not just the value but also other json properties.

However, as simple as this is, Logic App has its usage and should not be replacing a huge-application with complex logic. It’s just an integration tool. How and when to pick such a tool depends on many factors. In this case, this notifier app is fairly simple and I think Logic App can reduce my coding time.

Same goes to Azure Function. Serverless computation looks good, but it also depends on the architecture. The topics about serverless best practices should already been written multiple times by multiple people, but I would be happy to discuss it with you in the comment section.

There you have it. A simple to implement application that notify you to wear a mask every morning with serverless architecture using Azure Logic App and Azure Function. I will share the code soon.

--

--

Guide Vorapat

A software engineer turned cloud consultant, Microsoft Thailand. Still love coding. Also a category theory and functional programming padawan.