How To Manage MicroPython Modules With Mip on Raspberry Pi Pico

Managing modules in Python is often handled through pip, a Python package manager that lists available Python modules using repositories provided by PyPi. But what’s in MicroPython? There was upip, a micro version of pip, and now there’s mip, the new official lightweight package manager for MicroPython.
Mip is designed for all MicroPython devices, online or offline. Internet-connected devices can use it directly via the Python shell, while offline devices can use the tool mpremote to install modules from their computer.
This how-to shows how to use mip directly. Raspberry Pi Pico Wgo offline using raspberry pi pico and mpremote. It also describes some useful mpremote commands.
Using mip on Raspberry Pi Pico W
Using mip on a network-connected MicroPython device allows you to install modules directly on the device, similar to how pip installs Python modules and Linux package managers.
1. Do the following Download the latest version of MicroPython for Raspberry Pi Pico W. The most important steps are to download and install the UF2 firmware image and set up your Thonny. The rest are optional. Make sure you have downloaded MicroPython 1.20 or later.
2. open tony and click the stop button Refresh the connection. This will ensure that the Python shell will open and work correctly.
3. Create a new file. This file contains all the steps needed to connect to Wi-Fi.
Four. Add the following lines of code to the new file. Change the SSID and PASSWORD to your own.
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID","PASSWORD")
print(wlan.isconnected())
Five. Save the file to your Raspberry Pi Pico W as network-connection.py.
6. [実行]Click to start Wi-Fi connection. After a few seconds, True is printed to the Python shell. This indicates that you are connected to the Internet. If false,[停止]then again[実行]Click.
7. Import mip, a lightweight package manager.
import mip
8. Install the package and test mip. I chose umqtt, the MQTT module for MicroPython. A package is installed by calling mip’s install function and passing it the name of the package. Mip uses micropython-lib as an index, Python 3 packages manage, pip uses his PyPI index.
mip.install(“umqtt.simple”)
9. Test the installation of third-party MicroPython packages. You can also use mip to install third-party packages other than the micropython-lib index. Here we pass the URL to the install function. pico zero library From the Raspberry Pi Foundation.
mip.install(“https://raw.githubusercontent.com/RaspberryPiFoundation/picozero/master/picozero/picozero.py”)
Using Mip with Mpremote on Raspberry Pi Pico
For MicroPython on devices without network access, Raspberry Pi Pico requires the use of mip with mpremote, a tool that communicates with the device over the USB/serial interface.
1. Do the following Download the latest version of MicroPython for Raspberry Pi Pico W. The most important steps are to download and install the UF2 firmware image and set up your Thonny. The rest are optional. Make sure you have downloaded MicroPython 1.20 or later.
2. make sure Python 3 is installed on your machine.
3. Open a command prompt and install mpremote using pip.
pip install mpremote
Four. Run mpremote and pass mip as an argument. and Then specify the package name or module url. Here I am installing the packages that I use 7-segment display with Pico.
mpremote mip install https://raw.githubusercontent.com/mcauser/micropython-tm1637/master/tm1637.py
Other useful mpremote commands
Mpremote is a handy tool for quickly performing tasks on your MicroPython device. We’ve detailed some useful commands that help you manage MicroPython devices.
remote: It automatically connects to a device running MicroPython and displays the output of running code. Press CTRL + ]to close the connection.
mpremote repl: Opens an interactive Python shell, a REPL (read, evaluate, print, loop) that allows the user to interact directly with the hardware.
mpremote soft reset: Reboot the attached MicroPython device. This is the same as pressing CTRL + D in the REPL.
mpremote fs <コマンド>: Use a set of file system commands on MicroPython devices. These commands are similar to common Unix/Linux commands.
instructions | explanation |
---|---|
Cat | display the contents of the file |
ls | List the contents of the current directory |
ls | List the contents of the specified directory |
CP [-r] | Copy the files. Use the : prefix to specify files on the MicroPython device. Recursive use -r |
RM | Delete files from your device |
mkdir | Create a directory on your device |
rmdir | Delete directory on device |
touch | to create a file on the device. |
This example lists the contents of flash storage, creates a new file, then relists the storage to show the new file.
more: Best RP2040 board
more: best raspberry pi project
more: Raspberry Pi: Introduction