DS18B20(1-wire通信)は簡単に 複数本使用できます。
$ sudo apt-get install vim
$ sudo vim /boot/config.txt
dtoverlay=w1-gpio,gpiopin=4,pullup=y
$ reboot
$ lsmod | grep w1
w1_therm 28672 0
w1_gpio 16384 0
wire 36864 2 w1_gpio,w1_therm
$ sudo shutdown -h now
$ ls /sys/bus/w1/devices/
28-3c01b556b5e5 w1_bus_master1
$ cat /sys/bus/w1/devices/28-3c01b556b5e5/w1_slave
66 01 55 05 7f a5 81 66 fa : crc=fa YES
66 01 55 05 7f a5 81 66 fa t=22375
$ mkdir temp_rec
$ cd temp_rec
$ npm init
pi@raspberrypi:~/temp_rec $ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install ` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (temp_rec)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
Is this OK? (yes)
const fs = require('fs');
let data_th = [];
let temp = () => {
let buff = fs.readFileSync('/sys/bus/w1/devices/28-3c01b556b5e5/w1_slave'); // ファイルの読み込み
let data_sp = buff.toString().split("="); // =のところで分割する
data_th.push(data_sp.pop()); //pop()で後部を切り取りpush()で配列に追加
let data_dig = (data_th.map(Number))/100; // Numberで数値変換して/100で桁合わせ
data_th = []; //リセット
let data_rou = (Math.round(data_dig))/10; // roundで丸めた後少数点第1位にする
return data_rou; // 整えた測定値を返す
}
console.log(temp()); // ターミナルに測定値の表示
$ node app.js
pi@raspberrypi:~/temp_rec $ node app.js
21.4
Copyrights © 2019 - Imran Hossain, All Rights Reserved.