MicroPython is an exciting language to use on ESP8266 cards and ESP32-based microcontrollers, but it doesn’t always include all the libraries you need for a specific project. This problem is less of an issue, thanks to the upip package manager.
Upip allows you to download any package in the standard MicroPython library over a Wi-Fi connection. That’s really useful if you’re on a project that needs a different library and don’t want to go through the tedious process of loading it yourself.
To participate, you need an ESP8266, ESP8285 or ESP32 card, such as the NodeMCU or D1 Mini. Designed for wearables, the ESP8285 is a smaller version of the ESP8266 with less flash memory. The ESP32 is the successor to the other models and has faster Wi-Fi, an extra CPU core, touch-sensitive pins, more GPIOs and more.
Once you have a board you will need to connect it to a computer so you will need a micro USB cable if you don̵
Step 1: Access MicroPython on your board
We’re assuming you already have MicroPython installed on your board, so we won’t be discussing that process again as we’ve covered it in detail in our guide to getting started with MicroPython on microcontrollers. That article contains a lot of good information such as erasing your board, installing MicroPython and then accessing the board. Check it out if you don’t know how to find your board’s serial port to connect to it.
Step 2: Connect to your board
If you just set up your board via step 1, you are already connected to your computer. If not, make sure to connect your microcontroller to your computer via the micro USB cable. Then use screen to connect to it.
~$ screen /dev/ttyUSB3 115200 MicroPython v1.13.bin-g48dcbbe60 on 2020-09-11; ESP module with ESP8266 Type "help()" for more information. >>>
As instructed, let’s run help out() to see what’s going on here.
>>> help() Welcome to MicroPython! For online docs please visit http://docs.micropython.org/en/latest/esp8266/ . For diagnostic information to include in bug reports execute 'import port_diag'. Basic WiFi configuration: import network sta_if = network.WLAN(network.STA_IF); sta_if.active(True) sta_if.scan() # Scan for available access points sta_if.connect("
", " ") # Connect to an AP sta_if.isconnected() # Check for successful connection # Change name/password of ESP8266's AP: ap_if = network.WLAN(network.AP_IF) ap_if.config(essid=" ", authmode=network.AUTH_WPA_WPA2_PSK, password=" ") Control commands: CTRL-A -- on a blank line, enter raw REPL mode CTRL-B -- on a blank line, enter normal REPL mode CTRL-C -- interrupt a running program CTRL-D -- on a blank line, do a soft reset of the board CTRL-E -- on a blank line, enter paste mode For further help on a specific object, type help(obj) >>>
Step 3: Set up the network
Now let’s set up the network interface. According to the network configuration instructions on the MicroPython site, we use the network module. And there are two Wi-Fi interfaces to create. Firstly, one for the station i.e. when your microcontroller connects to your router. Second, the access point on your microcontroller so that other devices can connect to it. We’re only concerned about the former because we just want to get libraries on the board.
>>> import network >>> sta_if = network.WLAN(network.STA_IF)
Now you can check if they are active:
>>> sta_if.active() True
If you see ‘False’ for your Wi-Fi router, manually set it to true with:
>>> sta_if.active(True) #5 ets_task(4020f4d8, 28, 3fff9df0, 10)
If you are unsure of your Wi-Fi network, you can scan to remind yourself.
>>> sta_if.scan() [(b'SO YOUNG BEAUTY', b'x0cxeaxc9wx83x00', 1, -63, 3, 0), (b'WIFIEA18FD', b'xa8xa7x95xeax19x01', 1, -79, 3, 0), (b'CableWiFi', b'xc4x01|x13x1cxc8', 4, -76, 0, 0), (b'SpectrumWiFi Plus', b'xc4x01|Sx1cxc8', 4, -76, 5, 0), (b'SpectrumWiFi', b'xc4x01|x93x1cxc8', 4, -75, 0, 0), (b'piccadilly', b'x90x1axcalxd7x00', 6, -70, 3, 0), (b'spot 2.4 ghz', b'@ptzdx90', 11, -52, 3, 0)]
With the well-known network we can now connect the board to it with:
', ' ')
In my case that is:
>>> sta_if.connect("spot 2.4 ghz", "CafeSpot000")
Now you can check if you are connected to:
>>> sta_if.isconnected() True
Step 4: Install a MicroPython library
Now we are ready to start using upip. We are going to download a MicroPython library from the / micropython / micropython-lib GitHub page. For our example, we’re going to use pystone_lowmen to run a benchmark test.
>>> import upip
Now we are ready to download a package from the Internet. If you want to see usage, use upip.help () For more information.
>>> upip.help() upip - Simple PyPI package manager for MicroPython Usage: micropython -m upip install [-p
] ... | -r import upip; upip.install(package_or_list, [ ]) If is not given, packages will be installed into sys.path (can be set from MICROPYPATH environment variable, if current system supports that). Current value of sys.path: /lib Note: only MicroPython packages (usually, named micropython-*) are supported for installation, upip does not support arbitrary code in setup.py.
To install a package in upip use the following format.
>>> upip.install("micropython-pystone_lowmem") Installing to: /lib/ Warning: micropython.org SSL certificate is not validated Installing micropython-pystone_lowmem 3.4.2.post4 from https://micropython.org/pi/pystone_lowmem/pystone_lowmem-3.4.2.post4.tar.gz
To use the new package, you can import it as you would any standard library import. Then you can run the module as a function as formatted below.
>>> import pystone_lowmem >>> pystone_lowmem.main() Pystone(1.2) time for 500 passes = 1137ms This machine benchmarks at 439 pystones/second
If you want to see all installed libraries, you can you and use that to list all libraries in the current directory.
>>> import uos >>> uos.listdir() ['boot.py', 'lib']
This is basically how you would get started with libraries not already installed on your MicroPython system. To see all the packages you can use, check out / micropython / micropython-lib on GitHub.
Do you want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get more than 60 hours of training from cybersecurity professionals.
Buy now (90% discount)>