I want to automatically upload a sketch to an Arduino uno without using the application
My suggestion is to use the --format json
flag in your arduino-cli board list
command. This will cause the output to be in the machine readable JSON format.
You can pipe that output through the jq tool, which parses JSON. You can use this as the jq
filter argument to get the port from the arduino-cli board list
output: '.[0].port.address'
In shell, I would do this:
arduino-cli upload -b arduino:samd:mkrwifi1010 -p $(arduino-cli board list --fqbn arduino:samd:mkrwifi1010 --format json | jq -r '.[0].port.address') C:/Desktop/sketch.ino
I don't know about what changes would be needed to do that in a Windows batch file though. I do my best to avoid any involvement with that frustrating language these days.
Hi @robotmaster1989, what is your target Operating System?
PowerShell can handle JSON without any extra tools..
upload.ps1
You can give FQBN as a parameter, case insensitive, can be a pattern like *:uno
.
Modify the arduino-cli upload ...
line according to your needs.
param([string]$Board = "arduino:samd:mkrwifi1010")
$cli_board_list = arduino-cli board list --format json | ConvertFrom-JSON
if ($cli_board_list.count -eq 0) {
echo "No boards connected!"
exit
}
# create simple object(s) with only the fields we need
$board_list = $cli_board_list | foreach-object {
[PSCustomObject] @{
Name = $_.matching_boards.name
FQBN = $_.matching_boards.fqbn
Port = $_.port.label
}
}
echo "Connected boards"
$board_list | format-table
$m_boards = $board_list | Where-Object { $_.fqbn -like $board }
if ($m_boards.count -eq 0) {
echo "No boards matching $board found"
exit
}
echo "Boards matching $board"
$m_boards | format-table
if ($m_boards.count -gt 1) {
echo "multiple boards matching $board found"
}
elseif ($m_boards.port -like "COM*") {
arduino-cli upload --fqbn $m_boards.fqbn --port $m_boards.port
}
else {
echo "????"
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.