After reviewing various development tools for MicroPython in previous articles
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
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
importing the webrepl package, enabling it and starting it at each device restart. To access the configured device, we can use the link
or locally cloning it from github
git clone https://github.com/micropython/webrepl
- 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
- 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
as described below
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
#This file is executed on every boot (including wake-boot from deepsleep)
#Connects to the wifi AP providing a hostname, AP Name and passord
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
#Sets the device hostname
#CHANGE THE VALUES TO YOUR ACCESSPOINT NAME AND PASSWORD
while not sta_if.isconnected():
print('network config:', sta_if.ifconfig())
#Imports various modules for network, webrepl and garbage collector
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
To paste commands execute Ctrl+A e Ctrl+V
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
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