enebularとObnizを使ってWebsocketからモータや電磁石を動かしてみる

# まえがき
Node-REDを使ってObnizからモノを動かす話が少なそうに見えたこともあり、備忘録を兼ねて書いてみる。

# やること
Node-REDからObnizを操作して、モータとか電磁石を動かす。

# 用意するものとか前提条件とか
- Obniz Board(今は亡きBasic Licenseを使っていますが、他サービスを介せるライセンスならOKなはず)
- DCモータ（aitendoとかに売っている黄色いギアードモータでもOK）
- 電磁石(私はAliで仕入れましたが、国内でもルートはありそう。https://ja.aliexpress.com/item/32815809960.html)
- ジャンパとかブレッドボードとか
- モバイルバッテリーなどの電源とmicroUSBケーブル
- enebularのアカウント(enebularの使い方は本家tutorial等でお願いしますm(__)m)

# モノに関する準備
写真のようにモータあるいは電磁石とObnizの0,1ピンに差し込むだけでOK
![P_20191208_195745_vHDR_On.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/271381/a16dac38-b09d-87f7-24ae-d98952e8f6dd.jpeg)

モータに関してはバッドノウハウ感がありますがゆるしてください...orz
![P_20191208_202406_vHDR_On.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/271381/b1b1f177-516f-fed7-fd09-6280c0b9151d.jpeg)

![P_20191208_194034_vHDR_On.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/271381/c0e9ff54-33d3-f9d1-6451-bfcd5e522d63.jpeg)

![P_20191208_202742_vHDR_On (1).jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/271381/ee3ae438-ac29-9ce5-75e6-733ddf348dcb.jpeg)

# Node-REDに関する話
Obnizノードを取り込んで、フローを"読み込み"→"クリップボード"にて読み込めばOK
ただしObnizIDは各自のものをいれること。

![Obnizノードの設定.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/271381/cb9da9a9-93ba-134a-b442-ea730108548c.png)


```json:flow.json
[{"id":"633ac2dc.6dd72c","type":"template","z":"9834e454.6fb268","name":"actuator MAX","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 0,\n            \"freq\": 1000,\n             \"pulse\": 1\n        }\n    },{\n        \"io1\": false\n    }\n]","output":"json","x":517,"y":170,"wires":[["b3090c0f.f44ce"]]},{"id":"5f851bd4.5a6bc4","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":319,"y":171,"wires":[["633ac2dc.6dd72c"]]},{"id":"1d6c22d3.82926d","type":"template","z":"9834e454.6fb268","name":"actuator OFF","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 0,\n            \"freq\": 1000,\n             \"pulse\": 0\n        }\n    },{\n        \"io1\": false\n    }\n]","output":"json","x":509,"y":288,"wires":[["b3090c0f.f44ce"]]},{"id":"8cf73cc4.ddb89","type":"template","z":"9834e454.6fb268","name":"actuator half","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 0,\n            \"freq\": 1000,\n             \"pulse\": 0.5\n        }\n    },{\n        \"io1\": false\n    }\n]","output":"json","x":516,"y":229,"wires":[["b3090c0f.f44ce"]]},{"id":"d90360ef.25f91","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":318,"y":230,"wires":[["8cf73cc4.ddb89"]]},{"id":"f4a79ca.ca2e86","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":314,"y":289,"wires":[["1d6c22d3.82926d"]]},{"id":"14e282fd.4301dd","type":"template","z":"9834e454.6fb268","name":"actuator MAX(rev)","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 1,\n            \"freq\": 1000,\n             \"pulse\": 1\n        }\n    },{\n        \"io0\": false\n    }\n]","output":"json","x":518,"y":399,"wires":[["b3090c0f.f44ce"]]},{"id":"efc11452.8914b8","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":310,"y":400,"wires":[["14e282fd.4301dd"]]},{"id":"afdc712d.3af71","type":"template","z":"9834e454.6fb268","name":"actuatorhalf(rev)","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n    {\n        \"pwm0\": {\n            \"io\": 1,\n            \"freq\": 1000,\n             \"pulse\": 0.5\n        }\n    },{\n        \"io0\": false\n    }\n]","output":"json","x":517,"y":458,"wires":[["b3090c0f.f44ce"]]},{"id":"11e0a0d1.9819ff","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":309,"y":459,"wires":[["afdc712d.3af71"]]},{"id":"b76c0cf7.83d65","type":"template","z":"9834e454.6fb268","name":"reset","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"[\n\n    {\n        \"system\": {\n            \"reset\": true\n        }\n    },{\n        \"display\": {\n            \"clear\": true\n        }\n    }\n\n]","output":"json","x":510,"y":100,"wires":[["b3090c0f.f44ce"]]},{"id":"d754e73d.101408","type":"inject","z":"9834e454.6fb268","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":342,"y":95,"wires":[["b76c0cf7.83d65"]]},{"id":"b3090c0f.f44ce","type":"obniz","z":"9834e454.6fb268","obnizId":"XXXX-XXXX","x":839,"y":173,"wires":[]}]
```

読み込むとこんな感じ。actuator MAXで出力最大でhalfで1/2の出力、OFFで停止する。
あと、正方向から逆方向に出力する際にピンのアサインをかえるため、リセットボタンを用意した。
![フロー.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/271381/68be6d81-fc9d-f81d-73e9-04b7e35cddcb.png)

フローの中身を見ると、PWMで出力変えているだけだったりします。

```json:actuator_max
[
    {
        "pwm0": {
            "io": 0,
            "freq": 1000,
             "pulse": 1
        }
    },{
        "io1": false
    }
]
```

リセットはこんな感じ。displayの初期化はピンアサイン表示が切り替わる対策として
入れてるだけ。

```json:reset
[

    {
        "system": {
            "reset": true
        }
    },{
        "display": {
            "clear": true
        }
    }

]
```

# 動かしてみる。
enebular上でデプロイしたのちに、actuator maxにつながったtimestanpからinjectをすると、モータが回ったり電磁石に金属がくっつくはず。そのあとにreset側でinjectを流すとモータが止まったり、電磁石からくっついていたものが落ちたりする。
ちなみに、reset側でinjectを実施した後に(rev)を付与したノードにinjectを流すと逆方向に動く。

![P_20191208_195554_vHDR_On.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/271381/3cee9e54-dd44-efd2-a197-89cace2fecb8.jpeg)

# あとがき
センサの値をフィードバックして何かしてみたいなぁ