こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。
今回は、Raspberry Pi(zero w)でデジタル温度センサー DS18B20+を使って、温度測定を行います。
デジタル温度センサー DS18B20+
センサーは、秋葉原の秋月電子でゲットしました。(約300円)
デジタル温度センサ(1wire)DS18B20+: センサ一般 秋月電子通商-電子部品・ネット通販
http://akizukidenshi.com/download/ds/maxim/DS18B20.pdf
1-Wireという規格で非常に簡単にデータのやり取りができるデバイスです。
Raspberry Pi(Raspbian)
Raspberry Pi zero wで動作しているRaspbianに1-wireデバイスを使うための設定を行います。
使用したRaspbianは、以下の通り。
8.0
/etc/modulesへ以下を追記。
なお、/etc/modulesは、起動時に読み込むカーネルモジュールを記載するファイルです。
Ubuntu Manpage: /etc/modules - kernel modules to load at boot time
pi@raspberrypi:~ $ vi /etc/modules
pi@raspberrypi:~ $ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, opi@raspberrypi:~ $ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
i2c-dev
w1-gpio
w1-thermne per line. Lines beginning with "#" are ignored.
i2c-dev
w1-gpio ・・・・この2行を追加
w1-therm
続いて、/boot/config.txtへ以下を追記。
pi@raspberrypi:~ $ vi /boot/config.txt
pi@raspberrypi:~ $ cat /boot/config.txt
# For more options and information see
# http://www.raspberrypi.org/documentation/configuration/config-txt.md
# Some settings may impact device functionality. See link above for details
# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1
# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1
# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16
# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720
# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1
# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1
# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2
# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4
# uncomment for composite PAL
#sdtv_mode=2
#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800
# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on
# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi
# Additional overlays and parameters are documented /boot/overlays/README
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
# NOOBS Auto-generated Settings:
hdmi_force_hotplug=1
# for W1-GPIO
dtoverlay=w1-gpio-pullup,gpiopin=4
参考リンク:1-WIRE at Raspberry Pi GPIO Pinout
その後、設定を有効化するため再起動します。
配線
以下のようにRaspberry Pi・DS18B20+を接続します。
※プラスとマイナスが逆になると、センサーがむちゃくちゃ熱くなるので注意。
必要なもの
・Raspberry Pi (zero w)
・DS18B20+(デジタル温度センサー)
・ブレッドボード
・ジャンパーコード(オスーメス)
・抵抗(4.7kΩ)
温度を確認
DS18B20+で測定する温度情報は、「/sys/devices/w1_bus_master1/28-XXXXXXXXXXXX/w1_slave」というファイルに出力されます。
34 01 4b 46 7f ff 0c 10 1c : crc=1c YES
34 01 4b 46 7f ff 0c 10 1c t=19250 ・・・ここが温度、19.250℃を示している
おまけ
取得した温度を監視したい場合は、以下の記事にあるLINE notifyとCron(Crontab)を組み合わせるなどの方法があります。
LINE notify による通知連携 - Opensourcetechブログ
以下、サンプルです。
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 * * * * curl https://notify-api.line.me/api/notify -X POST -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -F "message=`grep t= /sys/devices/w1_bus_master1/28-0000068d4e4e/w1_slave | cut -d "=" -f 2`"
以下のスクリプトにすると、もうちょっといい感じに取れます。
https://github.com/kujiraitakahiro/RaspberryPi/blob/master/DHT11.sh