How to create your air pollution alert
By: Endika Iglesias
Tags: Alerts, Programming, Python, Grafana, Pollution

Photo by Matt Artz on Unsplash

Hi everyone, nowadays contamination is a trending topic. I’m living in Madrid and air pollution is one of the big problems in world cities. For that, I decided to create my own air pollution alert, I’m using my Grafana server (hosted in my raspberry pi) to create alerts and Python script to get pollution data.

Air pollution to be a headache for everyone attempting to solve it. In my current city Madrid, the government is trying to control this problem by limiting the use of vehicles in the city centre. But in my opinion, this is not enough. I’m not sure what is the best option to resolve that in the shortest possible time. I think to achieve a better result, people need to be made aware.

My small contribution, apart from not using the car unless strictly necessary, is create a little Python script to detect high levels of pollution.

First of all, you will need to create a new account in AirVisual to get your API key, that is necessary because this service will provide us the pollution level.

API_KEY = 'YOUR_AIRVISUAL_API_KEY'

Next step is very easy, you only need to execute a single request to the API.

import requests URL = 'http://api.airvisual.com/v2/city?city=Madrid&state=Madrid&country=Spain&key={}'.format(API_KEY) response = requests.get(URL)

I use /city endpoint to get information from the specific city. If you need more details about that endpoint or others look at the official API doc.

Great, now the value i get interested is aqius

aqius = response.json()['data']['current']['pollution']['aqius']

AQI value based on USA EPA is a standard Air quality value, on the next table you can see the different levels. With this API you can get other air quality standard value like AQI CN. Choose what you to want. On this post, I’m working with EPA.

import requests API_KEY = 'YOUR_AIRVISUAL_API_KEY'

city = 'Madrid'

state = 'Madrid'

country = 'Spain'

URL = 'http://api.airvisual.com/v2/city?city={}&state={}&country={}&key={}'.format(city, state, country, API_KEY) def get_aqius(response):

return response['data']['current']['pollution']['aqius'] aqius_level = get_aqius(requests.get(URL).json())

With this aqius_level , we’ll send to our Grafana server and configure our alerts. Sorry for omitting the rest of the steps, but Grafana has too many forms to send information to a server. I prefer to leave this part for another future post.

Why Grafana? I like that because it’s versatile and adapted for all kinds of requirements. But in your script, you can send any alert you want.

For example, this is my Gauge configuration. I created thresholds for each of the different levels

I use a graph to define my pollution alert. When the pollution is above 100, the service will send a message to my Slack.

Here is an example message from Slack.

And that’s it. I invite you to create your own alert with another kind of service for example email.

import smtplib, ssl def send_email(message):

context = ssl.create_default_context()

with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:

server.login(sender_email, password)

server.sendmail(sender_email, receiver_email, message)

I use the Grafana board a lot, in the future I will create more posts about the different sources of information to include on your different boards.

Thanks for reading, see you soon.