如何使用树莓派测定颗粒物（PM 2.5）
空气 树莓派

> 
> 使用两个简单的硬件设备和几行代码构建一个空气质量探测器。
> 
> 
> 


![](/data/attachment/album/201805/08/235157zbhruvv0zms8kmor.png)


我们在东南亚的学校定期测定空气中的颗粒物。这里的测定值非常高，尤其是在二到五月之间，干燥炎热、土地干旱等各种因素都对空气质量产生了不利的影响。我将会在这篇文章中展示如何使用树莓派来测定颗粒物。


### 什么是颗粒物？


颗粒物就是粉尘或者空气中的微小颗粒。其中 PM10 和 PM2.5 之间的差别就是 PM10 指的是粒径小于 10 微米的颗粒，而 PM2.5 指的是粒径小于 2.5 微米的颗粒。在粒径小于 2.5 微米的的情况下，由于它们能被吸入肺泡中并且对呼吸系统造成影响，因此颗粒越小，对人的健康危害越大。


世界卫生组织的建议[颗粒物浓度](https://en.wikipedia.org/wiki/Particulates)是：


* 年均 PM10 不高于 20 µg/m³
* 年均 PM2.5 不高于 10 µg/m³
* 不允许超标时，日均 PM10 不高于 50 µg/m³
* 不允许超标时，日均 PM2.5 不高于 25 µg/m³


以上数值实际上是低于大多数国家的标准的，例如欧盟对于 PM10 所允许的年均值是不高于 40 µg/m³。


### 什么是<ruby> 空气质量指数 <rt>  Air Quality Index </rt></ruby>（AQI）？


空气质量指数是按照颗粒物的测定值来评价空气质量的好坏，然而由于各国之间的计算方式有所不同，这个指数并没有统一的标准。维基百科上关于[空气质量指数](https://en.wikipedia.org/wiki/Air_quality_index)的词条对此给出了一个概述。我们学校则以<ruby> <a href="https://en.wikipedia.org/wiki/United_States_Environmental_Protection_Agency">  美国环境保护协会 </a> <rt>  Environment Protection Agency </rt></ruby>（EPA）建立的分类法来作为依据。


![空气质量指数](/data/attachment/album/201805/08/235157sb3bhid8vti9wbmv.png "Air quality index")


*空气质量指数*


### 测定颗粒物需要哪些准备？


测定颗粒物只需要以下两种器材：


* 树莓派（款式不限，最好带有 WiFi）
* SDS011 颗粒物传感器


![颗粒物传感器](/data/attachment/album/201805/08/235158jqg9lzx8zhksq30b.jpg "Particulate sensor")


*颗粒物传感器*


如果是只带有 Micro USB 的树莓派 Zero W，那还需要一根连接到标准 USB 端口的适配线，只需要 20 美元，而传感器则自带适配串行接口的 USB 适配器。


### 安装过程


对于树莓派，只需要下载对应的 Raspbian Lite 镜像并且[写入到 Micro SD 卡](https://www.raspberrypi.org/documentation/installation/installing-images/README.md)上就可以了（网上很多教程都有介绍如何设置 WLAN 连接，我就不细说了）。


如果要使用 SSH，那还需要在启动分区建立一个名为 `ssh` 的空文件。树莓派的 IP 通过路由器或者 DHCP 服务器获取，随后就可以通过 SSH 登录到树莓派了（默认密码是 raspberry）：



```
$ ssh pi@192.168.1.5

```

首先我们需要在树莓派上安装一下这些包：



```
$ sudo apt install git-core python-serial python-enum lighttpd

```

在开始之前，我们可以用 `dmesg` 来获取 USB 适配器连接的串行接口：



```
$ dmesg
[ 5.559802] usbcore: registered new interface driver usbserial
[ 5.559930] usbcore: registered new interface driver usbserial_generic
[ 5.560049] usbserial: USB Serial support registered for generic
[ 5.569938] usbcore: registered new interface driver ch341
[ 5.570079] usbserial: USB Serial support registered for ch341-uart
[ 5.570217] ch341 1–1.4:1.0: ch341-uart converter detected
[ 5.575686] usb 1–1.4: ch341-uart converter now attached to ttyUSB0

```

在最后一行，可以看到接口 `ttyUSB0`。然后我们需要写一个 Python 脚本来读取传感器的数据并以 JSON 格式存储，在通过一个 HTML 页面就可以把数据展示出来了。


### 在树莓派上读取数据


首先创建一个传感器实例，每 5 分钟读取一次传感器的数据，持续 30 秒，这些数值后续都可以调整。在每两次测定的间隔，我们把传感器调到睡眠模式以延长它的使用寿命（厂商认为元件的寿命大约 8000 小时）。


我们可以使用以下命令来下载 Python 脚本：



```
$ wget -O /home/pi/aqi.py https://raw.githubusercontent.com/zefanja/aqi/master/python/aqi.py

```

另外还需要执行以下两条命令来保证脚本正常运行：



```
$ sudo chown pi:pi /var/www/html/
$ echo '[]' > /var/www/html/aqi.json

```

下面就可以执行脚本了：



```
$ chmod +x aqi.p
$ ./aqi.py
PM2.5:55.3, PM10:47.5
PM2.5:55.5, PM10:47.7
PM2.5:55.7, PM10:47.8
PM2.5:53.9, PM10:47.6
PM2.5:53.6, PM10:47.4
PM2.5:54.2, PM10:47.3
…

```

### 自动化执行脚本


只需要使用诸如 crontab 的服务，我们就不需要每次都手动启动脚本了。按照以下命令打开 crontab 文件：



```
$ crontab -e

```

在文件末尾添加这一行：



```
@reboot cd /home/pi/ && ./aqi.py

```

现在我们的脚本就会在树莓派每次重启后自动执行了。


### 展示颗粒物测定值和空气质量指数的 HTML 页面


我们在前面已经安装了一个轻量级的 web 服务器 `lighttpd`，所以我们需要把 HTML、JavaScript、CSS 文件放置在 `/var/www/html` 目录中，这样就能通过电脑和智能手机访问到相关数据了。执行下面的三条命令，可以下载到对应的文件：



```
$ wget -O /var/www/html/index.html https://raw.githubusercontent.com/zefanja/aqi/master/html/index.html
$ wget -O /var/www/html/aqi.js https://raw.githubusercontent.com/zefanja/aqi/master/html/aqi.js
$ wget -O /var/www/html/style.css https://raw.githubusercontent.com/zefanja/aqi/master/html/style.css

```

在 JavaScript 文件中，实现了打开 JSON 文件、提取数据、计算空气质量指数的过程，随后页面的背景颜色将会根据 EPA 的划分标准而变化。


你只需要用浏览器访问树莓派的地址，就可以看到当前颗粒物浓度值等数据了： [http://192.168.1.5:](http://192.168.1.5/)


这个页面比较简单而且可扩展，比如可以添加一个展示过去数小时历史数据的表格等等。


这是[Github上的完整源代码](https://github.com/zefanja/aqi)。


### 总结


在资金相对紧张的情况下，树莓派是一种选择。除此以外，还有很多可以用来测定颗粒物的应用，包括室外固定装置、移动测定设备等等。我们学校则同时采用了这两种：固定装置在室外测定全天颗粒物浓度，而移动测定设备在室内检测空调过滤器的效果。


[Luftdaten.info](http://luftdaten.info/) 提供了一个如何设计类似的传感器的介绍，其中的软件效果出众，而且因为它没有使用树莓派，所以硬件更是小巧。


对于学生来说，设计一个颗粒物传感器确实算得上是一个优秀的课外项目。


你又打算如何使用你的[树莓派](https://openschoolsolutions.org/shutdown-servers-case-power-failure%e2%80%8a-%e2%80%8aups-nut-co/)呢？




---


via: <https://opensource.com/article/18/3/how-measure-particulate-matter-raspberry-pi>


作者：[Stephan Tetzel](https://opensource.com/users/stephan) 译者：[HankChow](https://github.com/HankChow) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
We regularly measure particulate matter in the air at our school in Southeast Asia. The values here are very high, particularly between February and May, when weather conditions are very dry and hot, and many fields burn. These factors negatively affect the quality of the air. In this article, I will show you how to measure particulate matter using a Raspberry Pi.

## What is particulate matter?


Particulate matter is fine dust or very small particles in the air. A distinction is made between PM10 and PM2.5: PM10 refers to particles that are smaller than 10µm; PM2.5 refers to particles that are smaller than 2.5µm. The smaller the particles—i.e., anything smaller than 2.5µm—the more dangerous they are to one's health, as they can penetrate into the alveoli and impact the respiratory system.

The World Health Organization recommends [limiting particulate matter](https://en.wikipedia.org/wiki/Particulates) to the following values:

- Annual average PM10 20 µg/m³
- Annual average PM2,5 10 µg/m³ per year
- Daily average PM10 50 µg/m³ without permitted days on which exceeding is possible.
- Daily average PM2,5 25 µg/m³ without permitted days on which exceeding is possible.

These values are below the limits set in most countries. In the European Union, an annual average of 40 µg/m³ for PM10 is allowed.

## What is the Air Quality Index (AQI)?

The Air Quality Index indicates how “good” or “bad” air is based on its particulate measurement. Unfortunately, there is no uniform standard for AQI because not all countries calculate it the same way. The Wikipedia article on the [Air Quality Index](https://en.wikipedia.org/wiki/Air_quality_index) offers a helpful overview. At our school, we are guided by the classification established by the United States' [ Environmental Protection Agency](https://en.wikipedia.org/wiki/United_States_Environmental_Protection_Agency).

![Air quality index Air quality index](https://opensource.com/sites/default/files/images/life-uploads/air_quality_index.png)

Air quality index

## What do we need to measure particulate matter?

Measuring particulate matter requires only two things:

- A Raspberry Pi (every model works; a model with WiFi is best)
- A particulates sensor SDS011

![Particulate sensor Particulate sensor](https://opensource.com/sites/default/files/images/life-uploads/particulate_sensor.jpg)

Particulate sensor

If you are using a Raspberry Pi Zero W, you will also need an adapter cable to a standard USB port because the Zero has only a Micro USB. These are available for about $20. The sensor comes with a USB adapter for the serial interface.

## Installation

For our Raspberry Pi we download the corresponding Raspbian Lite Image and [write it on the Micro SD card](https://www.raspberrypi.org/documentation/installation/installing-images/README.md). (I will not go into the details of setting up the WLAN connection; many tutorials are available online).

If you want to have SSH enabled after booting, you need to create an empty file named `ssh`

in the boot partition. The IP of the Raspberry Pi can best be obtained via your own router/DHCP server. You can then log in via SSH (the default password is *raspberry*):

`$ ssh pi@192.168.1.5`


First we need to install some packages on the Pi:

`$ sudo apt install git-core python-serial python-enum lighttpd`


Before we can start, we need to know which serial port the USB adapter is connected to. `dmesg`

helps us:

```
$ dmesg
[ 5.559802] usbcore: registered new interface driver usbserial
[ 5.559930] usbcore: registered new interface driver usbserial_generic
[ 5.560049] usbserial: USB Serial support registered for generic
[ 5.569938] usbcore: registered new interface driver ch341
[ 5.570079] usbserial: USB Serial support registered for ch341-uart
[ 5.570217] ch341 1–1.4:1.0: ch341-uart converter detected
[ 5.575686] usb 1–1.4: ch341-uart converter now attached to ttyUSB0
```


In the last line, you can see our interface: `ttyUSB0`

. We now need a small Python script that reads the data and saves it in a JSON file, and then we will create a small HTML page that reads and displays the data.

## Reading data on the Raspberry Pi

We first create an instance of the sensor and then read the sensor every 5 minutes, for 30 seconds. These values can, of course, be adjusted. Between the measuring intervals, we put the sensor into a sleep mode to increase its lifespan (according to the manufacturer, the lifespan totals approximately 8000 hours).

We can download the script with this command:

`$ wget -O /home/pi/aqi.py https://raw.githubusercontent.com/zefanja/aqi/master/python/aqi.py`


For the script to run without errors, two small things are still needed:

```
$ sudo chown pi:pi /var/www/html/
$ echo [] > /var/www/html/aqi.json
```


Now you can start the script:

```
$ chmod +x aqi.py
$ ./aqi.py
PM2.5:55.3, PM10:47.5
PM2.5:55.5, PM10:47.7
PM2.5:55.7, PM10:47.8
PM2.5:53.9, PM10:47.6
PM2.5:53.6, PM10:47.4
PM2.5:54.2, PM10:47.3
…
```


## Run the script automatically

So that we don’t have to start the script manually every time, we can let it start with a cronjob, e.g., with every restart of the Raspberry Pi. To do this, open the crontab file:

`$ crontab -e`


and add the following line at the end:

`@reboot cd /home/pi/ && ./aqi.py`


Now our script starts automatically with every restart.

## HTML page for displaying measured values and AQI

We have already installed a lightweight webserver, `lighttpd`

. So we need to save our HTML, JavaScript, and CSS files in the directory `/var/www/html/`

so that we can access the data from another computer or smartphone. With the next three commands, we simply download the corresponding files:

```
$ wget -O /var/www/html/index.html https://raw.githubusercontent.com/zefanja/aqi/master/html/index.html
$ wget -O /var/www/html/aqi.js https://raw.githubusercontent.com/zefanja/aqi/master/html/aqi.js
$ wget -O /var/www/html/style.css https://raw.githubusercontent.com/zefanja/aqi/master/html/style.css
```


The main work is done in the JavaScript file, which opens our JSON file, takes the last value, and calculates the AQI based on this value. Then the background colors are adjusted according to the scale of the EPA.

Now you simply open the address of the Raspberry Pi in your browser and look at the current particulates values, e.g., [http://192.168.1.5:](http://192.168.1.5/)

![AQI AQI](https://opensource.com/sites/default/files/images/life-uploads/aqi.png)

The page is very simple and can be extended, for example, with a chart showing the history of the last hours, etc. Pull requests are welcome.

The complete [source code is available on Github](https://github.com/zefanja/aqi).

**[Enter our Raspberry Pi week giveaway for a chance at this arcade gaming kit.]**

## Wrapping up

For relatively little money, we can measure particulate matter with a Raspberry Pi. There are many possible applications, from a permanent outdoor installation to a mobile measuring device. At our school, we use both: There is a sensor that measures outdoor values day and night, and a mobile sensor that checks the effectiveness of the air conditioning filters in our classrooms.

[Luftdaten.info](http://luftdaten.info/) offers guidance to build a similar sensor. The software is delivered ready to use, and the measuring device is even more compact because it does not use a Raspberry Pi. Great project!

Creating a particulates sensor is an excellent project to do with students in computer science classes or a workshop.

What do you use a [Raspberry Pi](https://openschoolsolutions.org/shutdown-servers-case-power-failure%e2%80%8a-%e2%80%8aups-nut-co/) for?

## 6 Comments