Sushi-IoT-Framework MicroPython interface
MicroPython introduction
Sushi-IoT-Framework integrates the MicroPython interpreter, giving you full access to the built-in firmware modules and the extensive set of libraries available online. Here we focus on the most important base concepts.
For beginners
If you are new to MicroPython development, the advice is follow these steps:
- Connect your board with USB cable
- Choose a user interface for MicroPython scripting
- Browse the examples section where you can start play with some scripts ready to test and modify.
Resources:
- ESP32 MicroPython quick reference, which gives a brief overview of the most common modules on the ESP32 microcontroller.
- Sushi-IoT-Framework MicroPython interface
Connect to the board
The first step to start coding is to connect to the MicroPython REPL interface.
There are basically two alternatives to program your scripts on the board:
-
Connect with USB cable directly to the micro board using the USB/UART REPL interface.
This is the most common, quick, and reliable solution to program your board. If you have a PC and no problems connecting by cable to the board, this is the best way. -
Connect via Web interface (WebREPL).
WebREPL is an alternative. From our tests it works, although there are still some issues and aspects to improve before recommending it as the best solution. Even in the MicroPython project it is considered experimental, but it will likely benefit from future improvements.
Choose a user interface
This section is for those who have no experience with MicroPython
Once the board is connected to your PC, you can choose among several user interfaces to develop your scripts. The REPL interface is an interpreter that executes MicroPython commands, allowing you to interact directly with the device. Through REPL, you can also access commands to read and write files on the device’s internal memory, which is managed by a file system available in MicroPython.
Although REPL can be used from any serial terminal that opens the COM port, working at such a low level quickly becomes impractical for development beyond simple tests.
For this reason, utilities are available that provide a higher-level interface, making it easier to perform tasks such as:
- Save Python files to the device memory (usually
.pyfiles), including:- Your application script files
- New classes or MicroPython components to add functionality to the device
- Open and edit files directly on the device
- Run the
.pyscripts in the device - Perform other actions, like restarting the device to test startup behavior after adding auto-run scripts
Some common interfaces for MicroPython development include:
- Arduino Lab for MicroPython – Arduino solution for MicroPython development
- Thonny – Simple interface, easy to use even for beginners
- mpremote – Command-line tool developed for MicroPython. Great for advanced users who prefer working with their own editor and sending commands from the prompt (Windows) or bash (Linux). Also useful for running scripts directly on the device.
The choice of interface is up to you. In our examples and tests, we often relied on Thonny.
Auto run a script
After you develop a script that performs some function, it is typical to want it to run automatically when the device starts.
It is important to know that MicroPython automatically runs two scripts at every boot:
- boot.py: typically contains system and hardware initialization commands.
- main.py: this is the main script executed to run your application. Here is where you put your code or run other ".py" scripts you created.
These two scripts can be placed, like any other ".py" file, into the file system using your favourite MicroPython UI interface.
Sushi Micropython interface
Sushi-IoT-Framework extends the base MicroPython interface with additional modules that allow you to quickly perform specific tasks.
Beyond the system tasks integrated into the framework these MicroPython modules offer another advantage: they speed up development by reducing the complexity of common operations to a minimum.
For example, with just a few lines of code you can:
- send or receive an SMS to remotely control a device, such as switching a light on or off
- integrate a local user interface menu to manage settings directly on the device
The MicroPython modules embedded in Sushi-IoT-Framework fall into two categories:
- Sushi core module - the core of the Sushi-IoT-Framework MicroPython interface, developed in C and providing all the framework’s native extensions.
- Extension modules - classic “.py” modules embedded in the firmware (as frozen modules). They rely on MicroPython interface in the background to perform the most common operations in the simplest way possible.
Help
To display a quick reference, from the MicroPython REPL call:
>>> sushi.help()
or see the result in Sushi IoT MicroPython quick reference.
Examples
See here all Sushi MicroPython examples
Frozen modules
Frozen modules can be wrapped or extended by the MicroPython interface.
See here all actual modules source code