install and init arduino CLI + boards via a script

Long version:
For my model train hobby, I devised a method of controlling all turnouts with just 2 buttons. An Arduino keeps track of where a train is, so the buttons may be used to control only the turnout which lies in front of the train. I am still building the hardware so I have not yet test it , though I expect it will work.

The current form of the code, uses a big struct with variables and hardcoded constants which contain information of the entire track layout. If I would change the layout, I would have to alter the struct table and reflash the arduino.

For an other unrelated project, I have built a processing program with which one can draw a schematic layout. This layout can be stored in a .csv file. This program does a lot more than just that but I am going to make a new stripped and modified version of this program which stores all track elements (that is sections, turnouts and detectors) in the EEPROM of the arduino via a USB connection. The arduino is than capable of loading the entire layout from it's EEPROM.

The problem here, is that the arduino itself still needs to be flashed with it's own program for the first time. The processing program will be made as an exported stand alone executable.

TL:DR version.
Many modeltrain enthusiasts are not very technical and cannot even fill in a few constants in a struct table nor program an arduino.

Therefor, I want to add an "UPLOAD PROGAM" button inside the processing program. This button should run a batch script or something similar which will use arduino CLI and flash the .hex file to an arduino nano or uno.

Ofcourse the mentioned people are by far incapable of installing arduino CLI and initialize it. I would like to download and install arduino CLI on a windows PC, do all initializing and install the cores for the uno/nano's atmega328P by simply double clicking on a single batch file (or whatever other script).

Before I spend my ever scarce free time in trying to figure it al out myself, I wanted to ask these two questions:

  • Can this be done in the first place?
  • Have people already done this?

Kind regards,

Bask185

bask185:

  • Can this be done in the first place?

Definitely.

bask185:

  • Have people already done this?

If not the exact thing, certainly people are doing similar things. For example, I wrote a Python script for continuous integration testing of Arduino projects. The script installs Arduino CLI, the necessary boards cores, and the necessary libraries, then compiles some sketches and generates a report on the results of the compilations:

Adafruit did something similar.

Those are both running on Linux systems, but Windows is essentially the same thing.

Arduino provides a download URL that will always give you the latest version of Arduino CLI for your operating system:
https://arduino.github.io/arduino-cli/latest/installation/#latest-packages

Thank you once again Pert,

I am currently assembling my first batch file and testing it step by step. So far clicking on the .bat downloads the arduino cli zip, unzips it, initialize everything and get the avr cores, compile a program and upload the program to the an uno.

I have one small question though. The compiling step, is a step which I wont be taking in the future. I just want to upload a hex file without al the default project folder and subfolders leading to the hex. What parameters can I use? The help page is not clear to me.

So to be clear, instead of the folder I want to type test.hex on the end.
arduino-cli upload -p COM9 --fqbn arduino:avr:uno test.hex

Bask185

bask185:
So to be clear, instead of the folder I want to type test.hex on the end.
arduino-cli upload -p COM9 --fqbn arduino:avr:uno test.hex

Use the --input-file option:

arduino-cli upload -p COM9 --fqbn arduino:avr:uno --input-file test.hex

@Pert, I got it working but somebody reminded me of a drawback of this method.

Not everybody is always keen when he runs some other guy's script which pulls stuff from the internet and 'hides' it on his computer.

Though the arduino cli executable is in the same folder, and I could easily auto-delete it, it are these 2 steps which lead to the unwanted effects

arduino-cli core update-index
arduino-cli core install arduino:avr

As I provide the hex file myself I have no need for the compiler components which comes with arduino-cli. My first question: were I not to do these steps, I cannot compile.. but can I upload hex files with arduino-cli?
(it is a bit hard to test as I already have all these files somewhere on my PC and I have no clue where atm)
To solve the issue I enabled arduino IDE's verbose output for uploading to reverse-engineer an other script which simply runs AVR dude.
To remain 'independed' from per-installed tools. I copied the executable, avrdude.conf and libush0.dll to my local folder.
If I invoke the batch script from my local folder with:

avrdude -C avrdude.conf -v -patmega328p -carduino -PCOM9 -b115200 -D -Uflash:w:test.hex:i

It works perfectly fine, but I have a another question, which you probably know the awnser to.
I still want to 'import' avrdude.exe and the two supplementing files from somewhere on the internet.
Now I looked at this website to download avrdude from the internet. link to sourceforge
Clicking the download buttons, gives you a setup.exe. Were I to run this setup, it would install avrdude and... probably other things as well... somewhere else in program files or appdata or....

Now if I were to host my current avrdude.exe and the two supplementing (config + dll) files on my personal github page, would this executable work on all or most windows 10 PCs?

If this would work, my script would not have to install anything else in a different folder on one's PC.

And also, is it allowed to host this software on my own github page? And is a GPL licence text file required?

I don't know any details but it could possibly be, that this setup would do something to avrdude.exe or the dll which could be platform specific. So my executable would not work on every other W10 PC.
I looked into the config and that one merely holds information about the uControllers to which it can upload stuff to.

Kind regards,

bask185

bask185:
but can I upload hex files with arduino-cli?

You don't need all the files, but you do need some of the files, and they need to be in the right place.

You don't need the avr-gcc compiler toolchain, you don't need the core library (where the code for things like digitalWrite() is), you don't need the bundled libraries (e.g., SPI, Wire).

You do need:

  • boards.txt
  • platform.txt
  • avrdude.exe
  • avrdude.conf
  • the .hex file

So you could create a minimal custom 3rd party Arduino boards platform that contains only the things needed to upload your .hex file.

On the other hand, you already discovered that you can just use avrdude directly. In your use case, arduino-cli is only acting as a way to install avrdude and run the avrdude command and your script could do all that.

bask185:
Now if I were to host my current avrdude.exe and the two supplementing (config + dll) files on my personal github page, would this executable work on all or most windows 10 PCs?

Other than the files being hosted on the arduino.cc server instead of github, this is what Arduino does and it seems to work perfectly for at least 99.99% of Windows users.

bask185:
And also, is it allowed to host this software on my own github page?

Yes.

bask185:
And is a GPL licence text file required?

I don't know enough about licenses to say. My approach is to just leave everything in place as it was and if it's only a component of my project I use the same license for my project as the subcomponent has. It's likely that is not all required. I am looking at the installation of avrdude on my computer that comes from Arduino and there is no license, readme, source code, nothing. But you can see the scripts Arduino uses to build avrdude and their license here:

and the version of the avrdude source code Arduino uses here:

There is definitely a lot of information available about the requirements to be compliant with open source licenses. Some small portion of it is even written in a manner that I can understand. I occasionally do some research and think I have some understanding of it, but then I seem to forget it pretty quickly. Maybe someone else here on the forum will be able to give a good answer.

bask185:
I don't know any details but it could possibly be, that this setup would do something to avrdude.exe or the dll which could be platform specific. So my executable would not work on every other W10 PC.

I do know that Arduino has made some modifications to the stock avrdude (see the links above for details). I would use the Arduino avrdude.