Using Raspberry Pi to program with arduino-cli

I started messing around with arduino-cli on the Raspberry Pi. It works great. I have a few Pi's though and wanted to do this on all of them. It's a little bit of work to install all of the boards and libraries on each of them.

The last couple of days I started playing with Docker on a Pi4. I started with a base image of Debian:buster-slim then installed the arduino-cli application and boards for a number of different Arduino compatible boards. The only issue I had was accessing the serial ports for programming the micros so I figured I would post here in case anyone else decides to do this. To get it to work I had to map my /dev volume and add the -- priviledged flag to the command line.

docker run -it -v /dev:/dev --priviledged imageName

After I was done setting up my docker image I exported it to a tar file using the following.

docker save -o outputFile.tar imageName

I then copied this to another Pi4 and used the following command to import the image

docker load -i outputFile.tar

This works amazingly well. The entire setup of all the boards/libraries and anything else that was setup in the image was transfered and started working immediately. It was definitely worth the time to learn a little bit about Docker.

Thanks for taking the time to share this information @theoutfield!

I need to take some time to learn more about Docker myself. I've been using it a good bit lately, but with only the most basic understanding.

Just for completeness these are the basic steps that I followed. I didn't setup the environment or add any users etc... inside of the Debian container. The beauty of this whole process is you always have a clean environment to work in. You can save it and go back at any time. If you screw up just delete the image and start over. You don't have to worry about messing up your Raspbian image. If you make changes that you want to keep just follow the steps to commit a new image.

I would do this incrementally until you know what you want. If you wanted to keep your projects on the Raspbian OS you could map a folder so your data isn't stored in the image. That's probably the Docker way of doing things but not a requirement.

All of the steps below could be used to create a Dockerfile that could be passed on to anyone to compile an image. I might do that once I get things working the way I want it.

Install Docker on Raspberry Pi Raspbian OS

#Get the Docker install script
curl -fsSL https://get.docker.com -o get-docker.sh

#Intall docker
sudo sh get-docker.sh

#Add pi user to docker group to allow not root docker use
sudo usermod -aG docker pi

#Check docker version to see if it installed
docker version

#Get some docker into
docker info

#Run the basic Docker hello-world image to verify install
docker run hello-world

#Download and run the Debian buster slim image
docker run -it debian:buster-slim

At this point you are working of a Debian container
Any changes you make here will not affect your Raspbian OS Install

#Go to the home folder
cd home

#Update the system and install vim and curl
apt-get update && apt-get install -y vim curl

#Get and install arduino-cli
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh

#Go to the bin folder where arduino-cli was installed
cd bin

#Update the core index
./arduino-cli core update index

#Install the arduino avr core
./arduino-cli core install arduino:avr

#Install the arduino megaavr core
./arduino-cli core install arduino:megaavr


#Make a new arduino project to test

#Make a new folder to hold projects
mkdir proj

#Change into the projects folder
cd proj

#Create a new empty sketch called blinky 
/home/bin/arduino-cli sketch new blinky

#Go into the blinky sketch folder
cd blinky/

#Complile the sketch for an arduino mega
/home/bin/arduino-cli compile -b arduino:avr:mega

#Try to upload the sketch to the board
#!!!This will fail because you don't have access to the /dev folder of the system inside of the container

/home/bin/arduino-cli upload -b arduino:avr:mega -p /dev/ttyACM0

#Exit the container
exit

You are now back in the Raspbian OS
Save all of the work you did in the Debian container
This will create a new Docker Image and Run It

#Get a list of all the containers

docker ps -a

#For my contain the ID was bc03.... so I commit that to a new image with the name theoutfield/arduino-cli
docker commit bc03600cea13 theoutfield/arduino-cli

#Now run the new image mapping the /dev volume in privileged mode
docker run -it -v /dev:/dev --privileged theoutfield/arduino-cli

#Upload the sketch binary to the board
/home/bin/arduino-cli upload -b arduino:avr:mega -p /dev/ttyACM0