Introduction: How to Integrate PlatformIO With Netbeans

Picture of How to Integrate PlatformIO With Netbeans

UPDATE: This Instructable is currently obsolete due to a feature in newer versions of PlatformIO that allow users to create NetBeans-compatible projects automatically. More information about this feature can be found here.

PlatformIO is a command line tool for creating, building and running programs for small devices like the Arduino or the Freescale Kinetis boards (and many others). It unifies the project structure and building process and makes it easier to port programmes between platforms. Although there is now an Atom-based IDE that uses PlatformIO under the hood, you may want to stick with your current IDE for C/C++ development and still take advantage of PlatformIO. This Instructable is about integrating PlatformIO with the Netbeans 8.1 with C/C++ support. Integrating PlatformIO with other IDEs like Eclipse or Visual Studio is simpler, as there are special project generators for those environments. However, if you find those generators not good enough or your favourite IDE is not supported, you should be able to repeat the steps from this tutorial, with some minor tweaking, to have PlatformIO integrated with your favourite IDE.

After completing this Instructable, you should have a powerful IDE for your next Arduino project.

NOTE: The following instructions were tested on Linux, but should work well on Windows and iOS without major changes.

ANOTHER NOTE: This tutorial borrows heavily on a blog entry by one of PlatformIO creators, Ivan Kravets, who has also contributed to this Instructable.

Step 1: Prerequisites

You will need a working instance of Netbeans and PlatformIO.

  • Netbeans can be downloaded from here. Just make sure you download the C/C++ version (or if you already use Netbeans, install the C/C++ development plugins).
  • PlatformIO may be a bit more complicated to install. Instructions can be found here.

You may also want to install a serial communication program like minicom or picocom (or PuTTY if you are using Windows). However, this is not necessary as PlatformIO contains its own serial port monitor.

Step 2: Create a Sample Project

Go to whatever directory you would like your sample project to reside in (I've chosen the Desktop directory but that's just because I'm not planning to keep this project for long ;-)). Create a project root directory:

mkdir nbBlink 

I've called the project "nbBlink" but you can use any name you like.

Now you need to initialize a PlatformIO project inside this directory. Let's say we want it to be an Arduino Uno project. So:

cd nbBlink
platformio init -b uno
  • "init" is the command to initialize a project.
  • "-b uno" is an option that tells PlatformIO that we want this project to run on the Arduino Uno board. It is actually possible to have a number of boards listed here.

PlatformIO will ask you to confirm the action. If you confirm, it will create a project directory structure and some configuration files. The project structure is very simple: There is a "src" directory for your source files and a "lib" directory for libraries. There's also a "platformio.ini" file with configuration.

Step 3: Create a Sample Source File

Picture of Create a Sample Source File

Create an empty file in the "src" directory called "main.cpp". The file must contain a valid Arduino project so there must be a "begin" and a "loop" procedure. You will also need a "#include " line at the top because after all this is a *.cpp file and not a *.ino file.

#include <Arduino.h>

void begin() {
}

void loop() {
}

You can copy the code above or use the attached "main.cpp" file.

Step 4: Importing the Project Into Netbeans

Picture of Importing the Project Into Netbeans

Launch Netbeans. In the "File" menu select "New Project". A wizard will appear. On the first page of the wizard select "C/C++" in the "Categories" section and "C/C++ Project with Existing Sources" in the "Projects" section. Click "Next". On the second page make sure to select "Custom" in the "Select Configuration Mode" option. Click "Next". Leave the "Pre-Build Action" page without changes. On the "Build Actions" page change the "Clean Command" to:

platformio -f -c netbeans run --target clean

and the "Build Command" to:

platformio -f -c netbeans run

Clieck "Next".

On the "Source Files" page, make sure you have the "src" directory in the "Source File Folders" list. If it's not there add it using the "Add Folder..." button.

Click "Next".

On the "Code Assistance Configuration" page, select "Manual Configuration" and click "Edit" besides the "Include Directories" text field. A dialogue window will appear. Use the "Add" button to add the following directories from the .platformio directory in your user's home directory:

/home/{username}/.platformio/packages/framework-arduinoavr/variants/standard 
/home/{username}/.platformio/packages/framework-arduinoavr/cores/arduino
/home/{username}/.platformio/packages/toolchain-atmelavr/avr/include

Click "OK" to close the dialogue window and "Next" to go to the last page of the wizard. The last page can be left with default values.

Click "Finish" to complete the wizard and import the project.

Step 5: Addidtional Configuration

Picture of Addidtional Configuration

After finishing the "New Project" wizard, you should see the project's structure in the "Projects" view on the left. Open the src/main.cpp file and try to add a line of code in the "setup" procedure to setup hardware serial communication:

Serial.begin(9600); 

The editor will complain that it cannot resolve identifier "Serial". This is because some of the header files, that you have made available to Netbeans by adding them to the "Included Directories" list in the wizard, require a special definition of the microcontroller chip that the program is being written for. To specify the chip, right-click on the project root in the "Projects" view and select "Properties" from the pop-up menu. Then go to "C++ Compiler" in the "Code Assistance" category. Edit the "Preprocessor Definitions" field and type in the following value:

__AVR_ATmega328P__

This is the symbol for the ATmega 328P chip that powers the Arduino Uno board.

After closing the properties dialogue window, the Serial identifier should be properly recognized by the editor and you should be able to use code completion.

Step 6: Add Some Logic and Build the Project

Picture of Add Some Logic and Build the Project

Since this is supposed to be a "Blink" project, we need to add the logic for blinking:

#include <Arduino.h>

const int LED_PIN = 13;

void setup() {
    Serial.begin(9600);
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_PIN, HIGH);
    delay(1000);
    Serial.println("Blink...");
    digitalWrite(LED_PIN, LOW);
    delay(1000);
}

