材料


  • raspberrypi3
  • 温度センサ(ケーブル付き) : DS18B20 1個
  • ジャンパー線メスオス又はGPIO40ピンケーブル
  • 端子台
  • 抵抗器 : 4.7KΩ 1個

  • ラズパイの設定

    
            w1_therm               28672  0
            w1_gpio                16384  0
            wire                   36864  2 w1_gpio,w1_therm
    クリックで画像拡大

    結線図


    結線の確認


    電源を入れる

    
            28-3c01b556b5e5  w1_bus_master1

    温度測定値の取得

    
            66 01 55 05 7f a5 81 66 fa : crc=fa YES
            66 01 55 05 7f a5 81 66 fa t=22375

    node.jsの準備

    
    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)

    コード

    
    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.jsで温度測定値表示

    
            pi@raspberrypi:~/temp_rec $ node app.js
            21.4
    次の記事