あっきぃ日誌

鉄道ブログのような技術系ブログのようななにか

ファッキンホット温度計で直接センサーの値を出す

前回のエントリーで書いた温度計は会社で常時稼働を始めました。サーバールームの室温が常に把握できていい感じです。

で、これはZabbixに記録されている値を出すものだったので、今度は温度計のRaspberry Piに接続されたDS18B20温度センサーの値を出すようにしてみました。つまり自席の室温も見たいというわけです。

f:id:Akkiesoft:20150722193009j:plain:h450

1-Wireに使えるGPIO4はすでに埋まっているので、なんかうまい方法をと思っていましたが、そういえばDeviceTreeの設定の説明(/boot/overlays/README)になんか書いてあったなと思い返しました。抜粋。

File:   w1-gpio-overlay.dtb
Info:   Configures the w1-gpio Onewire interface module.
        Use this overlay if you *don't* need a GPIO to drive an external pullup.
Load:   dtoverlay=w1-gpio,<param>=<val>
Params: gpiopin                  GPIO for I/O (default "4")

        pullup                   Non-zero, "on", or "y" to enable the parasitic
                                 power (2-wire, power-on-data) feature

gpiopinの値を変えたらなんか使えそうなので、/boot/config.txtに以下の一文を追記しました。

dtoverlay=w1-gpio,gpiopin=26

で、スクリプトにDS18B20周りの実装を追加。

電源に使える3VピンもLCDのボードで全部隠されているので、26番ピンのとなりの19番ピンを出力にして対処しました(雑)。

室温データの取得を行うread_temp()は、「RaspberryPi - Raspberry Piで1-Wireデジタル温度センサのDS18B20をPythonから使う - Qiita」のコードを参考に、いらない部分をガリガリ削りました。

(以下は、前回のエントリーのGistのスクリプトにいい感じに書き足す)
import subprocess
import Adafruit_GPIO as GPIO

# DS18B20
ds18b20 = "/sys/bus/w1/devices/28-00000xxxxxxx/w1_slave"
# Power
gpio=GPIO.get_platform_gpio()
gpio.setup(19, GPIO.OUT)
gpio.set_high(19)
# Data
def read_temp_raw():
	catdata = subprocess.Popen(['cat',ds18b20], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	out,err = catdata.communicate()
	out_decode = out.decode('utf-8')
	lines = out_decode.split('\n')
	return lines

def read_temp():
	lines = read_temp_raw()
	while lines[0].strip()[-3:] != 'YES':
		time.sleep(0.2)
		lines = read_temp_raw()
	equals_pos = lines[1].find('t=')
	if equals_pos != -1:
		temp_string = lines[1][equals_pos+2:]
		temp = float(temp_string) / 1000.0
		return round(temp, 2)

あとは表示を付け足して、冒頭の写真のとおりとなりました。良いですね。

というわけで、1-WireのGPIOピンはいざというときは変更して違うピンで使える、という話でした。