Monitoring CO2 Levels with Raspberry Pi and CO2-mini Using Zabbix
Image credit: Tomokatsu YukishitaIntroduction
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版】
CO2-mini
カスタム (CUSTOM) CO2モニター CO2-mini
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適用
サンディスク microSD 128GB UHS-I Class10 Nintendo Switch メーカー動作確認済 SanDisk Ultra SDSQUA4-128G-EPK エコパッケージ
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
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.

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


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
I was able to retrieve both CO2 concentration and temperature and plot them in Zabbix.

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

Trend after installation. In winter, ventilation quickly makes the room feel cold.
Script Download
You can download the script from the following repository: yukishita/co2-mini on GitHub



