Features Plugins Tutorials

Announcing the MicroPython Plugin for PyCharm

Today we’ve released the MicroPython plugin 1.0 for PyCharm. This plugin lets you edit your MicroPython code and interact with your MicroPython-powered microcontrollers using PyCharm. It supports ESP8266, Pyboard, and BBC Micro:bit devices. The plugin is being developed as a team project by the PyCharm Community lead Andrey Vlasovskikh. The source code for the project can be found on GitHub.

MicroPython is a relatively new member of the Python interpreters family. It’s basically a Python 3.5 implementation designed for microcontrollers — small computing devices that are used everywhere from smart watches to cars. People usually program microcontrollers in C or an assembly language due to low performance and memory limits. Thanks to clever optimization techniques implemented in MicroPython you can now use (almost) standard Python for microcontrollers. For example, you can create your own Internet of Things device and program it in MicroPython.

The MicroPython plugin is compatible with both PyCharm Community and Professional editions. We’re going to make it available for IntelliJ IDEA soon as well. Let me walk you through the setup process and the features of the plugin using PyCharm:

We’ll be using an ESP8266-based device called WEMOS D1 mini. Basically, it’s a Wi-Fi chip with a couple of digital and analog I/O pins to connect external sensors and actuators. But for our simple demo, we won’t need anything besides the LED light that is already located on the device and is connected to the digital output pin 2.

This is our demo program which toggles the LED every second:

import utime
from machine import Pin


def main():
    led = Pin(2, Pin.OUT)
    enabled = False
    while True:
        if enabled:
            led.off()
        else:
            led.on()
        utime.sleep_ms(1000)
        enabled = not enabled


if __name__ == '__main__':
    main()

Let’s get started setting up our device!

First of all, make sure your OS can see your device via USB. This step is device-dependent. For WEMOS D1 mini on Windows and macOS you’ll need a serial port driver provided by the device vendor.

Next, we’ll setup PyCharm to work with your device. First, you need to install the MicroPython plugin in PyCharm “File | Settings | Plugins”. Then you need to create a new Python project in “File | New Project…”. In PyCharm 2017.3 the new project dialog with the correct settings will look like this:

image6

Make sure you’ve configured a Python 3.5 or a newer interpreter for it (preferably a virtual environment), since the MicroPython plugin will later ask you to install a few Python packages to communicate with your device.

After that add a new file to your new project with the contents of our program above. Finally, enable MicroPython support for your project in “File | Settings | Languages & Frameworks | MicroPython” and specify your device there:

MicroPython Configurable

Now let’s see what the plugin has to offer.

Code Completion and Documentation

The MicroPython plugin provides code completion and documentation for MicroPython-specific modules:

MicroPython Code Completion

Notice that code completion is context-aware. On this screenshot PyCharm shows you only the members of the utime module.

The quick documentation window contains the docs for the selected name. Use Ctrl+Q (F1 on macOS) to show this pop-up window. You can also dock it and enable “Auto-update from Source” to keep it permanently.

Syntax Checking and Type Checking

The plugin searches syntax errors and other problems in your code like potential AttributeError or ImportError using static code analysis. It comes with Python stub files for device-specific binary modules. These stubs contain Python type hints that make it possible to check types in your MicroPython code:

MicroPython Type Checking

On the screenshot above you can see several Python syntax errors when the user tries to write some C code in the middle of their Python file. There is also a type error in utime.sleep_ms(3.14), since this function only accepts integers.

Flash Files to Devices

The MicroPython plugin helps you to upload your files to your MicroPython device via USB. Use “MicroPython” run configurations to flash files or folders to your device in “Run | Edit Configurations…” menu. To quickly upload a single file you can select “Run ‘Flash <your-file-name>.py’” from the context menu of your Python file:

MicroPython Run Configuration

MicroPython REPL

Interactive experiments play an important role in Python development, but they are even more important with microcontrollers which usually don’t have any screens to show possible errors. The MicroPython plugin allows you to quickly run an interactive Python REPL console. Use “Tools | MicroPython | MicroPython REPL” menu to run a MicroPython shell on your device.

MicroPython REPL

I hope you enjoy this plugin. I’ll be glad to hear your feedback and how you’re using it. Tell me about your experience with it in the comments below, or on twitter: @vlasovskikh or @pycharm. Star or fork the intellij-micropython repository on GitHub, send your issues and pull requests!

image description