コンテンツにスキップ

tinkerforgeで温度をはかる

概要

ラズパイにtinkerforgeセンサーを接続し、node-exporter でデータ取得する。

ラズパイに以下をインストール

node-exporter (text collecter)
tinkerforge brickd
tinkerforge shell bindings
bc

install

node-exporter

apt install prometheus-node-exporter-collectors

tinkerforge brickd

wget https://download.tinkerforge.com/tools/brickd/linux/brickd_linux_latest_armhf.deb
dpkg -i brickd_linux_latest_armhf.deb

tinkerforge shell bindings

mkdir tinkerforge
cd tinkerforge
wget https://download.tinkerforge.com/bindings/shell/tinkerforge_shell_bindings_latest.zip
unzip tinkerforge_shell_bindings_latest.zip

bc

apt install bc

スクリプト設置

スクリプト作成

vi get-tinkerforge.sh
内容
#!/bin/bash

TF_PATH=/root/tinkerforge
TF_CALL="${TF_PATH}/tinkerforge call"
PRM_PATH=/var/lib/prometheus/node-exporter
TMP_FILE=$PRM_PATH/tinkerforge.prom.tmp
PRM_FILE=$PRM_PATH/tinkerforge.prom

### target sensors ###
air_quality_bricklet=""
temperature_bricklet=""
humidity_bricklet=""
ambient_light_v2_bricklet=""
dust_detector_bricklet=""

### start
echo "# tinkerfoge sensor values" | tee $TMP_FILE

### air-quality-bricklet ###
if [[ $air_quality_bricklet = "" ]] ; then
  echo "air-quality-bricklet is n/a"
else
  TFID="$air_quality_bricklet"
  #temperture
  OUTPUT=`$TF_CALL air-quality-bricklet $TFID get-temperature`
  ITEM=tinkerforge_`echo $OUTPUT | cut -d"=" -f1`
  VALUE=`echo $OUTPUT | cut -d"=" -f2`
  VALUE=`bc <<< "scale=2; $VALUE/100"`
  echo $ITEM $VALUE | tee -a $TMP_FILE

  #humidity
  OUTPUT=`$TF_CALL air-quality-bricklet $TFID get-humidity`
  ITEM=tinkerforge_`echo $OUTPUT | cut -d"=" -f1`
  VALUE=`echo $OUTPUT | cut -d"=" -f2`
  VALUE=`bc <<< "scale=2; $VALUE/100"`
  echo $ITEM $VALUE | tee -a $TMP_FILE

  #air-pressure
  OUTPUT=`$TF_CALL air-quality-bricklet $TFID get-air-pressure`
  #ITEM=tinkerforge_`echo $OUTPUT | cut -d"=" -f1`
  ITEM=tinkerforge_air_pressure
  VALUE=`echo $OUTPUT | cut -d"=" -f2`
  VALUE=`bc <<< "scale=2; $VALUE/100"`
  echo $ITEM $VALUE | tee -a $TMP_FILE
fi

### temperature-bricklet ###
if [[ $temperature_bricklet = "" ]] ; then
  echo "temperature-bricklet is n/a"
else
  TFID=$temperature_bricklet
  OUTPUT=`$TF_CALL temperature-bricklet $TFID get-temperature`
  ITEM=tinkerforge_`echo $OUTPUT | cut -d"=" -f1`
  VALUE=`echo $OUTPUT | cut -d"=" -f2`
  VALUE=`bc <<< "scale=2; $VALUE/100"`
  echo $ITEM $VALUE | tee -a $TMP_FILE
fi

### humidity-bricklet ###
if [[ $humidity_bricklet = "" ]] ; then
  echo "humidity-bricklet is n/a"
else
  TFID=$humidity_bricklet
  OUTPUT=`$TF_CALL humidity-bricklet $TFID get-humidity`
  ITEM=tinkerforge_`echo $OUTPUT | cut -d"=" -f1`
  VALUE=`echo $OUTPUT | cut -d"=" -f2`
  VALUE=`bc <<< "scale=2; $VALUE/100"`
  echo $ITEM $VALUE | tee -a $TMP_FILE
fi

### ambient-light-v2-bricklet ###
if [[ $ambient_light_v2_bricklet = "" ]] ; then
  echo "ambient-light-v2-bricklet is n/a"
else
  TFID=$ambient_light_v2_bricklet
  OUTPUT=`$TF_CALL ambient-light-v2-bricklet $TFID get-illuminance`
  ITEM=tinkerforge_`echo $OUTPUT | cut -d"=" -f1`
  VALUE=`echo $OUTPUT | cut -d"=" -f2`
  VALUE=`bc <<< "scale=2; $VALUE/100"`
  echo $ITEM $VALUE | tee -a $TMP_FILE
fi

### dust-detector-bricklet ###
if [[ $dust_detector_bricklet = "" ]] ; then
  echo "dust-detector-bricklet is n/a"
else
  TFID=$dust_detector_bricklet
  OUTPUT=`$TF_CALL dust-detector-bricklet $TFID get-dust-density`
  ITEM=tinkerforge_dust_density
  VALUE=`echo $OUTPUT | cut -d"=" -f2`
  echo $ITEM $VALUE | tee -a $TMP_FILE
fi

mv $TMP_FILE $PRM_FILE

動作確認

chmod 755 get-tinkerforge.sh
./get-tinkerforge.sh

以下に保存されていることを確認

ls -l /var/lib/prometheus/node-exporter/
cat /var/lib/prometheus/node-exporter/tinkerforge.prom
出力例
tinkerforge_temperature 23.31
tinkerforge_humidity 36.57
tinkerforge_air_pressure 1014.67
tinkerforge_illuminance 61.47
tinkerforge_dust_density 25

crontab

crontab -e
### tinkerforge-exporter ###
* * * * * /root/tinkerforge/get-tinkerforge.sh

prometheus 設定

prometheus.yaml に追記する

  - job_name: node
    # If prometheus-node-exporter is installed, grab stats about the local
    # machine by default.
    static_configs:
    - targets:
      - localhost:9100
      - 192.168.0.50:9100 ★

grafana 設定

grafana-tinkerforge.json をインポートする

参考

https://github.com/tkhskh/tinkerforge-expoter

広告

Amazon.co.jp : 二酸化炭素センサー mh-z19
メルカリ:ラズパイ