- Prolog
- Download and installation of external libraries
- Microcontroller without LAN/WLAN
- Microcontroller with LAN/WLAN
If you want to use for example, displays and sensors, you don't always have to develop everything yourself. There are already many resources that you can use.
However, you should always ask yourself the following questions:
- Does my ESP Microcontroller support LAN/WLAN?
- Can or do I always want to connect the device to the Internet?
- Did it even make sense for my goal or can I save resources?
Depending on the answer to these questions, you then have to decide how and when to install these external libraries! Here are a few hints/solution suggestions for possible scenarios.
In this scenario, the microcontroller has no LAN/WLAN network features. In addition, no own firmware should be created.
In the local project (root folder) create another folder named lib
.
It is recommended to use the "lib" directory (best practice), but you can also save libraries to the
root
or other folders. But always notesys.path
for other folders!
# create new directory
$ mkdir -p ~/Projects/ESP/lib
Download now the library into the folder. Here is an example for SSD1306:
# download resource into directory
$ curl -L 'https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/drivers/display/ssd1306/ssd1306.py' -o ~/Projects/ESP/lib/ssd1306.py
Now decide if you will freeze the downloaded module. You have already learned how to do this in the MicroPython frozen code.
Optional you can verify first, if directory lib
exits on the ESP microcontroller. In my example only the boot.py
exists.
# verify if directory exits (optional)
(venv) $ rshell -p [SERIAL-PORT] ls /pyboard/
...
/your/current/path> boot.py
...
Upload the directory including the module and optional verify.
# upload directory incl file(s)
(venv) $ rshell -p [SERIAL-PORT] cp -r lib/ /pyboard/
# verify if upload was sucessful
(venv) $ rshell -p [SERIAL-PORT] ls /pyboard/lib/
...
/your/current/path> ssd1306.py
...
That's it! Wasn't it super easy?
In this scenario, the ESP microcontroller has WLAN. However, the WLAN functionality will not be used later (only for installation/downloading). Also, the library is not stored locally.
Here you simply use the REPL
and mip
.
Earlier versions of MicroPython still had
upip
. However, this is considered obsolete from version:1.20.0
.
# start REPL from rshell
(venv) $ rshell -p [SERIAL-PORT] repl
# import WLAN and STA_IF
>>> from network import WLAN, STA_IF
# create station object
>>> sta = WLAN(STA_IF)
# activate station network interface
>>> sta.active(True)
# establish connection to access point
>>> sta.connect('YOUR SSID', 'YOUR PASSWORD')
# verify connection (after few seconds)
>>> sta.isconnected()
# import mip
>>> import mip
# install module
>>> mip.install('ssd1306')
# verify installation/download (optional)
>>> import uos
>>> uos.listdir('/lib/')
['ssd1306.mpy']
To exit the REPL
press keys Control
+ x
.
This option was super easy too... wasn't it? There are many other options! No matter which one you want to use, simply adapt it to your needs and the environment. Also, have a look on MicroPython documentation!