A recent study conducted by IQAir AirVisual and Greenpeace stated that 7 out of the top 10 most air-polluted cities are in India and 22 out top 30. It’s clear that Air pollution is a severe issue for both human health and the economy. This led me to build a system that automatically checks air quality periodically using Bolt IoT and notifies me to take precautions.
So, let’s get started.
Step 1. Hardware SetupPlug the longer end of the Buzzer to the Pin 0 of Bolt WIFI module and the shorter end to the ground pin (GND) using Male/Female Jumper Wire and connect the bolt IoT Wi-Fi module to laptop or any USB power source using USB A to micro USB cable.
1. Login to your Bolt Cloud and note down the Device ID under Devices section.
2. Now copy the API key generated under the API section, you can also generate a new API key by clicking “GENERATE NEW API KEY”.
3. Your API Key will generally look like this-
Fkfjfm8-dssfsf-ghgfsa-vfffv55-sds5oi (just for illustration)
For this project I am installing an Ubuntu on VMware, you can also dual boot Ubuntu with your windows 10.
To install Ubuntu server image on VMware, follow the below steps –
1. Go to http://releases.ubuntu.com/18.04/ and download server image as shown in below image
2. Download VMware workstation for windows from below link and install - https://www.vmware.com/in/products/workstation-pro/workstation-pro-evaluation.html
Note – Make sure you have high-speed internet
3. After installing click on create a new virtual machine and select the “Installer Disk Image file(iso)” option. Next, browse and select the above-downloaded image and click Next.
4. Fill the username and password in the next window and click next.
5. Enter 10Gb for “Maximum disk size” and select “Store virtual disk as single file”.
6. Configure the hardware if necessary and click finish.
7. Let it install and reboot.
8. Voila! You have installed Ubuntu on VMware successfully.
9. Now lets update and setup necessary packages, enter the following commands one by one
- To update the packages on Ubuntu to the latest version.
sudo apt-get -y update
- To install python3 pip3, where pip3 is a package manager for python used to install and manage packages and libraries.
sudo apt install python3-pip
- To install Bolt IoT library using pip
sudo pip3 install boltiot
Step 4. Let’s create a telegram channel and a bot1. We will be making use of the telegram channel to broadcast Air Quality alerts periodically.
2. First, create a new channel in telegram and provide a suitable name and description as shown below.
3. In the next screen set the channel as Public and also enter the channel permanent link as per your wish. Save this channel link as “telegram_chat_id”, wherein below image its “aqi_alert”.
4. Now let’s create a bot for our channel. A bot is a third-party application that runs inside the telegram, we will use this bot to send alerts to our channel.
5. On the home screen of telegram, click on the search icon on the top right corner and search for botfather.
6. In search results, select the botfather which has a blue tick mark next to it.
7. The chat shows few possible commands, type “/newbot” to create a new bot.
8. Give proper name and username to your bot.
9. Once the bot is created successfully it generates a token. This token is used to send and receive messages through python.
10. Please save this note as “telegram_bot_id” where it is saved as “bot” followed by bot token.
11. Now let’s add the bot to our channel, go to channel description and select add administrator from 3 dot menu. Choose the bot from the search field and give proper rights to the bot.
12.Congratulations! You have successfully created a channel and a bot.
1. Go to below address and fill out all required credentials and click on submit
https://aqicn.org/data-platform/token/#/
NOTE - The API permits us to use 1000 API calls per second,
However, make sure you read all the terms and conditions and provide proper attributions before usage.
2. After submitting you will need to confirm your email address to get the API token, which looks like below image
Attributions-
This API belongs to https://aqicn.org/data-platform/token/#/
CPCB - India Central Pollution Control Board - http://cpcb.nic.in/
World Air Quality Index Project - https://waqi.info/
3. Copy and save the token URL which we will use it later in our python code.
4. To see the response of this request we can use the online service https://jsonlint.com/.
5. click the token URL and copy and paste the response in https://jsonlint.com/ as shown below and click validate JSON.
1. Login to your Ubuntu server and create a folder by typing below command-
sudo mkdir aqi
2. After creating the above folder, we can enter the folder by using below command-
cd aqi
3. Next, create a new file by typing below command-
sudo nano aqi_alert.py
4. At first let us import required libraries such as JSON, requests, time and Bolt IoT library.
5. Next, we will set some of the global variables such as Bolt API key, Device ID, telegram bot and channel id which we copied and saved in earlier steps.
6. Then let us write a function to fetch AQI values of Mysore city every 15 mins.
7. You can also fetch the AQI values of your desired city by substituting your city name after feed-in below URL-
https://api.waqi.info/feed/{Enter_Your_City_Name}/?token=your_generated_token
8. Now let us write another function to send alerts to our telegram channel-
9. If there was an error in sending telegram alerts, it will print it in the console.
10. Next, we will write a function to switch on and off the buzzer for 10 seconds whenever conditions are met.
11. At last, we will write an infinite loop to check AQI values for certain thresholds every 15 minutes and send alerts through telegram and also switch ON and OFF the buzzer.
Note – Although I have set the timer to notify every 15 minutes, you can modify the time. sleep(value) to your desired interval.
12. Let’s save the code and run the python script using the below command in the terminal.
sudo python3 aqi_alert.py
Step 7. Result -
Comments
Please log in or sign up to comment.