[PYTHON] Ventilation is important. What I did to keep track of the C02 concentration in the room

According to Japan Meteorological Agency, aging of carbon dioxide concentration, the average global CO2 concentration in 2018 is compared with the previous year. It is said that it has increased by 2.3ppm to 407.8ppm.

It is worrisome that the CO2 concentration continues to increase due to global warming, but the current concerns about the epidemic of the new coronavirus are "sealed," "dense," and "close." The question is whether the room is properly ventilated to avoid the risk of "sealing" in the dense.

About the indoor CO2 concentration Ministry of Health, Labor and Welfare Building Environmental Sanitation Management Standards shows 1,000 ppm or less Ministry of Education, Culture, Sports, Science and Technology School Environmental Hygiene Standards shows 1,500 ppm or less Is the management standard. Also, this figure compiled by CHC GROUP may be helpful. image.png (Source: http://group.chcsys.net/jp/chcgroup/)

I wanted to have proper data on the CO2 concentration of my place in comparison with the standard, so I decided to measure it using a sensor.

What you have prepared

Raspberry Pi 3 Model B CO2 sensor MH-Z19B

OS Debian GNU/Linux Linux raspberrypi 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l

Enable Serial Port in Raspberry Pi Settings-Interface. image.png

Restart the Raspberry Pi after enabling the Serial Port. image.png

wiring

Solder the wiring to each of GND, Vin, TxD, and RxD. image.png image.png (From the wiring diagram https://pypi.org/project/mh-z19/)

Connect the Raspberry Pi and the CO2 sensor according to the wiring diagram. image.png

Package installation

Based on the information at https://pypi.org/project/mh-z19/. Package installation

$ sudo pip3 install mh-z19
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting mh-z19
  Downloading https://www.piwheels.org/simple/mh-z19/mh_z19-0.4.1-py3-none-any.whl
Collecting argparse (from mh-z19)
  Downloading https://files.pythonhosted.org/packages/f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl
Requirement already satisfied: pyserial in /usr/lib/python3/dist-packages (from mh-z19) (3.4)
Collecting getrpimodel (from mh-z19)
  Downloading https://www.piwheels.org/simple/getrpimodel/getrpimodel-0.1.15-py3-none-any.whl
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from mh-z19) (2.21.0)
Installing collected packages: argparse, getrpimodel, mh-z19
Successfully installed argparse-1.4.0 getrpimodel-0.1.15 mh-z19-0.4.1

Measurement value acquisition test

~~ Module mh_z19 seems to output the result in dictionary type. ~~ Module mh_z19 seems to output the result in JSON. (I received an edit request from @UedaTakeyuki. Thank you.)

$ sudo python3 -m mh_z19
{"co2": 1456}

Create a program

The measured value is output together with the date and time.

import datetime
import mh_z19

now = datetime.datetime.now()
now = "{0:%Y-%m-%d %H:%M:%S}".format(now)
value = mh_z19.read().get("co2")  # ex: {"co2": 1456}
out = now + ',' + str(value)
print(out)  #Output to console

Output result

2020-04-15 19:34:01,1950

Creating a shell script

Redirects the result of standard output to a log file. do.sh

#!/bin/sh

cd /home/pi/mh-z19
sudo python3 MeasureAndOutput.py >> ./log/logfile.csv

Put the shell script into executable mode.

$ chmod +x do.sh

Scheduled execution settings

$ crontab -e

The editor will start, so set it to measure every minute and output the log.

# m h  dom mon dow   command
* * * * * mh-z19/do.sh

Restart cron for the settings to take effect.

$ sudo /etc/init.d/cron restart
[ ok ] Restarting cron (via systemctl): cron.service.

When I checked it a few minutes later, the log was output according to the scenario.

$ cat logfile.csv
2020-04-15 19:34:01,1950
2020-04-15 19:35:02,1957
2020-04-15 19:36:01,1965
2020-04-15 19:37:01,1976
2020-04-15 19:38:02,1983
2020-04-15 19:39:01,1996
2020-04-15 19:40:01,2032

Try to make a graph

Based on the output log file, I tried to make a graph manually for the time being. image.png

What I want to do after this

・ Alert to LINE Bot when the concentration exceeds a certain level ・ Voice alert by smart speaker

Qiita's senior who was helpful

@revsystem https://qiita.com/revsystem/items/76ab1e21d386c5977892 @tororu https://qiita.com/tororu/items/20e8625d3fb24f3e06da @UedaTakeyuki https://qiita.com/UedaTakeyuki/items/c5226960a7328155635f

Recommended Posts

Ventilation is important. What I did to keep track of the C02 concentration in the room
What I did to keep track of the humidity and temperature of the archive
[At Coder] What I did to reach the green rank in Python
How to keep track of work in Powershell
What I did to ssh to the VPS Ubuntu environment
What to do if the progress bar is not displayed in tqdm of python
What I did when I was angry to put it in with the enable-shared option
I want to make the LED Lighting of ErgoDox EZ shine, but tell me what the LED is in the first place
What I did to speed up the string search task
What I did to welcome the Python2 EOL with confidence
I tried to illustrate the time and time in C language
Report from the site. What I think is necessary to utilize AI / IoT in the manufacturing industry
How to give and what the constraints option in scipy.optimize.minimize is
[Introduction to Python] I compared the naming conventions of C # and Python.
I wrote the code to write the code of Brainf * ck in python
I want to know the population of each country in the world.
What to do when the value type is ambiguous in Python?
H29.2.27 ~ 3.5 Summary of what I did
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
[Linux] What is the method to solve the package dependency error of yum and rpm in the actual workplace?
What to do when the result downloaded via scrapy is in English
[C language] I want to generate random numbers in the specified range
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
What kind of book is the best-selling "Python Crash Course" in the world?
I want to leave an arbitrary command in the command history of Shell
What is a C language library? What is the information that is open to the public?
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
[Introduction to Python] What is the method of repeating with the continue statement?
What is the fastest way to create a reverse dictionary in python?
I created a script to check if English is entered in the specified position of the JSON file in Python.
Implement part of the process in C ++
What is the cause of the following error?
What is "mahjong" in the Python library? ??
I wrote the selection sort in C
What I did to save Python memory
I want to get the path of the directory where the running file is stored.
I want to visualize the transfer status of the 2020 J League, what should I do?
I want to use Python in the environment of pyenv + pipenv on Windows 10
I was in trouble because the behavior of docker container did not change
Why Docker is so popular. What is Docker in the first place? How to use
What I did when I got stuck in the time limit with lambda python
I made an appdo command to execute a command in the context of the app
AtCoder AGC 041 C --I was addicted to the full search of Domino Quality
I want to set a life cycle in the task definition of ECS
I want to see a list of WebDAV files in the Requests module
I want to store the result of% time, %% time, etc. in an object (variable)
Hackathon's experience that it is most important to understand the feelings of the organizer
What to do if (base) is displayed at the beginning of the Mac terminal
linux / c> link> Get the execution result of the shell command in the C program> I was taught how to use popen ()
How to use the C library in Python
What is wheezy in the Docker Python image?
[Minecraft] What are the important items in survival?
I tried to touch the API of ebay
I tried to correct the keystone of the image
What I did when updating from Python 2.6 to 2.7
I want to customize the appearance of zabbix
I want to display the progress in Python!