Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Created November 12, 2019 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/9e7bb6fe8b43666efa743f38ec439e31 to your computer and use it in GitHub Desktop.
Save mamemomonga/9e7bb6fe8b43666efa743f38ec439e31 to your computer and use it in GitHub Desktop.
arduino-cliメモ

arduino-cliメモ

ArduinoチームがつくってるCLIのArduino

arduino-cli

事前準備

$ cd
$ mkdir bin
$ echo 'export PATH="$HOME/bin:$PATH"' >> .bashrc
$ exec $SHELL
$ curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh

$HOME/bin/arduino-cli が導入される

インデックスリストの更新

$ arduino-cli core update-index

接続しているArduinoを検出する

$ arduino-cli board list
Port         Type              Board Name          FQBN            Core
/dev/ttyACM0 Serial Port (USB) Arduino/Genuino Uno arduino:avr:uno arduino:avr

登録のあるハードウェアがあればリストにでてくる。FTDIなどのUSB-Serial変換チップをつかったものはUnknownとなる

このリストから、接続されているのはArduino Unoで、Coreはarduino:avrであることがわかる。

Arduino Uno用のライブラリを取得する

$ arduino-cli core install arduino:avr

スケッチを作成する

$ arduino-cli sketch new MyFirstSketch
$ cat MyFirstSketch/MyFirstSketch.ino
$ cat > MyFirstSketch/MyFirstSketch.ino << 'EOS'
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
EOS

コンパイル

$ arduino-cli compile --fqbn arduino:avr:uno MyFirstSketch

アップロード

$ arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno MyFirstSketch

コンパイルとアップロードをいっぺんにできるMakefile

  • makeとjqが必要。スケッチディレクトリにコピーして使用する。
  • FQBNをボードに合わせて変更する
  • 1つのホストに1つのArduinoデバイス限定(FQBNをキーにして探すため)

Makefile

FQBN := arduino:avr:uno
PORT := $(shell arduino-cli board list --format json | jq -r '.[] | select(.boards[]?.FQBN=="$(FQBN)") | .address')
SKETCH := $(notdir $(basename $(CURDIR)))

upload: compile
        cd .. && arduino-cli upload -p $(PORT) --fqbn $(FQBN) $(SKETCH)

compile:
        cd .. && arduino-cli compile --fqbn $(FQBN) $(SKETCH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment