Monitor CO2 Levels with Raspberry Pi and CO2-mini, and Record with ZABBIX

Image credit: Tomokatsu Yukishita

Introduction

During the COVID-19 pandemic, I bought a CO2 monitor “CUSTOM CO2-mini” at a home center. It uses USB power, and according to information on the web, it seems capable of communication. So, I decided to connect it to a Raspberry Pi and record the room’s CO2 levels using ZABBIX.

Materials Needed

Raspberry Pi

【国内正規代理店品】Raspberry Pi4 ModelB 4GB ラズベリーパイ4 技適対応品【RS・OKdo版】
thumbnail
Raspberry Pi 4 は、エントリレベルの PC と同等の電力を供給するアップグレードされたプロセッサで、パフォーマンスが向上しています。(基板・パッケージに技適マーク入りです。工事設計認証番号:007-AH0184)
https://www.amazon.co.jp/【国内正規代理店品】Raspberry-Pi4-ModelB-ラズベリーパイ4-技適対応品/dp/B081YD3VL5?keywords=raspberry+pi&qid=1653384720&refinements=p_76:2227292051&rnid=2227291051&rps=1&sprefix=rasp,aps,367&sr=8-9&linkCode=sl1&tag=snowunderco07-22&linkId=3ebf8be4e6da4ccca7355be0f468de28&language=ja_JP&ref_=as_li_ss_tl

CO2-mini

カスタム (CUSTOM) CO2モニター CO2-mini
thumbnail
●周囲環境のCO2レベルを数値で表示。3段階のLEDでレベル表示されるので、直感的でわかりやすいのが特徴です。
●温度センサーも内蔵。CO2レベルと室温を交互に表示します。

https://www.amazon.co.jp/カスタム-CUSTOM-CO2-mini-CO2モニター/dp/B00I3XJ9LM?__mk_ja_JP=カタカナ&crid=271CTRYHBKC0V&keywords=co2-mini&qid=1653384769&sprefix=co2-mini,aps,162&sr=8-4&linkCode=sl1&tag=snowunderco07-22&linkId=a2fd9c50707ad0292fb0980dd8aa0d90&language=ja_JP&ref_=as_li_ss_tl

Additional items, such as a power source and SD card.

Smraza Raspberry Pi 4 USB-C (Type C)電源、5V 3A ラズベリーACアダプター RPi 4b Model B 1GB / 2GB / 4GB/ 8GB適用
thumbnail
  • 規格:5V 3A電源アダプター Raspberry Pi 4に適用.1.5M type C ケーブル オン/オフスイッチ付き.出力: DC 5V/3A 入力: AC100-240V
  • ON/OFFスイッチ付き:ワンボタン操作で電源のオン・オフ操作が出来ます。電源アダプターを差し込んだり抜いたりする必要は有りません。

https://www.amazon.co.jp/Smraza-Raspberry-用USB電源アダプター-ラズベリーACアダプター-aspberry/dp/B07DN5V3VN?__mk_ja_JP=カタカナ&crid=1F9Z33LD1C6QF&keywords=raspberry+pi+電源&qid=1653384828&sprefix=raspberry+pi+電源,aps,162&sr=8-6&linkCode=sl1&tag=snowunderco07-22&linkId=14d40b92bcf1b88b5676b8b0661bd044&language=ja_JP&ref_=as_li_ss_tl
サンディスク microSD 128GB UHS-I Class10 Nintendo Switch メーカー動作確認済 SanDisk Ultra SDSQUA4-128G-EPK エコパッケージ
thumbnail
  • A1規格のパフォーマンスで素早くアプリを起動
  • フルHDビデオの録画や視聴に最適
  • SDサイズへの変換アダプタ―付属。収納プラスチックケース入り。
  • Androidスマホやタブレット、オーディオプレーヤー、任天堂Switch、Chromebookに最適
  • 製品保証: Amazon購入日より30日間の初期不良返品対応のみとなります

https://www.amazon.co.jp/microSD-Nintendo-メーカー動作確認済-SanDisk-SDSQUA4-128G-EPK/dp/B08K41Q79R?__mk_ja_JP=カタカナ&crid=2ZOLZY3M5TWUN&keywords=micro+sdカード&qid=1653384877&sprefix=micro,aps,154&sr=8-6&linkCode=sl1&tag=snowunderco07-22&linkId=16070782f32f40a1c08c09136d38c2c3&language=ja_JP&ref_=as_li_ss_tl

How to Retrieve Data from the CO2-mini

By connecting the CO2-mini and Raspberry Pi via USB, it is automatically recognized without additional setup. For data retrieval, there’s a convenient Python library created by others.

