PlatformIO project to Arduino IDE script

Posted: May 06, 2021

Sometimes you need to share your PlatformIO project with a friend that is set on using the Arduino IDE. See the code and the link to a gist below this explanation below.

I recently had this challenge when working on a IOT project with a client where I had to convert the project from my setup using PlatformIO with VS code to run on their setup where Arduino IDE was the only option. I was unable to convert them to use PlatformIO for the project (ESP8266 based).

I think the Arduino IDE is great and is how I started with embedded development (I still use it often), but when you are doing something professionally I recommend PlatformIO and VS code. With PlatformIO I get the config file which helps me version libraries and set defaults, which makes running the project in future and on other environments much easier than doing this on the Arduino IDE. Then using VS code is great because I have the theme set the way I like, familiar keyboard shortcuts, extensions and nice git integration which helps me be way more productive than doing this on the Arduino IDE. I’m sure there are ways to make the Arduino IDE custom but most of my day job is in VS code so PlatformIO works for me.

The script below solves sending an Arduino IDE version of your code and sorting out the libraries versioning issue. First it will ask for a version number, this helps because when debugging you will be sure the other person is running the correct code. The libraries issue crops up when you have to tell the person which libraries and their version number to download for the project to work. This is not great when things are changing fast. So in the script I copy the libraries from the .pio folder to the Arduino IDE output. You can then tell the person to paste the libraries into then “Documents/Arduino/libraries” folder, now you all on the same page. There is also a small string replace piece to help with changing defaults consistently.

It’s not hard to do this manually however I was doing it often so I made a script. I am no bash professional so let me know if there are easier ways.

Instructions (Only tested on a Mac using zsh)

  1. Paste the make-arduino.sh into the root of your PlatformIO project. Give it execute permissions if needed: chmod u+x make-arduino.sh.
  2. Then adjust the script with the project name and any string replacements you need.
  3. Run it ./make-arduino.sh and give it a version. This will put it in a folder called build and add a zipped version.
  4. Send this to the Arduino IDE friend.
  5. Uzip folder.
  6. Included in the project is a libs folder. This is to remove the issues with having to find the libraries and the correct versions through the Arduino IDE.You take the the contents of the libs folder and paste it into the Arduino IDE libraries folder, usually at Documents/Arduino/libraries
  7. Double click the my-project-name.ino and you should be good to go.

Link to the gist

See the code (it might be behind the gist - I will update the gist if I find errors/improvements):

#!/bin/bash 
# make-arduino.sh

read -p "Enter version: " version

if [ -z "$version" ]
then
    echo "Error please add a version"
else
    echo "Make arduino ide version $version"

    projectName="my-project-name"
    # can replace strings in your project like passwords or settings, this is an array can add more
    stringReplacements=("s/string to be replaced/replacement string/g")

    # makes a folder called build for the results of this script
    mkdir build

    # main program
    mainName="$projectName-$version"
    mkdir build/$mainName
    # copying your custom libraries to the output folder
    cp ./lib/**/src/* build/$mainName
    # copying the source files
    cp ./src/* build/$mainName
    # rename the main.cpp to ino so it opens in arduino ide
    mv build/$mainName/main.cpp build/$mainName/$mainName.ino
    # replacing any strings like passwords and things
    for ((i = 0; i < ${#stringReplacements[@]}; i++))
    do
        sed -i '' "${stringReplacements[$i]}" build/$mainName/*.cpp build/$mainName/*.h build/$mainName/*.ino
    done

    # copy the libraries from pio folder to a libs folder (paste into the /Arduino/libraries folder)
    mkdir -p build/$mainName/libs
    cp -R ./.pio/libdeps/**/* build/$mainName/libs

    # rename if there are spaces 
    for i in build/$mainName/libs/*
    do
        mv "$i" ${i// /_}
    done

    # copying your flash data folder to the output folder
    mkdir -p build/$mainName/data
    cp -R ./data/* build/$mainName/data

    # zip it so you can send
    zip -r build/$mainName.zip build/$mainName
    echo "Made build/$mainName"
fi

© 2024 Running DeveloperAbout