LoginSignup
6
8

More than 5 years have passed since last update.

ArduinoとRaspberryPiをUSBケーブルで接続してNode-REDで値を読み込む

Last updated at Posted at 2018-04-14

概要

RaspberryPiとArduinoをUSBケーブルで接続し、Arduinoから出力される値をNode-REDで確認するだけで、完全に自分用の備忘録です。

前提条件

  • Arduino UNO
  • Raspberry Pi 2 Model B
  • (Arduino IDE をインストールした)端末

参考サイト

Arduinoの設定

Arduino と Arduino IDE をインストールした端末をUSBで接続して、Arduino IDEを起動し、以下のスケッチを書き込みます。

int i ;
void setup() {
     Serial.begin(9600) ;
     i = 0 ;
}
void loop() {
     i++ ;
     Serial.println(i) ;
     delay(1000) ;
}

シリアルモニタを起動すると、1秒ごとに数値が出力されます。

シリアルモニタ
1
2
3
4

Arduino と RaspberryPi の接続

次に Arduino と Raspberry Pi をUSBで接続します。以下、Raspberry Pi で操作します。

$ sudo apt-get update
$ sudo apt-get upgrade

$ sudo apt-get install cu # Arduinoと通信するためのcuコマンドをインストール

$ lsusb
Bus 001 Device 006: ID 2341:0043 Arduino SA Uno R3 (CDC ACM) # Arduinoが認識されていることを確認
$ dmesg
・・・
[  501.638776] cdc_acm 1-1.5:1.0: ttyACM0: USB ACM device # Arduinoが認識されていることを確認
・・・

準備ができたら接続。

$ cu -l /dev/ttyACM0 -s 9600
Connected.
1
2
3
4
~
[raspberrypi].
Disconnected.

シリアルモニタ同様に、1秒ごとに数値が出力されれば成功!!
~(チルダ).(ピリオド)で終了。反応が悪かったらUSBを抜く(力技)。

Node-REDのインストール

次にNode-REDをインストールします。
Node.jsがインストールされていることが前提です。

Node.jsをインストールしていない場合
$ sudo apt-get update
$ sudo apt-get install -y nodejs npm
$ sudo npm cache clean
$ sudo npm install npm n -g
$ sudo n stable
Node-REDのインストールと自動起動
$ sudo apt-get install nodered
$ update-nodejs-and-nodered

$ sudo systemctl enable nodered.service
$ sudo systemctl start nodered.service
$ sudo systemctl status nodered.service

Node-REDで値を読み込む

端末から、http://(Raspberry PiのIPアドレス):1880へ接続すると、Node-REDのエディタが開くので、以下の操作を実施します。

  • 入力から[serial]をフロー1にドラッグアンドドロップ。
  • [serial]をダブルクリックしてポートを/dev/ttyACM0、ポートレートを9600に設定して、[更新]、[完了]。
  • 出力から[debug]をフロー1にドラッグアンドドロップ。
  • [/dev/ttyACM0]と[msg.payload]を線で結ぶ。
  • デバッグタブに切り替えて、[デプロイ]。

3.jpg

デバッグタブに、1秒ごとに数値が出力されれば成功です。

まとめ

これだけでは何の役にも立ちません。ド素人のお勉強でした。

DSCN5668.JPG

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8