惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Weird Python "Features" That Might Catch You By Surprise Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Dictionary Dispatch Pattern in Python Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime What is Python's "self" Argument, Anyway? Python List Comprehensions Are More Powerful Than You Might Think You Should Be Using Python's Walrus Operator - Here's Why Recipes and Tricks for Effective Structural Pattern Matching in Python It's Time to Say Goodbye to These Obsolete Python Libraries Advanced Features of Kubernetes' Horizontal Pod Autoscaler Data and System Visualization Tools That Will Boost Your Productivity Stop Messing with Kubernetes Finalizers Automate All the Boring Kubernetes Operations with Python End-to-End Monitoring with Grafana Cloud with Minimal Effort Bitly | bit.ly/3JLmSgA Bitly | bit.ly/3uETfbi Ultimate CI Pipeline for All of Your Python Projects Bitly | bit.ly/3M30D82 Bitly | bit.ly/3oMJ6qR Bitly | bit.ly/3IRD7IK Bitly | bit.ly/3A3B69t Profiling and Analyzing Performance of Python Programs Bitly | bit.ly/30uviIM Bitly | bit.ly/3E1X2mw Bitly | bit.ly/3Dv7JxP Bitly | bit.ly/3GG1BEz Bitly | bit.ly/3lLavs4 Bitly | bit.ly/39TqP3m Bitly | bit.ly/3A5Mpx8 Bitly | bit.ly/3kGwPl4 Bitly | bit.ly/3iHtulU Bitly | bit.ly/3xGjtKS Bitly | bit.ly/3h8DZg0 Bitly | bit.ly/2RQn1dG Bitly | bit.ly/3p2B5wW Bitly | bit.ly/3tULpb0 Bitly | bit.ly/2PHVudx Bitly | bit.ly/3uPtnb0 Bitly | bit.ly/3dg3QR9 Bitly | bit.ly/3qHtSkZ Bitly | bit.ly/3kIkTPr Bitly | bit.ly/3qlRAUN Bitly | bit.ly/3pCUJ26 Hardening Docker and Kubernetes with seccomp Bitly | bit.ly/34ZhIMt Bitly | bit.ly/3qSO7h0 Bitly | bit.ly/3muGLOk Bitly | bit.ly/35xN79v Bitly | bit.ly/3mLGshK Bitly | bit.ly/2IvkGQl Bitly | bit.ly/2Sk1KFK Bitly | bit.ly/3iCNIL6 Bitly | bit.ly/3beQPpy Saving Your Linux Machine from Certain Death New Features in Python 3.9 You Should Know About Deploy Any Python Project to Kubernetes Analyzing Docker Image Security Recursive SQL Queries with PostgreSQL Automating Every Aspect of Your Python Project Tour of Python Itertools Implementing 2D Physics in Javascript Ultimate Setup for Your Next Python Project Making Python Programs Blazingly Fast Security and Cryptography Mistakes You Are Probably Doing All The Time Going Serverless with OpenFaaS and Golang - Building Optimized Templates Going Serverless with OpenFaaS and Golang - The Ultimate Setup and Workflow Setting Up Swagger Docs for Golang API Building RESTful APIs in Golang Pytest Features, That You Need in Your (Testing) Life Setting up GitHub Package Registry with Docker and Golang Ultimate Setup for Your Next Golang Project Python Tips and Trick, You Haven't Already Seen, Part 2. Tricks for Postgres and Docker that will make your life easier Getting The Most Out of Reading Books - Reading The "Professional Way" Python Tips and Trick, You Haven't Already Seen
Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor
Martin · 2024-01-03 · via Martin Heinz's Blog

Low indoor air quality - or high CO2 - negatively impacts cognitive performance, causes headaches, drowsiness and more. It's easy to fix though, just use a CO2 sensor and open a window from time-to-time.

But why stop there, when you can set up complete air quality monitoring solution using CO2 sensor, Prometheus, Grafana dashboards and alerts?

Note: All code used in this article is available in this repository.

What You Will Need

Let's start with the tools we will need to build this:

  • CO2 Sensor - Obviously, we will need a sensor to measure CO2 (and optionally other metrics like temperature or humidity). I use Aranet4 Home, but to make this setup work, any bluetooth-capable device would work (e.g. AirThings Wave Plus), or you could even build your own with ESP32/Arduino (see also this full build). Search GitHub tags for more ideas.
  • Raspberry Pi (RPi) - to collect data from the sensor and send it to Prometheus. Again, any network-connected device that can run continuously and can receive data over Bluetooth would work here. To test things out, your computer will work just fine.
  • Prometheus instance - I use Grafana Cloud free account, feel free to use any other service or self-managed instance.

The Architecture

As for the design/architecture, this how it works:

  • The CO2 sensor measures the metrics in some intervals and makes them available through its Bluetooth interface
  • RPi runs a Prometheus Exporter that collects the data over bluetooth in some intervals. The exporter then publishes the data on /metrics endpoint in Prometheus-compatible format
  • RPi also runs Grafana Cloud agent that scrapes the /metrics and pushes the data to Prometheus instance

