LoginSignup
0
1

More than 1 year has passed since last update.

室内環境をGrafanaで可視化する。

Last updated at Posted at 2022-02-20

温室度センサ(BME280)やSwitchBot温湿度計を使って室温、湿度の記録は取ってあったが、CO2センサ(SCD40)を導入したので、Grafanaを用いて計測データを可視化してみた。

データの記録

SCD4xの計測コードはGithubにPythonで書かれたものがあるので、ダウンロードして利用。

exampl.py
# SPDX-FileCopyrightText: 2020 by Bryan Siepert, written for Adafruit Industries
#
# SPDX-License-Identifier: Unlicense
import time
import board
import adafruit_scd4x

i2c = board.I2C()
scd4x = adafruit_scd4x.SCD4X(i2c)
print("Serial number:", [hex(i) for i in scd4x.serial_number])

scd4x.start_periodic_measurement()
print("Waiting for first measurement....")

while True:
    if scd4x.data_ready:
        print("CO2: %d ppm" % scd4x.CO2)
        print("Temperature: %0.1f *C" % scd4x.temperature)
        print("Humidity: %0.1f %%" % scd4x.relative_humidity)
        print()
    time.sleep(1)

サンプルコードは毎秒計測する上に、温度、湿度のデータもついてくるが、CO2濃度だけ1回吐いてくれれば良いので、出力を削ってループも廃止して、

scd4x_measure.py
import time
import board
import adafruit_scd4x

i2c = board.I2C()
scd4x = adafruit_scd4x.SCD4X(i2c)

scd4x.start_periodic_measurement()

time.sleep(10)
#センサのウェイクアップ待ち

if scd4x.data_ready:
    print("%d" % scd4x.CO2)

書き慣れたrubyでjson化して、シェルで繋いで出力。

co2_measure.rb
require 'json'
require 'net/http'
require 'openssl'
require 'uri'

data = gets.chop
output_data = {"CO2" => data }
puts JSON.pretty_generate(output_data)
json_output.sh
python3 /home/pi/scd40/scd4x_measure.py |ruby /home/pi/scd40/measure.rb > /mnt/ramdisk/co2_data.json
co2_data.json
{
  "CO2": "726"
}

他の計測データとまとめて、部屋の名前を書き足して、nginxなどで公開しておく。

measured_data.json
{
  "temperature": 20.5,
  "humidity": 52,
  "co2": "1556",
  "position": "LDK",
  "time": "2022-02-20 23:28:14"
}

データ集積

MySQLかMariaDBサーバを立てておいて、以下のようなテーブルを作成

ID date room temp humidity co2
1 2022-02-20 23:28:14.000 LDK 20.5 52 1556

各部屋ごとにおいてあるraspbery piからデータを集めてデータベースへ書き込み。

sql.rb
#!/usr/bin/ruby

require 'mysql2'
require 'json'
require 'net/http'

room_list = ["raspizero.local","raspi2.local"]

room_list.each do |raspi|
  room_condition = Hash.new
  room_condition=JSON.parse(Net::HTTP.get(raspi,'/measured_data.json'))
client = Mysql2::Client.new(
  host: "sql server url", 
  username: "username",
  password: "password",
  init_command: "USE iot")
client.query("INSERT INTO roomdata (id,date,room,temp,humidity,co2) VALUES (NULL,'#{room_condition["time"]}','#{room_condition["room"]}','#{room_condition["temperature"]}','#{room_condition["humidity"]}','#{room_condition["co2"]}');")

end

可視化

Grafanaでは計測データの取り込み部分に直接SQL文を書いた。

measure.sql
SELECT
  UNIX_TIMESTAMP(date) AS "time",
  room AS metric,
  value
FROM roomtemp
WHERE
  room = "LDK"
  and
  $__timeFilter(date)
ORDER BY date

ということで割と簡単に可視化できた。
IMG_3840.jpeg
CO2濃度高くて、なかなか1000ppm切らないので、24時間換気の導入検討中。
また、いちいちスマホやPC使わずに値を見たいので、適当な小型ディスプレイなどで表示させたいところ。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1