0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

環境センサのデータをInfluxDBとGrafanaを使って可視化する(2)

Posted at

オムロンの2JCiE-BUからデータを取得して、InfluxDBへ登録してみます。

オムロン提供のサンプルプログラムを取得する

日本語のREADMEもあります。

適当なフォルダを作ってサンプルプログラムを取得します。

nomura@raspberrypi5:~/omron_sensor$ git clone https://github.com/omron-devhub/2jciebu-usb-raspberrypi.git

取得したサンプルを動かしてみます。

nomura@raspberrypi5:~/omron_sensor $ cd 2jciebu-usb-raspberrypi/
nomura@raspberrypi5:~/omron_sensor/2jciebu-usb-raspberrypi $ python3 sample_2jciebu.py

Time measured:2024/06/09 12:20:13
Temperature:25.33
Relative humidity:68.7
Ambient light:253
Barometric pressure:1009.411
Sound noise:69.13
eTVOC:7
eCO2:448
Discomfort index:74.22
Heat stroke:24.0
Vibration information:0
SI value:0.0
PGA:0.0
Seismic intensity:0.0
Temperature flag:0
Relative humidity flag:0
Ambient light flag:0
Barometric pressure flag:0
Sound noise flag:0
eTVOC flag:0
eCO2 flag:0
Discomfort index flag:0
Heat stroke flag:0
SI value flag:0
PGA flag:0
Seismic intensity flag:0

Time measured:2024/06/09 12:20:14
Temperature:25.32
Relative humidity:68.72
Ambient light:253
Barometric pressure:1009.406
Sound noise:63.82
eTVOC:7
eCO2:448
Discomfort index:74.2
Heat stroke:23.99
Vibration information:0
SI value:0.0
PGA:0.0
Seismic intensity:0.0
Temperature flag:0
Relative humidity flag:0
Ambient light flag:0
Barometric pressure flag:0
Sound noise flag:0
eTVOC flag:0
eCO2 flag:0
Discomfort index flag:0
Heat stroke flag:0
SI value flag:0
PGA flag:0
Seismic intensity flag:0

データが取得できたことを確認できたので、取得したデータをInfluxDBに登録していきます。

InfluxDBへの登録

今回は、以下の記事を参考にオムロンのサンプルでデータを表示していた処理をInfluxDBに登録データを作成する形に変更して、そのあとにInfluxDBへ登録する形にしていきます。

importの追加

InfluxDBへ登録するためのライブラリをインポートします。

import influxdb
import datetime

登録データを作成する

サンプルの表示する関数を参考にデータを作成します。

def get_latest_data(data):
    return_data = {}

    return_data['time_measured'] = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
    return_data['temperature'] = str( s16(int(hex(data[9]) + '{:02x}'.format(data[8], 'x'), 16)) / 100)
    return_data['relative_humidity'] = str(int(hex(data[11]) + '{:02x}'.format(data[10], 'x'), 16) / 100)
    return_data['ambient_light'] = str(int(hex(data[13]) + '{:02x}'.format(data[12], 'x'), 16))
    return_data['barometric_pressure'] = str(int(hex(data[17]) + '{:02x}'.format(data[16], 'x')
                                  + '{:02x}'.format(data[15], 'x') + '{:02x}'.format(data[14], 'x'), 16) / 1000)
    return_data['sound_noise'] = str(int(hex(data[19]) + '{:02x}'.format(data[18], 'x'), 16) / 100)
    return_data['eTVOC'] = str(int(hex(data[21]) + '{:02x}'.format(data[20], 'x'), 16))
    return_data['eCO2'] = str(int(hex(data[23]) + '{:02x}'.format(data[22], 'x'), 16))
    return_data['discomfort_index'] = str(int(hex(data[25]) + '{:02x}'.format(data[24], 'x'), 16) / 100)
    return_data['heat_stroke'] = str(s16(int(hex(data[27]) + '{:02x}'.format(data[26], 'x'), 16)) / 100)
    return_data['vibration_information'] = str(int(hex(data[28]), 16))
    return_data['si_value'] = str(int(hex(data[30]) + '{:02x}'.format(data[29], 'x'), 16) / 10)
    return_data['pga'] = str(int(hex(data[32]) + '{:02x}'.format(data[31], 'x'), 16) / 10)
    return_data['seismic_intensity'] = str(int(hex(data[34]) + '{:02x}'.format(data[33], 'x'), 16) / 1000)
    return_data['temperature_flag'] = str(int(hex(data[36]) + '{:02x}'.format(data[35], 'x'), 16))
    return_data['relative_humidity_flag'] = str(int(hex(data[38]) + '{:02x}'.format(data[37], 'x'), 16))
    return_data['ambient_light_flag'] = str(int(hex(data[40]) + '{:02x}'.format(data[39], 'x'), 16))
    return_data['barometric_pressure_flag'] = str(int(hex(data[42]) + '{:02x}'.format(data[41], 'x'), 16))
    return_data['sound_noise_flag'] = str(int(hex(data[44]) + '{:02x}'.format(data[43], 'x'), 16))
    return_data['etvoc_flag'] = str(int(hex(data[46]) + '{:02x}'.format(data[45], 'x'), 16))
    return_data['eco2_flag'] = str(int(hex(data[48]) + '{:02x}'.format(data[47], 'x'), 16))
    return_data['discomfort_index_flag'] = str(int(hex(data[50]) + '{:02x}'.format(data[49], 'x'), 16))
    return_data['heat_stroke_flag'] = str(int(hex(data[52]) + '{:02x}'.format(data[51], 'x'), 16))
    return_data['si_value_flag'] = str(int(hex(data[53]), 16))
    return_data['pga_flag'] = str(int(hex(data[54]), 16))
    return_data['seismic_intensity_flag'] = str(int(hex(data[55]), 16))

    return return_data