Diagram

The Setup

The deployment has following 3 parts:

  • Install necessary packages on RPi, such as bluetooth.
  • Use bluetoothctl CLI to find and pair RPi with the CO2 sensor
  • Deploy the Prometheus exporter along with an agent, and start scraping and pushing the data

To make mine (and possibly also your) life easier, I automated most of the set-up using Ansible. All the code, as well as some notes are available in this repository. The following steps assume that you have the repository cloned on your machine.

Now, to actually run the set-up, I will assume that you have RPi on your local network. You can find its IP with:


nmap -sn 192.168.1.0/24
Starting Nmap 7.80 ( https://nmap.org ) at 2023-12-14 14:20 CET
...
Nmap scan report for raspberrypi (192.168....)
Host is up (0.0048s latency).
...
Nmap done: 256 IP addresses (3 hosts up) scanned in 2.51 seconds

And to make your life easier, I suggest you add it to ~/.ssh/config, e.g.:


...
Host rpi
 Hostname 192.168....
 User pi

And you can test it out with ssh rpi or ssh pi@rpi.

Additionally, to run Ansible playbooks, you will need to put the IP in inventory. So, update the ansible/inventory file in the cloned repository.

With that done you can run the first playbook:


cd ansible
ansible-playbook rpi.yaml

Which completes the first part of the setup - that is - install Docker and docker-compose, as well as Bluetooth-related packages.

You might be wondering why we need to Docker here - while it's not necessary, and you could run the agent as a binary or systemd service, I think it's more practical to use Docker and docker-compose, mainly because of environment variables.

As for the second part - finding and pairing the Bluetooth device - this cannot be easily automated, because bluetoothctl is meant as an interactive CLI. So, here's a manual setup:


ssh pi
sudo su -

systemctl start hciuart

bluetoothctl
> power on
> agent on
> scan on
[NEW] Device DD:...:1F Aranet4 26D0F
> scan off
> pair DD:...:1F
# enter PIN displayed on Aranet4 device
> trust DD:...:1F
> agent off
> quit

We first ssh into the RPi and switch to root and start the hciuart service which is needed for communication through Bluetooth interface. After that, we start interactive bluetoothctl session - we power on the controller; start the Bluetooth agent (similar to SSH agent) and start a scan for devices. You will need to find your sensor in the output and take a note of its MAC address, after which you can stop the scan. Next, we run pair command with the MAC address noted earlier - this should prompt for a PIN, which should display on the Aranet4 sensor screen. Finally, we trust the device; stop the agent and exit the session.

After this, we can do the last step - which is - deploying the exporter and agent.

First we populate the environment file at ansible/files/.env with the URL and credentials to Prometheus which will be used by the agent, and then we simply run the aranet.yaml playbook, substituting the aranet_device variable with MAC of your sensor and the "friendly name" in format {MAC}={Name}. You can optionally also set the scraping interval with aranet_interval. It defaults to 60 seconds, the higher the frequency, the faster will the battery drain.


ansible-playbook aranet.yaml --extra-vars "aranet_device='DD:...:1F=office' aranet_interval=60"

This playbook copies the environment variables file and starts the agent using docker-compose. It also installs the exporter as a systemd service. Be aware that this part will only work if you're using Aranet4 - if you have different sensor, then you will have to find/write exporter that can scrape data from your sensor, such as this one for AirThings.

With all that done, we can test out whether the exporter is able to scrape the data from sensor:


curl http://localhost:9100/metrics | grep aranet

# Should show something like:
# aranet4_co2_ppm{room="office"} 995
# ...

Data, Dashboards and Alerts

At this point, the data should be flowing to Prometheus. You can use the Explore feature of Prometheus to read the metrics listed in the output of curl http://localhost:9100/metrics, but I also created a Grafana dashboard that shows all the available metrics from Aranet4:

Grafana Dashboard

Here you can see CO2, temperature, humidity and atmospheric pressure measurements as well as battery life.

You can find the dashboard JSON config in the repository here, which can be imported into your Grafana.

Be aware that Aranet4 measures absolute humidity, so it needs to be adjusted for altitude. The included dashboard adds 24 due my altitude, you will probably need to adjust that. Compare local weather station relative humidity with Aranet4's absolute value to find the offset.

The dashboard also assumes that the Prometheus datasource is named DS_GRAFANACLOUD-PROM, you will need to change that as well.

Finally, with all the data available, we could also set up also alerts using Alertmanager, but I will leave that as "exercise for the reader", because I personally don't want to be spammed via email or Slack about high CO2 in my room. 😉

Closing Thoughts

Could you just use the phone app that comes with sensor? Sure. Is this setup over-engineered? Probably. Is it unnecessary? Most definitely; but it works, and it's fun to tinker with hardware.

It also gives you full control of the data; transforms it into standardized format in case you want to play with it further, and you don't have to rely on closed-source smartphone app.