Abstract: In order to make Python, an easy-to-learn, community-API-rich language, available to the embedded world, Internet gamers gradually began a wave of embedded migration on Python, resulting in today’s protagonist, MircoPython.

This article is shared by Huawei Cloud community “Python Internet of Things derived from microPython [Grow Python!]”, original author: O0 Longlong0o.

The introduction

With the development of the Internet of Things and the sinking of the Internet to embedded applications, many programming languages applied in the Internet are also slowly developing to the field of embedded and microcontroller.

Because of the rapid development of the Internet of things, side to side also begins to be Internet players involved in the embedded application, originally embedded development mainly the application of embedded C language, because language C contrast on the Internet using scripting languages have both, learn bad, community API, less intervention against Internet players and patterns, In order to make Python easy to learn and community API-rich language available in the embedded field, some people say that 1000 lines of C program, 100 lines of Java program, Python only need 10 lines, which reflects the ease of Python. Gradually began a round of embedded migration to Python, leading to today’s hero, MircoPython.

MicroPython, like Python, is an open source interpretive language that complies with the GNU General Public License (GPL). It can run the ported API to control hardware and perform algorithm operations on any hardware board. Common hardware, such as IO-memory, sensors, displays, light switches, moving parts, networks and communications, can be operated using MicroPython.

MicroPython allows people with Python language experience, but not C language foundation, embedded foundation and hardware foundation, more quickly access to embedded, control their own MCU.

MicroPython

MicroPython is a streamlined and efficient implementation of the Python 3 programming language that includes a small portion of the Python standard library and is optimized for running in microcontrollers and constrained environments.

MicroPythonpyboard is a compact electronic board that runs MicroPython bare-metal, providing you with a low-level Python operating system that you can use to control a variety of electronic projects.

MicroPython is full of advanced features such as interactive hints, arbitrary precision integers, closures, list understanding, generators, exception handling, and more. However, it is compact enough to run in only 256K of code space and 16K of RAM. MicroPython is designed to be as compatible with plain Python as possible, allowing you to easily transfer code from your desktop to a microcontroller or embedded system.

MicroPython is a complete Python compiler and runtime that runs bare-metal. You’ll get an interactive prompt (REPL) to execute commands immediately, as well as the ability to run and import scripts from the built-in file system.

MicroPython tries to be as compatible with Python (called CPython) as possible, and modules such as the “machine” for accessing low-level hardware also implement some core Python libraries.

Take a look at how MicroPython operates on hardware

Import Pyb # print('Hello MicroPython! ')Copy the code

With MicroPython, I can simply switch an LED on and off, or print a log to the serial port window

To be more complex, we can manipulate a memory as follows:

I2C = I2C(SCL =Pin('X1'), Sda =Pin('X2')) # find device on bus dev_list = i2c.scan() # write '4' to device address 0x42 and read 4 bytes from 0x42 Writeto_mem (0x42, 0x12, b ") data = i2c. Readfrom_mem (0x42, 0x12, 2)Copy the code

We can simple operation by the above command a I2C memory, too much convenient than C language, a mircoPython, we can even directly using embedded mathematical operation, to accomplish the transfer of calculation, of course large computing still need embedded enough to calculate the force to a certain extent, the current equipment and define some discrepancy, It was fun, for example, to run the following calculations directly on the mircoPython IDE, which were done not by a computer but by a connected embedded MCU.

# # mircoPython data representation and calculation small integer > > > 123 # 123 large integer > > > 1 < < 160 > 1461501637330902918203684832716283019655932542976 # floating-point Numbers > > 1.23e6 1230000.0 # Complex computation >>> (1 + 2j) * 4j (-8+4j)Copy the code

MicroPython application

MicroPython currently supports 32-bit ARM processors such as PyBoard (STM32F405), Support for NRF51822 (Micro :bit), FireBeetle-ESP32, WiPy, ESP8266 core master, CC3200 and more.

More and more manufacturers are adapting to MicroPython. After all, there is a potential user base. Maybe LiteOS will inherit MicroPython support in the future. It will be easier for Internet people to get involved in the development of the sensing layer of the Internet of things, which is a great good thing for the development of the industry. I hope there will be more support in the future, such as Hongmeng, and more language packages will be supported.

Click to follow, the first time to learn about Huawei cloud fresh technology ~