Nov.22
Using PlatformIO to get started with Arduino in CLion IDE
As an almost everyday Android Studio user for past 2 years I feel like my whole programming effectiveness and efficiency became kind of ‘tightly coupled’ with IntelliJ’s IDE product family. Probably that’s why, when my new Arduino Yún finally arrived, the first thing on my to-do list was configuring a CLion template project. Most of IDE plugins and other minor solutions seemed not to be working, and that’s when I’ve found out about…
PlatformIO
In short words, it provides a very elegant, simple mechanism to generate all the code needed by CLion (and many other IDE’s) to get the thing going. My Yún model turned out to be the problematic, temporarily not working marginal case. That gave me an opportunity to get to know Ivan, the creator of PlatformIO himself (cheers!). Watching Ivan fast fixing these PlatformIO Python scripts on my computer through TeamViewer was an interesting experience :). Anyway, you can surely count on solid support here.
To the point
Assuming that you’ve already got the CLion and Python 2.6+ you can start installation of PlatformIO with below super-quick command:
python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)"
Now you need to generate the code in reference to PlatformIO’s get started section, in my case it’s:
mkdir YunMe cd YunMe platformio init --board=yun --ide=clion
Open CLion and import YunMe project. Create a new file in /src directory:
#include "Arduino.h" // Most Arduino boards already have a LED attached to pin 13 on the board itself #define LED_PIN 13 void setup() { pinMode(LED_PIN, OUTPUT); // set pin as output } void loop() { digitalWrite(LED_PIN, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(LED_PIN, LOW); // set the LED off delay(1000); // wait for a second }
Build the project [Run->Build] (⌘F9) to test it out. You can also use below CLI command:
platformio run -t upload
Your Arduino should start blinking now. 🙂
Auto-completion & syntax highlighting
Open CMakeLists.txt file in root directory and modify it like below:
# # To enable code auto-completion, please specify path # to main source file (*.c, *.cpp) and uncomment line below # add_executable(YunMe src/blink.cpp)
Just in case…
If you’ve encountered an ‘Please specify ‘upload_port’ (…)’ error message, you need to manually find out your Arduino’s upload port and add it to configuration. Make sure it’s plugged with USB and execute:
ls /dev/cu.*
Edit platformio.ini and add proper upload_port configuration line:
# Project Configuration File # # A detailed documentation with the EXAMPLES is located here: # http://docs.platformio.org/en/latest/projectconf.html # # A sign `#` at the beginning of the line indicates a comment # Comment lines are ignored. # Simple and base environment # [env:mybaseenv] # platform = %INSTALLED_PLATFORM_NAME_HERE% # framework = # board = # # Automatic targets - enable auto-uploading # targets = upload [env:yun] platform = atmelavr framework = arduino board = yun targets = upload upload_port = /dev/cu.usbmodem1421
Other useful commands
sudo easy_install pip
pip show platformio
pip install -U pip setuptools
pip install -U platformio
Trackback: C++ Annotated: Fall 2015 | JetBrains CLion Blog
Trackback: C++ Annotated: Fall 2015 | ReSharper C++ Blog
Konstantin Gredeskoul
Ivan, thank you so much for this guide! I just started playing around with CLion (as Jetbrains family has always been my go-to IDE), and can not wait to try your instructions here. I will follow up when I do, and if I find anything worth mentioning on my blog, I might add it there also.
Thanks again for your continuing work on this tool!
Ivan Kravets
Dear Michal,
Thanks a lot for the article! 🙂
> Now you need to find out your Arduino’s upload port, plug it in with USB and execute:
Why do you need this step? PlatformIO has built-in upload port finder. Just upload via `platformio run -t upload`
> probably CLion will throw some minor error popups but it’s ok as long as Yún starts blinking
I got your errors. Please don’t press “Run” button (green arrow). In this situation, CLion tries to run AVR binary on the host OS 🙂 Use “Menu: Run > Build” command instead and you will not have any warnings/errors. See #3 step from http://docs.platformio.org/en/latest/ide/clion.html
P.S: I’ve added your article to PlatformIO list http://docs.platformio.org/en/latest/articles.html
Regards, Ivan Kravets
– Ph.D, Researcher and Software Architect
– “Creativity comes from talent and never from knowledge” (c)
___________________________
http://www.ikravets.com
Michał Seroczyński
Hey there! Thanks for pointing these things out, I’ve updated the post, hope everything looks clear now :).
Ivan Kravets
Thanks. You have not answered why do you need `ls /dev/cu*` and `upload_port = /dev/cu.usbmodem1421`. Try without these steps. The `platformio run -t upload` should be enough.
Michał Seroczyński
Well, you already know that after our today’s talk, but I think I should write it down :). Mentioned CLI command should be enough, but wasn’t in my specific case (which is probably caused by bug in PySerial).