DS18B20 をRaspberry Pi 3 a で使う

買ったもの

配線

最初のうまくいかないとき

root@raspberrypi:~# lsmod | grep w1
w1_gpio 16384 0
wire 36864 1 w1_gpio

ここからの試行錯誤

この中のどれがキーだったのか分からないが、一応うまくいきました。

root@raspberrypi:~# modprobe w1-gpio
root@raspberrypi:~# modprobe w1-therm
root@raspberrypi:~# shutdown -r now

root@raspberrypi:~# vi /etc/modules

# 1-wire
w1-gpio
w1-therm
を追記

成功

認識された。
root@raspberrypi:/sys/bus/w1/devices/w1_bus_master1# lsmod | grep w1
w1_therm 28672 0
w1_gpio 16384 0
wire 36864 2 w1_gpio,w1_therm

root@raspberrypi:/sys/bus/w1/devices/28-3c01f095a090# cat w1_slave
8b 01 55 05 7f a5 81 66 f5 : crc=f5 YES
8b 01 55 05 7f a5 81 66 f5 t=24687

Pythonコード

こちらのコードを使わせて頂きました。

$ cat ds18b20.py
import sys
import subprocess

SENSOR_ID = “28-3c01f095a090”
SENSOR_W1_SLAVE = “/sys/bus/w1/devices/” + SENSOR_ID + “/w1_slave”
ERR_VAL = 85000

def main():
res = get_water_temp()
if res is not None:
temp_val = res.split(“=”)
if temp_val[-1] == ERR_VAL:
print “Got value:85000. Circuit is ok, but there’s something wrong”
sys.exit(1)

temp_val = round(float(temp_val[-1]) / 1000, 1)
print temp_val
else:
print “cannot read the value.”
sys.exit(1)

def get_water_temp():
try:
print SENSOR_W1_SLAVE
res = subprocess.check_output([“cat”, SENSOR_W1_SLAVE])

return res
except:
print “Response is none”
return None

if __name__ == “__main__”:
main()

結果

指先ではあまり温度は上がらず、脇下で上がりました。

$ python ds18b20.py
/sys/bus/w1/devices/28-3c01f095a090/w1_slave
24.2

$ python ds18b20.py
/sys/bus/w1/devices/28-3c01f095a090/w1_slave
34.4

シェアする

  • このエントリーをはてなブックマークに追加

フォローする