Integration options
The three pillars of the Arduino CLI
The Arduino CLI is an open source Command Line Application written in Golang that can be used from a terminal to compile, verify and upload sketches to Arduino boards and that’s capable of managing all the software and tools needed in the process. But don’t get fooled by its name: Arduino CLI can do much more than the average console application, as shown by Arduino IDE 2.x and Arduino Cloud, which rely on it for similar purposes but each one in a completely different way from the other. In this article we introduce the three pillars of the Arduino CLI, explaining how we designed the software so that it can be effectively leveraged under different scenarios.
The first pillar: command line interface
Console applications for humans
As you might expect, the first way to use the Arduino CLI is from a terminal and by a human, and user experience plays a key role here. The UX is under a continuous improvement process as we want the tool to be powerful without being too complicated. We heavily rely on sub-commands to provide a rich set of different operations logically grouped together, so that users can easily explore the interface while getting very specific contextual help (even in Chinese!).
1$ LANG=zh arduino-cli2Arduino 命令行界面 (arduino-cli)3
4用法:5 arduino-cli [command]6
7示例:8 arduino-cli <命令> [参数...]9
10可用命令:11 board Arduino 开发板命令12 burn-bootloader 上传引导加载程序。13 cache Arduino 缓存命令。14 compile 编译 Arduino 项目15 completion 已生成脚本16 config Arduino 配置命令。17 core Arduino 内核操作。18 daemon 在端口上作为守护进程运行:5005119 debug 调试 Arduino 项目20 help Help about any command21 lib Arduino 关于库的命令。22 monitor 开启开发板的通信端口。23 outdated 列出可以升级的内核和库24 sketch Arduino CLI 项目命令25 update 更新内核和库的索引26 upgrade 升级已安装的内核和库。27 upload 上传 Arduino 项目。28 version 显示 Arduino CLI 的版本号。29
30参数:31 --additional-urls strings 以逗号分隔的开发板管理器附加地址列表。32 --config-file string 自定义配置文件(如果未指定,将使用默认值)。33 --format string 日志的输出格式,可以是:text, json, jsonmini, yaml (default "text")34 -h, --help help for arduino-cli35 --log 在标准输出上打印日志。36 --log-file string 写入日志的文件的路径。37 --log-format string 日志的输出格式,可以是:text, json38 --log-level string 记录此级别及以上的消息。有效级别为 trace, debug, info, warn, error, fatal, panic39 --no-color Disable colored output.40
41使用 "arduino-cli [command] --help" 获取有关命令的更多信息。
Console applications for robots
Humans are not the only type of customers we want to support and the Arduino CLI was also designed to be used programmatically - think about automation pipelines or a CI/CD system. There are some niceties to observe when you write software that’s supposed to be easy to run when unattended and one in particular is the ability to run without a configuration file. This is possible because every configuration option you find in the arduino-cli.yaml configuration file can be provided either through a command line flag or by setting an environment variable. To give an example, the following commands are all equivalent and will fetch the external package index for ESP32 platforms:
1$ cat ~/.arduino15/arduino-cli.yaml2board_manager:3 additional_urls:4 - https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json5
6$ arduino-cli core update-index7Downloading index: package_index.tar.bz2 downloaded8Downloading index: package_esp32_index.json downloaded
or:
1$ export ARDUINO_BOARD_MANAGER_ADDITIONAL_URLS="https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json"2$ arduino-cli core update-index3Downloading index: package_index.tar.bz2 downloaded4Downloading index: package_esp32_index.json downloaded
or:
1$ arduino-cli core update-index --additional-urls="https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json"2Downloading index: package_index.tar.bz2 downloaded3Downloading index: package_esp32_index.json downloaded
See the configuration documentation for details about Arduino CLI's configuration system.
Consistent with the previous paragraph, when it comes to providing output the Arduino CLI aims to be user friendly but also slightly verbose, something that doesn’t play well with robots. This is why we added an option to provide output that’s easy to parse. For example, the following figure shows what getting the result in JSON format and filtering using the
jq
tools may look like:1$ arduino-cli lib search FlashStorage --format json | jq .libraries[0].latest2{3 "author": "Various",4 "version": "1.0.0",5 "maintainer": "Arduino <info@arduino.cc>",6 "sentence": "The FlashStorage library aims to provide a convenient way to store and retrieve user's data using the non-volatile flash memory of microcontrollers.",7 "paragraph": "Useful if the EEPROM is not available or too small. Currently, ATSAMD21 and ATSAMD51 cpu are supported (and consequently every board based on this cpu like the Arduino Zero or Aduino MKR1000).",8 "website": "https://github.com/cmaglie/FlashStorage",9 "category": "Data Storage",10 "architectures": [11 "samd"12 ],13 "types": [14 "Contributed"15 ],16 "resources": {17 "url": "https://downloads.arduino.cc/libraries/github.com/cmaglie/FlashStorage-1.0.0.zip",18 "archive_filename": "FlashStorage-1.0.0.zip",19 "checksum": "SHA-256:2f5a349e1c5dc4ec7f7e22268c0f998af3f471b98ed873abd6e671ac67940e39",20 "size": 12265,21 "cache_path": "libraries"22 }23}
Even if not related to software design, one last feature that’s worth mentioning is the availability of a one-line installation script that can be used to make the latest version of the Arduino CLI available on most systems with an HTTP client like curl or wget and a shell like bash.
For more information on Arduino CLI's command line interface, see the command reference.
The second pillar: gRPC interface
gRPC is a high performance RPC framework that can efficiently connect client and server applications. The Arduino CLI can act as a gRPC server (we call it daemon mode), exposing a set of procedures that implement the very same set of features of the command line interface and waiting for clients to connect and use them. To give an idea, the following is some Golang code capable of retrieving the version number of a remote running Arduino CLI server instance:
1// This file is part of arduino-cli.2//3// Copyright 2024 ARDUINO SA (http://www.arduino.cc/)4//5// This software is released under the GNU General Public License version 3,6// which covers the main part of arduino-cli.7// The terms of this license can be found at:8// https://www.gnu.org/licenses/gpl-3.0.en.html9//10// You can be released from the requirements of the above licenses by purchasing11// a commercial license. Buying such a license is mandatory if you want to12// modify or otherwise use the software for commercial activities involving the13// Arduino software without disclosing the source code of your own applications.14// To purchase a commercial license, send an email to license@arduino.cc.15
16package main17
18import (19 "context"20 "log"21 "time"22
23 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"24 "google.golang.org/grpc"25 "google.golang.org/grpc/credentials/insecure"26)27
28func main() {29 // Establish a connection with the gRPC server30 conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))31 if err != nil {32 log.Println(err)33 log.Fatal("error connecting to arduino-cli rpc server, you can start it by running `arduino-cli daemon`")34 }35 defer conn.Close()36
37 // Create an instance of the gRPC clients.38 cli := rpc.NewArduinoCoreServiceClient(conn)39
40 // Now we can call various methods of the API...41 versionResp, err := cli.Version(context.Background(), &rpc.VersionRequest{})42 if err != nil {43 log.Fatalf("Error getting version: %s", err)44 }45 log.Printf("arduino-cli version: %v", versionResp.GetVersion())46}
gRPC is language agnostic: even if the example is written in Golang, the programming language used for the client can be Python, JavaScript or any of the many supported ones, leading to a variety of possible scenarios. Arduino IDE 2.x is a good example of how to leverage the daemon mode of the Arduino CLI with a clean separation of concerns: the IDE knows nothing about how to download a core, compile a sketch or talk to an Arduino board and it demands all these features of an Arduino CLI instance. Conversely, the Arduino CLI doesn’t even know that the client that’s connected is the Arduino IDE, and neither does it care.
For more information on Arduino CLI's gRPC interface, see the gRPC interface reference.
The third pillar: embedding
Arduino CLI is written in Golang and the code is organized in a way that makes it easy to use it as a library by including the modules you need in another Golang application at compile time. Both the first and second pillars rely on a common Golang API, based on the gRPC protobuf definitions: a set of functions that abstract all the functionalities offered by the Arduino CLI, so that when we provide a fix or a new feature, they are automatically available to both the command line and gRPC interfaces. The source modules implementing this API are implemented through the
commands
package, and it can be imported in other Golang programs to embed a full-fledged Arduino CLI. For example, this is how
some backend services powering Arduino Cloud can compile sketches and manage libraries. Just to give you a taste of
what it means to embed the Arduino CLI, here is how to search for a core using the API:1// This file is part of arduino-cli.2//3// Copyright 2024 ARDUINO SA (http://www.arduino.cc/)4//5// This software is released under the GNU General Public License version 3,6// which covers the main part of arduino-cli.7// The terms of this license can be found at:8// https://www.gnu.org/licenses/gpl-3.0.en.html9//10// You can be released from the requirements of the above licenses by purchasing11// a commercial license. Buying such a license is mandatory if you want to12// modify or otherwise use the software for commercial activities involving the13// Arduino software without disclosing the source code of your own applications.14// To purchase a commercial license, send an email to license@arduino.cc.15
16package main17
18import (19 "context"20 "fmt"21 "io"22 "log"23
24 "github.com/arduino/arduino-cli/commands"25 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"26 "github.com/sirupsen/logrus"27)28
29func main() {30 // Create a new ArduinoCoreServer31 srv := commands.NewArduinoCoreServer()32
33 // Disable logging34 logrus.SetOutput(io.Discard)35
36 // Create a new instance in the server37 ctx := context.Background()38 resp, err := srv.Create(ctx, &rpc.CreateRequest{})39 if err != nil {40 log.Fatal("Error creating instance:", err)41 }42 instance := resp.GetInstance()43
44 // Defer the destruction of the instance45 defer func() {46 if _, err := srv.Destroy(ctx, &rpc.DestroyRequest{Instance: instance}); err != nil {47 log.Fatal("Error destroying instance:", err)48 }49 fmt.Println("Instance successfully destroyed")50 }()51
52 // Initialize the instance53 initStream := commands.InitStreamResponseToCallbackFunction(ctx, func(r *rpc.InitResponse) error {54 fmt.Println("INIT> ", r)55 return nil56 })57 if err := srv.Init(&rpc.InitRequest{Instance: instance}, initStream); err != nil {58 log.Fatal("Error during initialization:", err)59 }60
61 // Search for platforms and output the result62 searchResp, err := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{Instance: instance})63 if err != nil {64 log.Fatal("Error searching for platforms:", err)65 }66 for _, platformSummary := range searchResp.GetSearchOutput() {67 installed := platformSummary.GetInstalledRelease()68 meta := platformSummary.GetMetadata()69 fmt.Printf("%30s %8s %s\n", meta.GetId(), installed.GetVersion(), installed.GetName())70 }71}
Embedding the Arduino CLI is limited to Golang applications and requires a deep knowledge of its internals. For the average use case, the gRPC interface might be a better alternative. Nevertheless, this remains a valid option that we use and provide support for.
Conclusions
You can start playing with the Arduino CLI right away. The code is open source and the repo contains example code showing how to implement a gRPC client. If you’re curious about how we designed the low level API, have a look at the commands package and don’t hesitate to leave feedback on the issue tracker if you’ve got a use case that doesn’t fit one of the three pillars.
ON THIS PAGE