GitHub - heinemml/CO2Meter: Python Module to use co2meter code derived from https://hackaday.io/project/5301-reverse-engineering-a-low-cost-usb-co-monitor
thumbnail
Python Module to use co2meter code derived from https://hackaday.io/project/5301-reverse-engineering-a-low-cost-usb-co-monitor - heinemml/CO2Meter
https://github.com/heinemml/CO2Meter

Once you install the CO2Meter, you can use it immediately.

$ sudo pip3 install git+https://github.com/heinemml/CO2Meter

Sample code was created based on the example.

from CO2Meter import *
import time
sensor = CO2Meter("/dev/hidraw0")
while True:
    time.sleep(1)
    data = sensor.get_data()
    if 'temperature' in data and 'co2' in data:
        f = open('/dev/shm/co2', 'w')
        f.write(str(data['co2']))
        f.close()

        f = open('/dev/shm/temperature', 'w')
        f.write(str(data['temperature']))
        f.close()

Setting to Start Automatically at Boot with root Privileges

Prepare a shell script to have it start automatically on boot via crontab (requires root privileges to work properly).

#!/bin/bash

sleep 5
/usr/bin/python /root/co2.py

Add the above script to crontab.

@reboot /root/co2.sh

Configure ZABBIX to Read Data (ZABBIX Server Setup)

There are several methods, but I chose the following:

  • Write the CO2 value obtained in the shared memory of Raspberry Pi.
  • Loop to keep the latest data constantly.
  • When retrieving data from ZABBIX, read the file stored in shared memory.

Create a Host

Install the zabbix-agent on the Raspberry Pi and create a host for the target IP address.

screen reader text

Create Items

Since both CO2 and temperature can be measured, create items to retrieve both.

screen reader text

screen reader text

Set the keys as follows:

co2.co2   (CO2 concentration)
co2.temp  (Temperature)

zabbix-agent Configuration

Make sure the zabbix-agent is running.

Edit /etc/zabbix/zabbix_agentd.conf and set the zabbix-server address.

/etc/zabbix/zabbix_agentd.conf

### Option: Server
#       List of comma delimited IP addresses, optionally in CIDR notation, or DNS names of Zabbix servers and Zabbix proxies.
#       Incoming connections will be accepted only from the hosts listed here.
#       If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally
#       and '::/0' will allow any IPv4 or IPv6 address.
#       '0.0.0.0/0' can be used to allow any IPv4 address.
#       Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
#
# Mandatory: yes, if StartAgents is not explicitly set to 0
# Default:
# Server=

Server= Server Address

##### Active checks related

### Option: ServerActive
#       List of comma delimited IP:port (or DNS name:port) pairs of Zabbix servers and Zabbix proxies for active checks.
#       If port is not specified, default port is used.
#       IPv6 addresses must be enclosed in square brackets if port for that host is specified.
#       If port is not specified, square brackets for IPv6 addresses are optional.
#       If this parameter is not specified, active checks are disabled.
#       Example: ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1]
#
# Mandatory: no
# Default:
# ServerActive=

ServerActive=Server Address

### Option: Include
#       You may include individual files or all files in a directory in the configuration file.
#       Installing Zabbix will create include directory in /etc/zabbix, unless modified during the compile time.
#
# Mandatory: no
# Default:
# Include=

# Include=/etc/zabbix/zabbix_agentd.userparams.conf
# Include=/etc/zabbix/zabbix_agentd.conf.d/
Include=/etc/zabbix/zabbix_agentd.conf.d/*.conf

/etc/zabbix/zabbix_agentd.conf.d/userparameter_co2.conf

Create a config file to retrieve parameters by using cat on the file stored in shared memory.

UserParameter=co2.co2,cat /dev/shm/co2
UserParameter=co2.temp,cat /dev/shm/temperature

Restart the zabbix-agent.

Results

Successfully obtained CO2 concentration and temperature and plotted them on ZABBIX.

screen reader text

CO2が取得できました

Summary

This summarizes the method of monitoring with Raspberry Pi + CO2-mini + ZABBIX. Since purchasing the CO2 monitor, ventilation frequency has significantly increased.

screen reader text

設置してからの推移 冬は換気すると寒い。

Script Download

You can download the script created from the following link.

GitHub - yukishita/co2-mini: CUSTOM CO2-mini を使って ZABBIX で CO2 濃度を記録するプログラム
thumbnail
CUSTOM CO2-mini を使って ZABBIX で CO2 濃度を記録するプログラム. Contribute to yukishita/co2-mini development by creating an account on GitHub.
https://github.com/yukishita/co2-mini

Related