Node-REDのHowTo(その2)

Node-REDのメモ GPIO & I2C編

Node-REDでGPIO操作をするときの設定手順のメモ。
鶏頭で忘れっぽいのでメモ。

Node-REDはRaspberryPiで起動済みでブラウザで接続済みであるものとします。

RaspberryPiでGPIO出力

フローの例


[
    {
        "id": "51e0a206.805964",
        "type": "tab",
        "label": "GPIO_OUT",
        "disabled": false,
        "info": ""
    },
    {
        "id": "597df548.0311f4",
        "type": "rpi-gpio out",
        "z": "51e0a206.805964",
        "name": "LED_0",
        "pin": "18",
        "set": true,
        "level": "0",
        "freq": "",
        "out": "out",
        "x": 550,
        "y": 80,
        "wires": []
    },
    {
        "id": "89834ee2.89e27",
        "type": "inject",
        "z": "51e0a206.805964",
        "name": "",
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 190,
        "y": 80,
        "wires": [
            [
                "597df548.0311f4"
            ]
        ]
    },
    {
        "id": "8b020b32.e26818",
        "type": "inject",
        "z": "51e0a206.805964",
        "name": "",
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 190,
        "y": 120,
        "wires": [
            [
                "597df548.0311f4"
            ]
        ]
    },
    {
        "id": "21621e60.e300ba",
        "type": "rpi-gpio out",
        "z": "51e0a206.805964",
        "name": "LED_1",
        "pin": "22",
        "set": true,
        "level": "0",
        "freq": "",
        "out": "out",
        "x": 550,
        "y": 180,
        "wires": []
    },
    {
        "id": "87abb89b.307418",
        "type": "inject",
        "z": "51e0a206.805964",
        "name": "",
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 190,
        "y": 180,
        "wires": [
            [
                "21621e60.e300ba"
            ]
        ]
    },
    {
        "id": "a7ae7810.959a88",
        "type": "inject",
        "z": "51e0a206.805964",
        "name": "",
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 190,
        "y": 220,
        "wires": [
            [
                "21621e60.e300ba"
            ]
        ]
    }
]

RaspberryPiでGPIO入力

フローの例


[
    {
        "id": "1385085c.ab8648",
        "type": "tab",
        "label": "GPIO_IN",
        "disabled": false,
        "info": ""
    },
    {
        "id": "e9177a48.70b74",
        "type": "rpi-gpio in",
        "z": "1385085c.ab8648",
        "name": "SW0",
        "pin": "15",
        "intype": "up",
        "debounce": "25",
        "read": false,
        "x": 150,
        "y": 100,
        "wires": [
            [
                "932b0e92.05cdd8"
            ]
        ]
    },
    {
        "id": "932b0e92.05cdd8",
        "type": "debug",
        "z": "1385085c.ab8648",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 470,
        "y": 100,
        "wires": []
    },
    {
        "id": "8f31f20e.4b6d28",
        "type": "rpi-gpio in",
        "z": "1385085c.ab8648",
        "name": "SW1",
        "pin": "16",
        "intype": "up",
        "debounce": "25",
        "read": false,
        "x": 150,
        "y": 160,
        "wires": [
            [
                "d27c860d.7de3a8"
            ]
        ]
    },
    {
        "id": "d27c860d.7de3a8",
        "type": "debug",
        "z": "1385085c.ab8648",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 470,
        "y": 160,
        "wires": []
    }
]

RaspberryPiでI2Cを使用する

事前準備

以下はターミナルやコンソールでの作業

sudo raspi-config
    5 Interfacing Options
        P5 I2C
            Would you like the ARM I2C interface to be enabled?
            に対して<はい>を選択
            The ARM I2C interface is enabled
            と表示されるので<了解>
    <Finish>
sudo apt install i2c-tools
i2cdetect -y 1
#   ↓結果(例)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
# 76がBME280(Bosch温湿度センサ)
i2cget -y 1 0x76 0xd0
# ↓ 結果
0x60
# レジスタ 0xd0(CHIP ID)をリードするとデバイスのID 0x60が読める

BME280用ノードをインストールする

[!WARNING] 2019/09/13現在、何やらインストール時にエラーになるが、
モジュールのコンパイル時にwarning/noteが出ているだけのようなので、インストール自体はできているようだ。
とりあえず下記サンプルは動いているので、大丈夫でしょう。

BME280を使用するフローを作成する

変数名 値の例 項目
msg.topic “bme280” ノードの設定で設定したTopic
msg.payload.temperature_C 34.23 温度
msg.payload.humidity 54.402349427117336 湿度
msg.payload.pressure_hPa 1013.9016246356634 気圧
msg.payload.model “BME280” センサ名

フローの例

[
    {
        "id": "e4065603.3c6dc",
        "type": "tab",
        "label": "BME280",
        "disabled": false,
        "info": ""
    },
    {
        "id": "c0602bca.110b8",
        "type": "Bme280",
        "z": "e4065603.3c6dc",
        "name": "",
        "bus": "1",
        "address": "0x76",
        "topic": "bme280",
        "extra": false,
        "x": 300,
        "y": 100,
        "wires": [
            [
                "8b845ce5.ec2fd"
            ]
        ]
    },
    {
        "id": "8b845ce5.ec2fd",
        "type": "debug",
        "z": "e4065603.3c6dc",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 630,
        "y": 100,
        "wires": []
    },
    {
        "id": "cdbf55ce.3aa94",
        "type": "inject",
        "z": "e4065603.3c6dc",
        "name": "",
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 100,
        "wires": [
            [
                "c0602bca.110b8"
            ]
        ]
    }
]