IoT Tech Put to Good Use
The degrading quality of air around us remains one of the most serious issues of the 21st century. In this project article, Dhairya designs and builds a long-range and low-power system for monitoring air quality, using various sensors and the popular LoRaWAN communication protocol.
In recent years, air pollution has become a growing concern because of the harmful effects it has on health and our environment. The problem is prevalent particularly in outdoor urban areas because of heavy traffic and a wide array of industrial emissions. Apart from this, indoor air pollution is also of great concern in cities. According to research, the air inside our homes is even more polluted than outdoor air. Many countries have already taken preventive measures and set up various monitoring systems for air and weather quality in their big cities. These systems provide the data that enable necessary steps to be taken to prevent the adverse effects of air pollution.
All that said, there are several problems with existing systems. First, they use primitive methods that involve collecting air samples and having them tested in a laboratory. While this approach is reliable, it is highly ineffective and requires very expensive laboratory equipment. Second, most such testing focuses on outdoor air and doesn’t address the issue of indoor air pollution. And indoor air pollution has harmful effects on the health of increasing numbers of individuals every year. With all that in mind, it is of the utmost importance to develop an advanced system that can overcome those drawbacks.
This project presents a real-time and long-range system for monitoring air pollution in both indoor and outdoor environments. The system implements a wireless sensor network using LoRaWAN (long-range, wide-area networking protocol) technology for data communication among all nodes and sensors. The system consists of two nodes distributed within 1km distance to a gateway for measuring the concentrations of VOCs (volatile organic compounds), carbon dioxide (CO2) and PM2.5 (particulate matter 2.5µm).
Experimental results show the system is reliable in both indoor and outdoor applications. The distance coverage achieved was up to 900 meters and results can be displayed through a web-based client (Thingspeak) or from an Android or iOS app. My experiments with LoRaWAN transmission showed that LoRaWAN technology is well suited for the air pollution system, especially in long-range transmission. In addition, the power consumption of the system is considerably lower than that of existing systems.
HARDWARE COMPONENTS
Figure 1 shows all the hardware components we will be using for the project. Let’s look at each one.
LG01-N single channel LoRa gateway: This is the main gateway used in the project. It receives all the sensor data from the nodes and funnels these data to different sources using Integrations for visualization using Internet access. According to the manufacturer (Dragino Technology), LG01-N is an open source, single channel LoRa Gateway. It lets you bridge LoRa a wireless network to an IP network via Wi-Fi, Ethernet or 3G/4G cellular via optional LTE module. The LoRa wireless allows users to send data and reach extremely long ranges at low data-rates. It provides ultra-long range spread spectrum communication and high interference immunity. The LG01-N can connect to the Internet via Wi-Fi interface, Ethernet port or USB host port.
— ADVERTISMENT—
—Advertise Here—
Dragino LoRa shield for Arduino v1.4 (EU868): Also from Dragino, this is an Arduino shield that adds LoRa capability to our Arduino board. It is based on an open-source library, which makes programming it a lot easier. The shield allows the user to send data and reach extremely long ranges at low data-rates. It provides ultra-long range spread spectrum communication and high interference immunity while minimizing current consumption. It is based on the Semtech SX1276 chip and in practical applications, the range of this product is between 800-1000 meters, which is pretty impressive.
Arduino Uno board: This board is the brains of our sensor node. It will be connected to a LoRa shield and all the sensors will be interfaced to it. Sensor interfacing is covered in a later section of the article.
CCS811 gas sensor [1]: The CCS811 from ams is an ultra-low power digital gas sensor solution which integrates a metal oxide gas sensor to detect a wide range of volatile organic compounds (VOCs) for indoor air quality monitoring with a microcontroller (MCU). The MCU includes an analog-to-digital converter (ADC) and an I²C interface. CCS811 is based on ams’ micro-hotplate technology which enables a highly reliable solution for gas sensors, very fast cycle times and a significant reduction in average power consumption. CCS811 supports intelligent algorithms to process raw sensor measurements to output a TVOC (total VOC) value or equivalent CO2 (usually expressed as “eCO2”) levels. It also supports multiple modes based on frequency and power consumption the application requires.
The CCS811 has 5 modes of operation as follows: Mode 0: Idle, low current mode; Mode 1: Constant power mode, IAQ measurement every second; Mode 2: Pulse heating mode IAQ measurement every 10 seconds; Mode 3: Low power pulse heating mode IAQ measurement every 60 seconds; and Mode 4: Constant power mode, sensor measurement every 250ms.
To use the sensor module for accurate readings, we need to “burn” in the sensor which is achieved by changing its resistance which is achieved by letting it run for 48 hours continuously after it’s taken out of the box. The sensor measures two values, the eCO2 which has an output range of 400-8192ppm (parts per million) and the TVOC which has an output range of 0-1187ppb (parts per billion). It also supports temperature and humidity compensation.
MICS–5524 gas sensor module [2]: The MICS-5524 is a MEMS sensor for indoor carbon monoxide (CO) and natural gas leakage detection. It’s suitable also for indoor air quality monitoring, breath checking and early fire detection. This sensor is sensitive to CO (approximately 1 to 1000ppm), ammonia (approximately 1 to 500ppm), ethanol (approximately 10 to 500ppm), hydrogen (H2) (approximately 1 – 1000ppm) and methane/propane/isobutane (approximately 1,000+ ppm). However, the sensor can’t tell you which gas it has detected. The sensors can be cleaned by exposing them for a minimum 24 hours in clear air.
DSM501A PM2.5 dust sensor module [3]: The DSM501 series detects the level of indoor airborne dust, particles and pollen. It is well suited for use in a stand-alone air cleaner or IAQ monitor. PWM (pulse-width modulation) output of the sensor can be directly connected to µ-com to control fan speed or ventilation and also to display the level of particles. Detectable size of particles is limited to minimum 0.7µm, and its application for use in smoke or fire alarms is prohibited.
The dust sensor module DSM501 is a compact-sized particle density sensor. Quantitative particle density measurement is done using the principle of particle counting—fine particles of bigger than one micron could be detected with high sensitivity. An internal heater induces air inflow to the module and one control contact and two output contacts. Note that the Figure 1 images does not show he DSM501 but rather the Sharp GP2Y1010AU0F Optical Dust Sensor [4]. You can use either for the project.
Raspberry Pi SBC: For this project, we will be using the Raspberry Pi SBC as an end user device. It will be part of a hardware system which will be installed at the user’s house, allowing them to monitor the values of different nodes on a dashboard that will be hosted on the Raspberry Pi. The dashboard will be available on a locally hosted Node RED web-board with the necessary UI and all the information required. The Raspberry Pi can also be used to add the functionality of machine learning in this project to predict future values or to predict how badly polluted the surrounding are is. This will be discussed in detail at the end of this article in the Conclusions section.
— ADVERTISMENT—
—Advertise Here—
These are all the components we will require for the project. We will enclose the sensor node within a temporary cardboard enclosure and the node will be powered using a portable battery bank. Figure 2 shows the final node.
SOFTWARE COMPONENTS
Now let’s look at each of the software components we will be using for this project. Code for this project is available on GitHub [9]. And reference links and other resources are available in RESOURCES at the end of this article.. Later in the article, we will learn how the Arduino code works in a step-by-step explanation. Here are the software components:
The Things Network: The Things Network is an open source fully decentralized data network that allows “things” (nodes) to connect to the Internet without the use of any 4G LTE or Wi-Fi. It is based on the popular LoRaWAN technology. Our gateway will send the received node data to this platform only. The rest of the platforms receiving the sensor data will be through The Things Network using the Integrations that are provided by the site.
ThingSpeak: ThingSpeak is an IoT analytics platform service that enables people to aggregate, visualize and analyze live data streams in the cloud. We use this platform in our project because we can easily integrate it with the Things Network platform and also because it supports data sharing using simple HTTP URLs, which is the protocol we will be using to fetch the sensor data for our Raspberry Pi Dashboard. This is done using simple Read and Write API Keys.
Arduino IDE: We will use the open source Arduino IDE to program our Arduino LoRa sensor node. The program will involve the following functions: fetching sensor data, stacking data in byte format (pre preparation to send this data wirelessly), setup LoRa communication with the gateway and send data to the gateway.
Raspbian OS: This is the operating system that will be flashed onto the Raspberry Pi. With this, we will get native support of Node RED which will be used to create our dashboard using simple drag and drop blocks.
SYSTEM WORKFLOW
Before diving into the implementation of the project, I want to discuss how this system will work using the main components that are a part of this system. For that, take a look at Figure 3 and Figure 4 which show the diagrams of the LoRa Node and the LoRa Gateway. With those diagrams in mind, we can see how the whole system works in a step-by-step workflow as follows:
1. Node data acquisition: First, each node is battery powered. They have three air quality sensors attached to the Arduino I/O ports—two to digital pins and one to an analog pin. Each sensor sends the measured data to the Arduino every 10-20 seconds.
2. Sensor data transmission: Once the data has been received by the Arduino’s MCU, it is then pre-processed (converted into byte format so it can be easily transmitted). Next, this processed data is sent via the LoRa shield at a specific frequency of 868.3MHz.
3. Data reception: Now that the data has been transmitted, it will be received by the gateway within a specific amount of time, depending on the distance between the node and the gateway. This received data can be viewed on the Things Network Page for that particular node. Note that there are some additional settings that need to be applied to get the data in proper format. These settings will be discussed later.
4. Thingspeak dashboard update: After receiving the data, we’ve setup our things network such that it will funnel all its received data to Thingspeak IoT dashboard. There, we can view any of the changes in the sensor data in real time and even create GUIs for better understanding.
5. Raspberry Pi dashboard update: From Thingspeak, we fetch the sensor data and update our Home dashboard running on the Pi locally.
SENSOR INTERFACING
Now that we’ve covered how the project will work, let’s move on to setting up the node. This includes adding the LoRa Arduino shield and interfacing to the three air quality sensors. Figure 5 shows how to interface the sensors to your Arduino board.
I would suggest that, rather than interfacing all the sensors at once, interface them one by one and test them using the example code for each sensor. (The code is available on GitHub [9])
Note that each sensor is connected differently to the Arduino:
• The CCS-811 is an I2C sensor, meaning that it will connect to the SCL and SDA pins of the Arduino, which are multiplexed with the A5 and A4 pins of the board respectively. There are two I2C addresses used for this sensor: 0x5A and 0x5B (these can differ according to the company which produces these sensors). You can check the code of your sensor by connecting the sensor and uploading a simple i2cdetect code to your Uno.
• The MICS sensor gives a simple analog output, so it is directly connected to the A0 pin of the Arduino Uno.
• The dust sensor has two digital outputs, one for PM2.5 reading and the other for PM1.0 reading. We will be using the PM2.5 readings for this project, so we will connect that to pin D2 of the Arduino board.
Of the three sensors, only the CCS811 requires an external library, so we don’t need to program the I2C signals manually. We will be using the CCS-811 Library from Sparkfun for our project [1]. This concludes the sensor interfacing. Now, we’re ready to move on to programming our node. In the next section, we will discuss the final code for our LoRa Sensor node in detail.
LoRa SETUP
In this section, we’ll talk about your The Things Network (TTN) account setup, the Gateway setup and the Node setup. After these setups, you will be able to send the “Hello World!” string via LoRa from your Node to your TTN Gateway and Device on an interval of 30 seconds to 1 minute. Be aware that this is a brief explanation of the setup process and a document with detailed step-by-step instructions is provided at [5].
TTN account setup: We will be using TTN to connect to our Gateway and obtain the sensor data it receives. The setup process for this is very easy, so let’s get started. First, go to www.thethingsnetwork.org [6] and create a new account by signing up. Now do the necessary verifications and then login to your account.
— ADVERTISMENT—
—Advertise Here—
After that go to the TTN Console and go to the Gateway tab and register a new Gateway. Fill all the requested details in the form—note the Gateway ID that you enter and then click on Register Gateway. Now our physical gateway will route all the received data and we will be able to see the raw data from the Traffic tab. We also need to add an application because we will require its credentials when we are programming our node.
While adding the application, we need to take a note of a couple of things. We have to change the Activation method to ABP because we will be using the Arduino Library created specifically for this method. In this way, we will get two 16-bit keys and a device ID that we need to store somewhere. They will be used in our Arduino code. Note that the device ID is unique for each application and we can set it. After this setup, we can now move to the next step, which is setting up our gateway device.
Gateway setup: First, we need to download the appropriate firmware for our gateway. The gateway we are using is Dragino’s LG 01N single channel LoRa gateway based on the SX1276 LoRa chip. Different firmware versions are available according to your use case. But the firmware we will be using is already the default out-of-the-box firmware that the gateway comes with. So, we will just download the latest version of that firmware and burn it into our gateway. Note that you will need the firmware version LG02_LG08-5.2.15xxxxxxxx for this project. If you have any other firmware, just go to System > Backup/Flash Firmware and reflash the desired firmware. You can download the firmware from the link provided at [7].
Now, we will connect our gateway to the Internet, so we can connect to the various IoT platforms we are going to use in this project. For connecting to the Internet (specifically Wi Fi), just go to the following from the menu: Network > Wireless. After that, follow these steps:
1. First, disable the block with SSID name dragino-xxxx so that we can use that to connect to our own Wi-Fi network.
2. On top you will find a block saying Generic MAC80211 802.11bgn. In that block, press the Scan button.
3. Connect to your Wi-Fi network and enter your password in the field “WPA Passphrase”. After that, press the Submit button.
4. Next, you’ll be taken to the next page where won’t have to change anything. Just go to the bottom of the page and press the Save and Apply button. After pressing that, you will see on the screen something like “Applying Configuration, wait for xx seconds.” After those xx seconds, the new config will be applied. That’s it! You’ve successfully connected to the Internet.
Now that Wi-Fi is connected, we can go on and setup the LoRaWAN gateway part. For that, go to Service > LoRaWAN Gateway. Then, refer to Figure 6 and make the changes shown there accordingly. Save these changes and your gateway is ready to go. Note that you have to enter your own gateway address—the one that you obtained while setting up your TTN Account in the previous step. This completes our gateway setup. Now we will move to the setup of our node followed by a brief explanation of the final code we will be using as well as the Decode function, which will be discussed in the next section.
(Click to enlarge)
Node setup: To setup the node, we simply need to connect our Dragino Shield to the Arduino UNO board and the hardware part is done. Next, we’ll need to download some of the libraries required for the Arduino code that we will be writing for our node. For now, we just want to send the standard “Hello World” text to our gateway, so just follow these steps:
1. Download and install the latest “stable” version of Arduino IDE. I would suggest you download the 2nd latest version—I’ve often faced issues when using the latest version.
2. After that, install the Arduino LMIC library so that we can do the necessary LoRa configurations. The link to the library is provided at [8].
3. You can get all the code used in this article from the GitHub repository link [9] provided in RESOURCES at the end of this article. Download the code and add NWKSKEY, APPSKEY and DEVADDR that we obtained setting up our application in the TTN setup.
4. Connect the Arduino to your PC, select the correct port and upload this code. If everything goes well, you will see data traffic on TTN’s Gateway Traffic page in hex character format as shown in the (Figure 7).
(Click to enlarge)
To view the String data, we need to set the Decoder function which handles all the traffic coming through. As the name suggests, it decodes the hex data to the form it is programmed for. The final decoder function for this project is also available on GitHub [9], so you can just download the code files and copy paste this function.
You have successfully connected your LoRa components to the LoRaWAN network! Next, we will get into a brief explanation of the final code that sends the air quality sensor data to our gateway, which sends it to the Things Network, which in turn sends it to a dashboard.
FINAL IMPLEMENTATION
After following the steps so far, the only remaining tasks are to connect our air quality sensors to the node, upload the final project code and you are good to go. That said, there are some more steps which need to be performed. As instructed in the earlier sensor interfacing step, you’ve already obtained all the required libraries installed for this project. We also need to make some changes to our TTN console. We want to add a Thingspeak Integration because it will be used to send the raw sensor data to the Thingspeak dashboard where we can visualize it. We’ll also use it to send the sensor data to our local dashboard running on the Pi using the REST API links provided by Thingspeak. The steps to add an integration are as follows:
1. Go to the Applications page from the console of the Things Network
2. Open your application so that you can see several functions on the top right side of the page. Go to the Integrations tab and click on the Add Integration button.
3. After that, you can see a number of supported integrations on the next page. Now select the one which we will be using: Thingspeak. You will find it at the bottom of the list and the version number is v2.7.14
4. After clicking on that, you will see three fields that need to be filled: Process ID, Authorization and Channel ID. Just find that info from your Thingspeak channel that you will be using and then press the Add Integration button on the bottom of the page.
You have successfully connected the Things Network application with your Thingspeak IoT channel. The next step is the Thingspeak IoT channel part. After starting Thingspeak, sign up and create a free account. After verifying your email that you used to create your account, you are now ready to use the Thingspeak channel. Create fields according to your needs. Graphs will then be automatically created on the dashboard for you. Actually, this is all that is initially required for the Thingspeak setup. We will require the following information on the channel we are using: Thingspeak channel ID, Read API Key, Write API Key and Authorization (for TTN). After setting up everything, we are ready to test our system. The dashboard of Thingspeak will look as shown in the Figure 8.
(Click to enlarge)
DASHBOARD VISUALIZATION
This is the last part of our project. We will now create a very nice looking and visual dashboard with practically no coding! To do that we will use the popular Node RED package which runs JavaScript on the backend. For the hardware, we will use the Raspberry Pi model 3B SBC, but you can use any version of the Pi—it even works with the Pi Zero.
The only setup required is to install the latest Raspbian OS on your Pi (for me, that was Raspbian Buster) and that’s it! The OS comes with Node RED pre-installed. Just go to the software section and click on Node RED to open a terminal. That will start Node Red as a local server and give you the IP address where you can access it. After it opens, just go to the options and go to the Palette. Install the Node RED Dashboard Package using the provided UI and you are good to go!
Next, we will create a flow that will fetch the sensor data from Thingspeak using its REST API and display it on the Node RED Dashboard. The final completed flow is shown in (Figure 9). Just copy this and deploy the flow. View the final dashboard in a browser at http://(Node RED IP address here):1800/ui
(Click to enlarge)
Set the flow components as follows:
Make Request: Thingspeak Field No. (1,2,3 or 4).json?results=1
Set msg.query: SET msg.query to msg.payload
Set msg.payload: SET msg.payload to msg.payloads.feeds[0].feild1 (setting payload as the sensor data value)
Set msg.payload: SET msg.payload to $number(payload) (Convert the string value to number)
UI Components: You can use any UI components you like. I’ve used A Gauge and Line chart. They simply show your sensor data (msg.payload) in graphical format. You can get the components easily after you install the Dashboard package.
Extent of Value: I used an LED indicator to show how bad the value is with reference to the acceptable values. For that, I created a function which places the value in a range and returns a flag which helps us determine what is the extent and hence change the color of LED accordingly.
The final dashboard is shown in (Figure 10). The dashboard will have gauges to view the real time data and also an indicator that tells you how bad the received values are with respect to a reference scale. I created the reference scale after performing some experiments and some research. You can import my node flow easily by using the file provided on GitHub [9].
(Click to enlarge)
This completes our project. We now have a running air quality monitoring system that communicates through the LoRaWAN protocol. We also have a local dashboard that we can use to view the real time change in the data.
CONCLUSION
The environmental pollution monitoring system in this article overcomes many of the disadvantages of traditional pollution measurement systems that rely on wireless transmission technologies with no more than 100 meters of coverage—such as ZigBee and Wi-Fi. My proposed system is a low-cost solution and it does not require any phone/data bills (as a cellular-based system would). It features small size, long coverage and real-time transmitting.
Based on the practical experiments in indoor and outdoor environments, the system has proven its ability to detect pollution and classify pollution levels. The experiment with LoRa transmission has shown that LoRa technology is very suitable for the air pollution system, especially in long-range transmission, compared to other wireless transmission techniques. Meanwhile, the dashboard makes visualization of the real time data very easy. The power consumption of the system is much less compared to other systems and it can easily run on battery power for several days.
With air pollution becoming such a serious concern, there is a lot of potential for this project. Here are the two aspects that I plan to improve in the future:
Cost: The current cost of LoRa components is too high. My plan it to reduce those cost by using modules that are less expensive, but which will require a little more expertise in programming them.
Adding Machine Learning: I intend to add AI capability to this project. Using AI, I would like to create time series prediction models to predict the future values of air quality on the basis of previously obtained data.
RESOURCES
References:
[1] CCS811 – https://cdn.sparkfun.com/assets/learn_tutorials/1/4/3/CCS811_Datasheet-DS000459.pdf
[2] Mics Sensor – https://cdn-shop.adafruit.com/product-files/3199/MiCS-5524.pdf
[3] DSM501 Dust Sensor – https://i.publiclab.org/system/images/photos/000/003/726/original/tmp_DSM501A_Dust_Sensor630081629.pdf
[4] Sharp Dust Sensor – https://www.sparkfun.com/datasheets/Sensors/gp2y1010au_e.pdf Sensor Info: https://github.com/sharpsensoruser/sharp-sensor-demos/wiki/Application-Guide-for-Sharp-GP2Y1014AU0F-Dust-Sensor
[5] Detailed Document for Lora Component Setup:https://drive.google.com/file/d/1dKPxLvezmhzUscJEdCyDBjxhEOhIz8v3/view?usp=sharing
[6] Go to www.thethingsnetwork.org and create a new account by signing up. Now do the necessary verifications and then login to your account.
[7]. http://www.dragino.com/downloads/index.php?dir=LoRa_Gateway/LG01N/Firmware/Test_Firmware
[8]. https://github.com/matthijskooijman/arduino-lmic
[9] Project GitHub Repo:
https://github.com/Dhairya1007/Lora-Powered-Air-Quality-Monitoring-SystemThis source contains:1. Node Source Code2. Sensor Test codes3. Decoder Function4. Node Red Flow JSON file
More resources:
1. Sujuan Liu, Chuyu Xia and Zhenzhen Zhao, “A low-power real-time air quality monitoring system using LPWAN based on LoRa,”.
2. Evaluation of LoRa-based Air Pollution Monitoring System Nael Abd Alfatah Husein, Abdul Hadi Abd Rahman, Dahlila Putri Dahnil
3. IoT deployment for city scale air quality monitoring with Low-Power Wide Area Networks Steven J. Johnston Philip J. Basford Florentin M. J. Bulot Mihaela Apetroaie-Cristea Gavin L Foster Matthew Loxham Simon J. Cox.
4. Ma Y, Zhao L, Yang R, et al. Development and Application of an Atmospheric Pollutant Monitoring System Based on LoRa-Part I: Design and Reliability Tests. Sensors (Basel). 2018;18(11):3891. Published 2018 Nov 12. https://doi.org/doi:10.3390/s18113891
5. Thu, Ye & Htun, Wunna & Aung, Yan & Ei, Pyone & Tun, Nay. (2018). Smart Air Quality Monitoring System with LoRaWAN. https://doi.org/10-15. 10.1109/IOTAIS.2018.8600904
6. Johnston SJ, Basford PJ, Bulot FMJ, et al. City Scale Particulate Matter Monitoring Using LoRaWAN Based Air Quality IoT Devices. Sensors (Basel). 2019;19(1):209. Published 2019 Jan 8. https://doi.org/10.3390/s19010209
7. Knoll, M., Breitegger, P. & Bergmann, A. Low-Power Wide-Area technologies as building block for smart sensors in air quality measurements. Elektrotech. Inftech. 135, 416–422 (2018). https://doi.org/10.1007/s00502-018-0639-y
8. https://www.thethingsnetwork.org/docs
9. Dragino Documentation for LoRa Gateway and Node setup: https://wiki.dragino.com
10. Thingspeak IoT Documentation: https://in.mathworks.com/help/thingspeak
Adafruit | www.adafruit.com
ams | www.ams.com
Dragino Technology | www.dragino.com
Mathworks | www.mathworks.com
Raspberry Pi Foundation | www.raspberrypi.org
Sparkfun | www.sparkfun.com
The Things Network | www.thethingsnetwork.org
ThingSpeak | www.thingspeak.com
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JANUARY 2021 #366 – Get a PDF of the issue
Sponsor this ArticleDhairya Parikh is an Electronics Engineer and an avid project developer. Dhairya makes projects that can bring a positive impact in a person’s life by making it easier. He is currently working as an IoT engineer. His projects are mainly related to IoT and machine learning. Dhairya can be contacted on dhairyaparikh1998@gmail.com