indaclouds.fr

Some tips when I take time to write them down.

Build and upload code without knowing your arduino USB port address


In order to upload your code to your arduino, you have to know its USB port address.

You can get the port using arduino-cli.

arduino-cli board list
Port         Type              Board Name              FQBN                 Core
/dev/ttyACM1 Serial Port (USB) Arduino/Genuino MKR1000 arduino:samd:mkr1000 arduino:samd

So you know that your arduino is available through /dev/ttyACM1

Then you can upload your code to your arduino using retrieved port.

arduino-cli upload --port /dev/ttyACM1

But if you change the USB port, the address change, so it can be a bit painful to fetch the port, modify the upload command accordingly. That's why I've used a Makefile and .env file to do it for me.

# .env
ARDUINO_PORT=$(
  arduino-cli board list --format json \
    | jq -r '.[] | if has("matching_boards") then .port.address else null end | values' \
)

I'm using jq to filter the data I want from the output and store it in a variable.

Then I'm sourcing that file in the Makefile instructions to make the variable available for the next commands.

# Makefile
compile:
    @arduino-cli compile --profile dev

upload: compile
    @source .env; \
    arduino-cli upload --profile dev --port $$ARDUINO_PORT

monitor:
    @source .env; \
    arduino-cli monitor --port $$ARDUINO_PORT

Now you can just do make upload to upload the code to your arduino without having to know its USB port address.