InfluxDBへ登録する

データベースの情報(テーブル名やポート番号、ユーザ名、パスワード)を設定して、InfluxDBへ登録します。
InfluxDBは、勉強不足なので参考にしたほぼ記事のままです。

device_id = 'sensor'

influx = influxdb.InfluxDBClient(
    host = 'localhost',
    port = 8086,
    database = 'omron_sensor',
    username = 'test_user',
    password = 'test'
)

def write_influxdb(data):
    json_body = [{
        'measurement': 'sensor',
        'tags': {'macaddr': device_id},
        'time': datetime.utcnow(),
        'fields': data
    }]

    influx.write_points(json_body)

メインループ

メインループの表示していたところをInfluxDBへ登録する処理に置き換えます。

            #print_latest_data(data)
            insertData = get_latest_data(data)
            write_influxdb(insertData)

実行結果

influxコマンドを実行して登録された内容を確認してみます。

nomura@raspberrypi5:~/omron_sensor/2jciebu-usb-raspberrypi $ influx
Connected to http://localhost:8086 version 1.6.7~rc0
InfluxDB shell version: 1.6.7~rc0
> 

使用するテーブルを指定します。

> USE omron_sensor
Using database omron_sensor

SELECT分を利用して、登録したデータを確認してみます。

> SELECT * FROM sensor
name: sensor
time                ambient_light ambient_light_flag barometric_pressure barometric_pressure_flag discomfort_index discomfort_index_flag eCO2 eTVOC eco2_flag etvoc_flag heat_stroke heat_stroke_flag macaddr pga    pga_flag relative_humidity relative_humidity_flag seismic_intensity seismic_intensity_flag si_value si_value_flag sound_noise sound_noise_flag temperature temperature_flag time_measured       vibration_information
----                ------------- ------------------ ------------------- ------------------------ ---------------- --------------------- ---- ----- --------- ---------- ----------- ---------------- ------- ---    -------- ----------------- ---------------------- ----------------- ---------------------- -------- ------------- ----------- ---------------- ----------- ---------------- -------------       ---------------------
1717723752415425000 643           0                  996.274             0                        72.01            0                     1458 161   0         0          23.67       0                sensor  0.0    0        81.56             0                      0.0               0                      0.0      0             76.73       0                23.11       0                2024/06/07 10:29:12 0
1717723753535021000 643           0                  996.276             0                        72.01            0                     1436 157   0         0          23.67       0                sensor  0.0    0        81.56             0                      0.0               0                      0.0      0             83.02       0                23.11       0                2024/06/07 10:29:13 0
1717723754639275000 646           0                  996.274             0                        72.0             0                     1436 157   0         0          23.67       0                sensor  0.0    0        81.61             0                      0.0               0                      0.0      0             59.96       0                23.1        0                2024/06/07 10:29:14 0
1717723755743010000 643           0                  996.272             0                        72.0             0                     1436 157   0         0          23.67       0                sensor  0.0    0        81.61             0                      0.0               0                      0.0      0             61.66       0                23.1        0                2024/06/07 10:29:15 0
1717723756887958000 643           0                  996.27              0                        72.0             0                     1436 157   0         0          23.67       0                sensor  0.0    0        81.61             0                      0.0               0                      0.0      0             63.31       0                23.1        0                2024/06/07 10:29:16 0
1717723757991907000 648           0                  996.271             0                        71.97            0                     1436 157   0         0          23.65       0                sensor  0.0    0        81.61             0                      0.0               0                      0.0      0             64.56       0                23.08       0                2024/06/07 10:29:17 0
1717723759099233000 637           0                  996.273             0                        71.96            0                     1436 157   0         0          23.65       0                sensor  0.0    0        81.56             0                      0.0               0                      0.0      0             74.84       0                23.08       0                2024/06/07 10:29:19 0
1717723760203840000 643           0                  996.276             0                        71.94            0                     1436 157   0         0          23.64       0                sensor  0.0    0        81.65             0                      0.0               0                      0.0      0             63.96       0                23.06       0                2024/06/07 10:29:20 0
1717723761309080000 643           0                  996.274             0                        71.91            0                     1436 157   0         0          23.62       0                sensor  0.0    0        81.69             0                      0.0               0                      0.0      0             64.44       0                23.04       0                2024/06/07 10:29:21 0
1717723762416402000 648           0                  996.27              0                        71.91            0                     1436 157   0         0          23.62       0                sensor  0.0    0        81.69             0                      0.0               0                      0.0      0             62.12       0                23.04       0                2024/06/07 10:29:22 0
1717723763520693000 650           0                  996.271             0                        71.91            0                     1436 157   0         0          23.62       0                sensor  0.0    0        81.68             0                      0.0               0                      0.0      0             61.3        0                23.04       0                2024/06/07 10:29:23 0
1717723764625517000 648           0                  996.27              0                        71.92            0                     1458 161   0         0          23.63       0                sensor  0.0    0        81.64             0                      0.0               0                      0.0      0             63.11       0                23.05       0                2024/06/07 10:29:24 0

(以下省略)

データは、うまく登録されているようなので次のGrafanaのインストールと設定に続きます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?