MicroPython – MicroPython program structure

After reviewing various development tools for MicroPython in previous articles

MicroPython – Development tools Ampy – Eric

MicroPython IDE uPyCraft

in this article we briefly describe the main features of MicroPython. In particular, we examine the documentation relating to the ESP8266, but the concepts can also be extended to the other boards.

The documentation on the ESP8266 soc and MicroPython  is available at the link

ESP8266 MicroPython Documentation

At the link we can find all the informations necessary to implement the code on the card and the features of MicroPython as well as the differences compared to Python.

Here are the main points for coding in MicroPython:

  • MicroPython has a REPL (Read Evaluate Print Loop) console through the serial port to which the device is connected on which we can give the various MicroPython commands interactively.
  • The REPL console can also be accessed via connection to the IP address of the device by enabling the WEBREPL (Web Browser Interactive prompt) console

WebREPL Documentation

importing the webrepl package, enabling it and starting it at each device restart. To access the configured device, we can use the link

WEBREPL Access

or locally cloning it  from github

  • At the start of the device the system runs boot.py and after main.py.
  • The MicroPython libraries are available on the github at the link

MicroPython libraries

  • Additional packages

The additional modules can be downloaded from the link of the libraries presented above or downloaded with the upip utility (Package Manager) from the REPL console if available in the Python Package Index (PyPI) repository

PYPI

as described below

upip

Below is a simple example that connects to the router and allows us to access the MicroPython console with the browser and WEBREPL.

  • boot.py code

At the first access it is required to enable webrepl to be executed in the REPL console with the command

we can activate the service and the access password.

The procedure generates the webrepl_cfg.py file on the device.

After setting up and restarting, we access the web console by opening the locally cloned webrepl.html file with a browser

WebreplPressing connect and after entering the password ,set in the initial setup, we access the console

WebreplConnectedNow we install from the WEBREPL console, for example, the stat module using upip

To paste commands  execute Ctrl+A e Ctrl+V

WebreplupipIn this case the package was taken from the PYPI repository, unpacked and installed on the device.

We have noticed that some modules, although available on the repository, give an error during installation with upip; these modules are installed directly without using the upip utility,but by downloading the library from the site listed above and transferring the code to the device.

Here is the link of the code shown above

Webreplsample

Another option available is to build  the firmware with the required modules.

In the next article we will examine examples similar to what was done with LUA on ESP8266 in previous articles

MicroPython – Nodes examples with DHT11, Relay and MQTT