Note, that code completion should work on all methods and objects.

After entering the code, build the project by selecting "Run -> Build Project" from the main menu or by pressing F11. You should see messages from the PlatformIO in the Output view on the bottom of the main IDE window.

Step 7: Uploading the Project to Arduino Board

Picture of Uploading the Project to Arduino Board

There is no "Upload" command in Netbeans but we can set the "Run" command to upload the compiled program to Arduino. To do that, right-click on the project's root in the "Projects" view and select "Properties" in the pop-up menu. Select the "Run" category. In the "Run Command" field type:

platformio -f -c netbeans run --target upload

Click "OK" to close the dialogue.

Now, if you have connected your Arduino board to the USB port on your computer, you can upload the project by selecting "Run -> Run Project" or pressing F6. You should see some avrdude messages in the Output view on the bottom ending with a green "Success" message. The Arduino should restart itself and the built-in led connected to pin 13 should start to blink.

Step 8: Monitoring Serial Port (Optional)

Picture of Monitoring Serial Port (Optional)

Our sample project does not only blink a led but it also sends a "Blink..." message repeatedly over the serial port. In order to see this message we need to open some sort of a Serial Port Monitor. We can do this outside Netbeans but since this is an Instructable about Netbeans, let's try to do this within our IDE. The easiest solution is to take advantage of a special feature of Netbeans that allows us to run the native console inside the IDE. To open this native console go to "Window -> IDE Tools" and select "Terminal". The terminal view will appear at the bottom of the IDE window. Now we can use some standard Unix tool for serial port monitoring like picocom. To use picocom for reading messages from the serial port used by Arduino run it with the following command:

picocom /dev/ttyACM0 -b 9600

"/dev/ttyACM0" is the name of a serial port on a Linux machine. Arduino boards will usually be connected to /dev/ttyACM0 or /dev/ttyUSB0.

The "-b 9600" option specifies that the baud rate for this connection is 9600 bits/second. This value must be the same as specified in the "Serial.begin(...)" method call your code.

After running this command, you should see some picocom initialization messages and a series of "Blink..." lines. To close the picocom session press Ctrl+A+X.

Step 9: Summary

And that's all there is to it!

After some configuration, Netbeans turns out to be quite a decent IDE for microcontroller development. The whole process described in this Instructable is a bit tedious and begs for some automation. I do hope that sooner or later there will be either a PlatformIO project generator for Netbeans or a full-fledged Netbeans plugin that will perform most of the steps described here automatically. Until then, I hope this Instructable will make it easier for you to use Netbeans with PlatformIO.

Thanks for reading!

Comments

seamster (author)2016-02-22

Thanks for sharing this great info. Welcome to instructables! :)

About This Instructable

1,325views

14favorites

License:

Add instructable to: