센서로 삶의 혼란을 시각화해 보았다 (RaspberryPi + fluentd + Elasticsearch + Kibana)

1. 소개



사생활을 가시화하는 것으로, 생활의 혼란의 개선에 연결하고 싶습니다.
방에 설치한 센서로부터 얻은 데이터(빛, 소리, 온도)를 Kibana로 대시보드화했습니다.
  • 쓴 것 : 데이터 취득용 스크립트나, 설정 파일의 내용 (얼룩)
  • 작성하지 않음 : 설치 방법

  • 2. 전체 구성





    3. Sensor side



    3-1. 환경



    본체를 TV의 그림자에 숨깁니다.

  • 하드웨어
  • Raspberry Pi 3 ModelB
  • GrovePi+
  • Grove - Light Sensor
  • Grove - Sound Sensor
  • Grove - Temperature Sensor V1.2

  • OS
  • RASPBIAN STRETCH LITE 9.1

  • fluentd 0.12.40

  • 3-2. 센서 데이터 취득



    센서 데이터를 읽을 스크립트를 준비합니다.

    myroom.py
    #!/usr/bin/python -u
    
    from datetime import datetime
    import numpy
    import time
    import grovepi
    import math
    import json
    
    light_sensor = 0  # A0
    sound_sensor = 1  # A1
    temp_sensor  = 2  # A2
    
    light_sensor_samples = 10
    sound_sensor_samples = 5000
    temp_sensor_samples  = 10
    temp_sensor_version  = "1.2"
    
    def get_light_level(analog_sensor):
        samples = []
        for i in range(light_sensor_samples):
            samples.append(grovepi.analogRead(analog_sensor))
        return numpy.median(samples)
    
    def get_sound_level(analog_sensor):
        samples = []
        for i in range(sound_sensor_samples):
            samples.append(grovepi.analogRead(analog_sensor))
        return max(samples)
    
    def get_temp_level(analog_sensor, version):
        samples = []
        for i in range(temp_sensor_samples):
            samples.append(grovepi.temp(analog_sensor, version))
        return numpy.median(samples)
    
    if __name__ == '__main__':
        grovepi.pinMode(light_sensor,"INPUT")
        grovepi.pinMode(sound_sensor,"INPUT")
        grovepi.pinMode(temp_sensor,"INPUT")
    
        while True:
            light = get_light_level(light_sensor)
            sound = get_sound_level(sound_sensor)
            temp  = get_temp_level(temp_sensor, temp_sensor_version)
            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            date = datetime.now().strftime("%Y-%m-%d")
            hour = int(datetime.now().strftime("%H"))
            wday = int(datetime.now().weekday())
    
            json_data = {
                    "timestamp": timestamp,
                    "hour": hour,
                    "wday": wday,
                    "light": light,
                    "sound": sound,
                    "temp": temp
                    }
            encode_json_data = json.dumps(json_data)
            print encode_json_data
    

    위의 스크립트를 실행하여 표준 출력을 파일로 내보냅니다.
    sudo python -u ./myroom.py >> /home/pi/myroom.log &
    

    출력은 다음과 같습니다.
    $ tail /home/pi/myroom.log
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:18:37", "light": 566.0, "wday": 2}
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:19:10", "light": 554.0, "wday": 2}
    {"sound": 375, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:19:43", "light": 564.0, "wday": 2}
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:20:16", "light": 554.0, "wday": 2}
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:20:48", "light": 566.5, "wday": 2}
    {"sound": 259, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:21:21", "light": 557.5, "wday": 2}
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:21:54", "light": 568.0, "wday": 2}
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:22:27", "light": 555.0, "wday": 2}
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:22:59", "light": 555.0, "wday": 2}
    {"sound": 255, "hour": 1, "temp": 18.82680514639611, "timestamp": "2017-10-18 01:23:32", "light": 552.0, "wday": 2}
    

    3-3. fluentd 설정



    fluentd 구성 파일을 준비합니다.
    위에서 출력한 데이터의 증분을 Server side의 fluentd로 forward합니다.

    /home/pi/fluent/fluent.conf
    <source>
      @type tail
      format json
      path /home/pi/myroom.log
      pos_file /home/pi/myroom.log.pos
      tag log.myroom
    </source>
    
    <match log.myroom>
      @type forward
      buffer_type file
      buffer_path /tmp/testlog-tmp.log
      <server>
        host [ホスト名]
        port 24224
      </server>
    </match>
    

    4. Server side



    4-1. 환경


  • 사쿠라 VPS 1GB 플랜
  • CentOS 7.4
  • fluentd(td-agent) 0.12.40
  • Elasticsearch 5.6.2
  • Kibana 5.6.2

  • 4-2. fluentd(td-agent) 설정



    Sensor side에서 전송된 데이터를 Elasticsearch에 저장합니다.

    /etc/td-agent/td-agent.conf
    <source>
      type forward
      port 24224
      bind 0.0.0.0
    </source>
    
    <match log.myroom>
      type elasticsearch
      host localhost
      port 9200
      index_name myroom
      logstash_format true
      logstash_prefix myroom
    </match>
    

    4-3. Elasticsearch 설정



    거의 기본값으로 사용하고 있습니다.

    /etc/elasticsearch/elasticsearch.yml
    (空)
    

    4-4. Kibana 설정



    액세스 소스 IP를 제한하지 않도록 설정합니다.

    /etc/kibana/kibana.yml
    server.host: "0.0.0.0"
    

    5. 완성된 대시보드



    사생활의 혼란이 시각화되었습니다.


    설명을 생략하고 있습니다만, 별도 인증을 넣고 있습니다. 공개하면 방범상 좋지 않으므로 조심합시다.

    좋은 웹페이지 즐겨찾기