Create a Makefile that allows several Arduino sketches to share the same code a Makefile and local libraries. This is useful for when different sketches will be used on potentially various boards and when customization is needed as it allows a set of sketches to share the same code set of libraries.
Clone the repo, cd into the directory
and run make help
to see the various targets.
:
$ make help
Usage:
make help
make <target> SKETCH=<SketchDir>
Examples:
make compile SKETCH=esp32s3-rbg-blink
make c SKETCH=esp32s3-rbg-blink
Notes:
- <SketchDir> must be a subdir containing <SketchDir>/<SketchDir>.ino
- Trailing '/' after the sketch name is accepted
Targets:
help h H Show this help
init One-time: init config, install core + libs
uninit Remove all files/dirs created by running `make init`
lib.help show lib help
#lib.<cmd> lib.<cmd>
lib.<cmd> Runs: arduino-cli lib <cmd> "$(PKG)"
list l List connected boards
compile c Compile (requires S or SKETCH)
upload u Compile if needed and Upload, (requires S or SKETCH, optional PORT)
monitor m Open serial monitor (optional PORT=<port> default=auto-detected, optional BAUD=<baudrate>, default 115200)
clean cl Remove build artifacts
After cloning the repo, you have a simple set of files and currently one sketch:
$ tree -L 2
.
├── esp32s3-rgb-blink
│ └── esp32s3-rgb-blink.ino
├── LICENSE-APACHE
├── LICENSE-MIT
├── Makefile
├── Makefile.broken
└── README.md
2 directories, 6 files
After cloning the next step is to run make init
to initialize the Arduino CLI
configuration, which installs the ESP32 core and the required libraries.
This, generally, only needs to be done once:
$ make init
arduino-cli --config-file "./arduino-cli.yaml" config init
Config file written to: ./arduino-cli.yaml
arduino-cli --config-file "./arduino-cli.yaml" config set directories.data shared/data
arduino-cli --config-file "./arduino-cli.yaml" config set directories.user shared
arduino-cli --config-file "./arduino-cli.yaml" config set directories.downloads shared/downloads
arduino-cli --config-file "./arduino-cli.yaml" config add board_manager.additional_urls https://espressif.github.io/arduino-esp32/package_esp32_index.json
arduino-cli --config-file "./arduino-cli.yaml" core update-index
Downloading index: library_index.tar.bz2 downloaded
Downloading index: package_index.tar.bz2 downloaded
...
Installed ESPAsyncTCP@1.2.4
Downloading ESPAsyncWebServer@3.1.0...
ESPAsyncWebServer@3.1.0 downloaded
Installing ESPAsyncWebServer@3.1.0...
Installed ESPAsyncWebServer@3.1.0
arduino-cli --config-file "./arduino-cli.yaml" lib install "TMC2209"
Downloading TMC2209@10.1.1...
TMC2209@10.1.1 downloaded
Installing TMC2209@10.1.1...
Installed TMC2209@10.1.1
Now if you look at the first two levels we have:
$ tree -L 2
.
├── arduino-cli.yaml
├── esp32s3-rgb-blink
│ └── esp32s3-rgb-blink.ino
├── LICENSE-APACHE
├── LICENSE-MIT
├── Makefile
├── Makefile.broken
├── README.md
└── shared
├── data
├── downloads
└── libraries
6 directories, 7 files
And if you use make lib.list
you can see the installed libraries:
$ make lib.list
arduino-cli --config-file "./arduino-cli.yaml" lib list
Name Installed Available Location Description
AsyncTCP 1.1.4 - user -
ESPAsyncTCP 1.2.4 - user -
ESPAsyncWebServer 3.1.0 - user -
TMC2209 10.1.1 - user -
You can know compile the example sketch with:
$ make compile SKETCH=esp32s3-rgb-blink
Compiling sketch 'esp32s3-rgb-blink' for esp32:esp32:esp32s3...
/home/wink/data/prgs/arduino-cli-shared/esp32s3-rgb-blink/esp32s3-rgb-blink.ino:6:10: fatal error: Adafruit_NeoPixel.h: No such file or directory
6 | #include <Adafruit_NeoPixel.h>
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Used platform Version Path
esp32:esp32 3.3.1 /home/wink/data/prgs/arduino-cli-shared/shared/data/packages/esp32/hardware/esp32/3.3.1
Error during build: exit status 1
make[1]: *** [Makefile:104: build/esp32s3-rgb-blink.ino.bin] Error 1
make: *** [Makefile:108: compile] Error 2
Notice we got an error that Adafruit_NeoPixel.h
is missing. This is because
the library is not installed. You can install it with:
$ make lib.install ARG="AdaFruit NeoPixel"
arduino-cli --config-file "./arduino-cli.yaml" lib install "AdaFruit NeoPixel"
Downloading Adafruit NeoPixel@1.15.1...
Adafruit NeoPixel@1.15.1 downloaded
Installing Adafruit NeoPixel@1.15.1...
Installed Adafruit NeoPixel@1.15.1
Now if you look at the list of libraries we can see Adafruit NeoPixel
is installed:
$ make lib.list
arduino-cli --config-file "./arduino-cli.yaml" lib list
Name Installed Available Location Description
Adafruit NeoPixel 1.15.1 - user -
AsyncTCP 1.1.4 - user -
ESPAsyncTCP 1.2.4 - user -
ESPAsyncWebServer 3.1.0 - user -
TMC2209 10.1.1 - user -
And now we can try to compile the sketch:
$ make c S=esp32s3-rgb-blink/
Compiling sketch 'esp32s3-rgb-blink' for esp32:esp32:esp32s3...
Sketch uses 330291 bytes (25%) of program storage space. Maximum is 1310720 bytes.
Global variables use 20696 bytes (6%) of dynamic memory, leaving 306984 bytes for local variables. Maximum is 327680 bytes.
Now upload the sketch:
$ make upload S=esp32s3-rgb-blink/
make[1]: 'build/esp32s3-rgb-blink.ino.bin' is up to date.
esptool v5.1.0
Connected to ESP32-S3 on /dev/ttyUSB0:
Chip type: ESP32-S3 (QFN56) (revision v0.2)
Features: Wi-Fi, BT 5 (LE), Dual Core + LP Core, 240MHz, Embedded PSRAM 16MB (AP_1v8)
Crystal frequency: 40MHz
MAC: 90:e5:b1:ac:fc:80
Stub flasher running.
Changing baud rate to 921600...
Changed.
Configuring flash size...
Flash will be erased from 0x00000000 to 0x00004fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x00060fff...
Wrote 20224 bytes (13058 compressed) at 0x00000000 in 0.4 seconds (460.6 kbit/s).
Hash of data verified.
Wrote 3072 bytes (146 compressed) at 0x00008000 in 0.0 seconds (681.6 kbit/s).
Hash of data verified.
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.1 seconds (927.8 kbit/s).
Hash of data verified.
Wrote 330432 bytes (178137 compressed) at 0x00010000 in 3.3 seconds (799.8 kbit/s).
Hash of data verified.
Hard resetting via RTS pin...
New upload port: /dev/ttyUSB0 (serial)
And finally you can monitor the serial output:
$ make monitor
Monitor port settings:
baudrate=115200
bits=8
dtr=on
parity=none
rts=on
stop_bits=1
Connecting to /dev/ttyUSB0. Press CTRL-C to exit.
color=0xFF0000
current_secs=5
color=0x00FF00
color=0x0000FF
current_secs=6
color=0x000000
color=0xFF0000
current_secs=7
color=0x00FF00
color=0x0000FF
current_secs=8
color=0x000000
color=0xFF0000
current_secs=9
color=0x00FF00
^C
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.