Internet Of Things: The IDE scandal
I recently entered the wonderful world of IOT — Internet Of Things (what a buzzword) — thanks to a nice gift a friend made me for Christmas: an Arduino kit. I’ve played with it for a couple of hours, trying to make flashing lights out of some C++ code.
So I spent some times searching for alternatives. Most of them are plugins and that’s not what I wanted (I’ll explain later why I don’t think this approach is the most efficient). As an absolute fan of JetBrains, I wanted to use WebStorm. But of course there’s no out of the box integration for it.
Wait, you say there’s no direct integration with most of the IDEs out there… But is there an elegant alternative?
Here comes PlatformIO.
Before going any further please note that if you are using WebStorm like me, there IS a plugin for it enabling pushing code to a microcontroller. This might work in some cases but as I said I wanted a more flexible solution. That’s why I will not speak more in detail about that. But you might want give it a try: Serial Port Monitor.
PlatformIO is an amazing little command line utility which lets you create a project for a specific type of board and, most importantly, deploy the code to it. Why would I prefer this over a plugin? For two simple reasons:
- This is a command line utility. This means that developers working on the project don’t need to work with a specific IDE to be able to simply push code to a board (there are even some editors with plugins for PlatformIO. You can find the full list here).
- Continuous integration. You can run it through Travis CI or any CI system. (PlatformIO default project comes with a configuration for Travis).
Before being able to play with this tool you need to install it (OMG! so innovative). It runs on Python 2.7 and can be installed using PIP, the Python package manager. PIP comes with Python since version 2.7.9. If you are on Mac and don’t have it installed yet, I highly recommend you to install it using Homebrew:
brew update && brew install python
For the others who are using Windows or Linux, here is the documentation.
Once you have these two requirements on your system, install PlatformIO using PIP. Note that you might need to sudo the command.
pip install -U platformio
Next step is to find the type of your microcontroller. This is important because you will need to tell PlatformIO the type’s ID of the hardware it will deploy to. You can find a list of supported boards here. For example, if yours is an Arduino Uno, then it’s type ID is uno.
Once you have this information, create a new folder and give it the name of your project. Then, using the command line, cd inside it and create a new PlatformIO project by running:
#just in case... replace BOARD_TYPE_ID by your board’s type ID
platformio init --board=BOARD_TYPE_ID
If your project is running on multiple devices, you can easily configure it as cross platform by adding a new “board” argument to the command for every different microcontroller type you want your code to run on. More on the init command here.
After you pressed enter and confirmed you want to create a new project, you should see something like this in your console:
Project has been successfully initialized!
If you take a look inside your project, you’ll see there is no boilerplate.
This is because the main goal of this tool is to focus on the upload/deployment of code to the microcontroller. And this is all we need it to do.
Run the program
To run and build a program on a board you just need to run:
platformio run --target upload
As of now, you can code and run the project from any code editor you want. Note that if you want to turn auto upload on (via platformio.ini), you don’t need the “target” argument.
That’s basically it!
But I got two more tips that can be really useful.
Bonus #1: Automation using npm
We saw the command to send your code to the microcontroller. This line is too long and having to run it multiple times is not very efficient. Yes, it is standalone and runs everywhere but… still…
"build": "platformio run --target upload"
At this stage, you can test everything works fine by adding a file inside the “src” folder and run the “build” script. You should then see a success message in your console and the microcontroller will be executing the new code.
Bonus #2: Quick run from WebStorm or other JetBrains product!
Cool, I have an npm script building and uploading files to the board. But this is not what I call an IDE integration…
Yes, and you’re right, npm scripts are nice but they are not what we can call a perfect alternative to integrated plugins. But what if we could execute them inside the IDE just like any other plugin? If your software comes from JetBrains then, good news, you can! Welcome to the world of “Run/Debug” configurations!
This feature is one of my favorite when speaking about JetBrains softwares (but you can probably find this in other IDEs as well). This allows you to integrate a lot of things. And this is what we are going to do.
Using the configuration above, you will be able to build, upload and run the content of your “src” folder on your microcontroller.
WE DID IT!
That’s all folks!
This is how to setup a development environment for IOT using your everyday tools. Hope this will be useful for others.
Once again you can find a list of code editors providing plugins for PlatformIO by following this link: http://docs.platformio.org/en/latest/ide.html