1. 程式人生 > >使用微創聯合M5S空氣檢測儀、樹莓派3b+、prometheus、grafana實現空氣質量持續監控告警WEB視覺化

使用微創聯合M5S空氣檢測儀、樹莓派3b+、prometheus、grafana實現空氣質量持續監控告警WEB視覺化

# 1.簡介 使用微創聯合M5S空氣檢測儀、樹莓派3b+、prometheus、grafana實現空氣質量持續監控告警WEB視覺化 grafana dashboard效果: ![image-20201031171328947](https://img2020.cnblogs.com/other/1900199/202010/1900199-20201031173250106-1100564947.png) # 2.背景 ## **2.1 需求:** 1.過段時間公司要搬新辦公室,較多同事擔心甲醛(HCHO)、異味(TVOC)這些重要的空氣指標,所以需要能夠對其檢測 2.能夠把這些空氣指標進行WEB展示 3.監控告警關心的空氣指,告警傳送到釘釘群 ## **2.2 已有硬體:** 樹莓派3b+ (長期吃灰中)以下簡稱樹莓派 微創聯合M5S空氣檢測儀(版本:M5S溫溼+鋰電+TVOC+**資料匯出** 17年485元價格購買 )以下簡稱空氣檢測儀 M5S 家用 鐳射PM2.5檢測儀 甲醛 CO2 空氣質量 霧霾甲醛 檢測儀 【在售價】380.00 元(基礎版) 【立即下單】點選連結立即下單:https://s.click.taobao.com/zREU4vu ![img](https://img2020.cnblogs.com/other/1900199/202010/1900199-20201031173251634-671868087.jpg) # 3.設計 把空氣檢測儀的usb插到樹莓派上(樹莓派上通過串列埠讀取資料),供電也是通過樹莓派供電 相關軟體執行在樹莓派上 ## **3.1 硬體:** 空氣檢測儀如何匯出資料? 空氣檢測儀是有資料匯出版本,自帶串列埠轉USB(ch340晶片) 開啟空氣檢測儀並通過檢測儀左側 FUN 按鈕,把螢幕切換到**第 3 屏**,這個時候串列埠中就會持續輸出監控資料 在linux上裝置檔案符為“/dev/ttyUSB0” ``` cat /dev/ttyUSB0 #可以看到 24.9 45.2 23 32 35 26 33 35 4419 1301 159 8 3 0 0.006 0.21 ``` 讀取過程中,按一下檢測儀上的 **RST 鍵**,可以顯示每列資料對應的欄位定義,(按完後需要再按 FUN 鍵切換到**第 3 屏**) ``` *--------------------------------------- Data Output Format Definition ----------------------------------------* TEMP HUMI CH_PM1.0 CH_PM2.5 CH_PM10 US_PM1.0 US_PM2.5 US_PM10 >0.3um >0.5um >1.0um >2.5um >5.0um >10um HCHO TVOC ``` | TEMP | HUMI | CH_PM1.0 | CH_PM2.5 | CH_PM10 | US_PM1.0 | US_PM2.5 | US_PM10 | >0.3um | >0.5um | >1.0um | >2.5um | >5.0um | >10um | HCHO | TVOC | | ---- | ---- | -------- | -------- | ------- | -------- | -------- | ------- | ------ | ------ | ------ | ------ | ------ | ----- | ----- | ---- | | 25.1 | 43.3 | 21 | 29 | 31 | 22 | 29 | 31 | 3843 | 1136 | 170 | 6 | 2 | 0 | 0.003 | 0.2 | | 25.1 | 43.2 | 22 | 30 | 31 | 23 | 30 | 31 | 3876 | 1156 | 170 | 5 | 2 | 0 | 0.005 | 0.16 | | 25.1 | 43.3 | 22 | 30 | 31 | 23 | 30 | 31 | 3963 | 1174 | 164 | 6 | 2 | 0 | 0.003 | 0.21 | | 25.2 | 43.3 | 22 | 30 | 31 | 23 | 30 | 31 | 3942 | 1163 | 167 | 6 | 2 | 0 | 0.007 | 0.21 | 帶二氧化碳檢測的版本在HCHO欄位前多一個CO2欄位 更多資料看空氣檢測儀配套的資料 ## **3.2 軟體:** 軟體監控採用prometheus + grafana的方案 本文為流水理魚wwek原創 www.iamle.com 現在只需要實現一個串列埠(/dev/ttyUSB0) exporter即可把空氣監測儀的資料打通給prometheus使用 最後配置prometheus取空氣檢測儀的exporter資料,並配置grafana面板 監控告警直接使用grafana帶的監控告警 # 4.實現 ## *安裝配置wclh_air_detector_exporter 獲得空氣檢測儀資料 wclh_air_detector_exporter讀取串列埠資料並把資料進行結構化,然後輸出metrics M5S Temperature and Humidity+lithium battery+CO2+TVOC PM2.5 CO2(S8)TEMP&HUMI Detector Haze PM2.5 sensors Laser PM2.5 detector M5S 家用 鐳射PM2.5檢測儀 甲醛 CO2 空氣質量 霧霾甲醛 檢測儀 ``` WCLH_AIR_DETECTOR_EXPORTER_VERSION=0.1.3 wget https://github.com/wwek/wclh_air_detector_exporter/releases/download/v${WCLH_AIR_DETECTOR_EXPORTER_VERSION}/wclh_air_detector_exporter_${WCLH_AIR_DETECTOR_EXPORTER_VERSION}_linux_armv7.tar.gz tar zxvf wclh_air_detector_exporter_${WCLH_AIR_DETECTOR_EXPORTER_VERSION}_linux_armv7.tar.gz mkdir -p /data/soft/wclh_air_detector_exporter mv wclh_air_detector_exporter /data/soft/wclh_air_detector_exporter cd /data/soft/wclh_air_detector_exporter #./wclh_air_detector_exporter -serial_port /dev/ttyUSB0 #自動啟動&程序守護 sudo bash -c 'cat > /etc/systemd/system/wclh_air_detector_exporter.service << EOF [Unit] Description=https://github.com/wwek/wclh_air_detector_exporter Wants=network-online.target After=network-online.target [Service] Restart=on-failure #User=root ExecStart=/data/soft/wclh_air_detector_exporter/wclh_air_detector_exporter [Install] WantedBy=default.target EOF' sudo systemctl daemon-reload sudo systemctl status wclh_air_detector_exporter sudo systemctl start wclh_air_detector_exporter sudo systemctl enable wclh_air_detector_exporter sudo systemctl status wclh_air_detector_exporter curl http://localhost:9166/metrics ``` ## 安裝配置prometheus ``` PROMETHEUS_VERSION=2.22.0 wget https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-armv7.tar.gz tar zxvf prometheus-${PROMETHEUS_VERSION}.linux-armv7.tar.gz mkdir -p /data/soft/ mv prometheus-${PROMETHEUS_VERSION}.linux-armv7 prometheus && mv prometheus /data/soft/ #自動啟動&程序守護 sudo bash -c 'cat > /etc/systemd/system/prometheus.service << EOF [Unit] Description=https://prometheus.io Wants=network-online.target After=network-online.target [Service] Restart=on-failure #User=root ExecStart=/data/soft/prometheus/prometheus --config.file="/data/soft/prometheus/prometheus.yml" [Install] WantedBy=default.target EOF' sudo systemctl daemon-reload sudo systemctl status prometheus sudo systemctl start prometheus sudo systemctl enable prometheus sudo systemctl status prometheus curl http://localhost:9090 #Found. ``` ## 安裝配置grafana ``` sudo apt-get install -y apt-transport-https sudo apt-get install -y software-properties-common wget wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add - echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list sudo apt-get update sudo apt-get install grafana sudo systemctl daemon-reload sudo systemctl start grafana-server sudo systemctl enable grafana-server sudo systemctl status grafana-server curl http://localhost:3000 #Found. ``` grafana 中先配置 prometheus(http://localhost:9090)資料來源,然後匯入 “grafana-dashboard.json”空氣檢測儀的dashboard ## 監控告警 直接使用grafana自帶的告警功能,將關心的指標進行監控並告警 本文首發於[流水理魚部落格](https://www.iamle.com/?utm_source=cnblogs),如要轉載請註明出處。 歡迎關注我的公眾號:流水理魚(liushuiliyu),全棧、雲原生、Homelab交流。 如果您對相關文章感興趣,也可以關注我的部落格:[www.iamle.com](https://www.iamle.com/?utm_source=cnblogs) 上面有更多內容