Abstract:In order to make Python as easy to learn, easy to learn, community API-rich language can be used in the embedded field, the Internet players gradually began a round of embedded migration on Python, so that today’s protagonist – Mircopython.

This article is from the Huawei cloud community “Python Internet of Things derivative MicroPython”, the original author: o0 long long 0o.

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 single-chip microcomputer.

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, Internet players can use the language which is easy to learn and rich in community API in the embedded field. Some people say that 1000 lines of C program, 100 lines of Java program and Python only need 10 lines, which reflects the ease of Python, and gradually start a round of migration of embedded Python. Which brings us to today’s protagonist, Mircopython.

MicroPython, like Python, is an open source interpreted language under the GPL (GNU General Public License). MicroPython can run ported APIs on any hardware board to control hardware and perform algorithms. Common hardware such as IO- memory, sensors, displays, light switches, moving parts, networking and communication can be operated using the MicroPython language.

MicroPython allows people with Python language experience but no C language foundation, embedded foundation and hardware foundation to access embedded more quickly and control the MCU in their hands

MicroPython

MicroPython is a compact 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.

MicroPython PyBoard is a compact electronic board that runs MicroPython on bare metal machines, giving you 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 aims to be as compatible with normal Python as possible, allowing you to easily transfer code from the desktop to microcontrollers or embedded systems.

MicroPython is the complete Python compiler and runtime that runs on a bare metal machine. You 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 is as compatible with Python (called CPython) as possible, and some of the core Python libraries are implemented in modules such as “machines” for accessing low-level hardware.

Take a look at how MicroPython manipulates hardware

Print ('Hello MicroPython! ') print('Hello MicroPython! ')

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

More complex we can manipulate a memory as follows:

I2C = I2C(SCL =Pin('X1')), Sda =Pin('X2') dev_list = i2c.scan() # write '4' to device address 0x42 and read 4 bytes from 0x42 i2c. writeTo (0x42,) ReadFrom (0x42, 4) # ReadFrom (0x42, 4) # ReadFrom (0x42, 4) # ReadFrom (0x42, 0x12, 2) # ReadFrom (0x42, 0x12, 2) # ReadFrom (0x42, 0x12, 2) # ReadFrom_Mem (0x42, 0x12, 2)

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, quite good play, For example, on the miCopython IDE we can run the following calculations directly, when the calculations are done not by the computer but by an embedded MCU connected to it.

# # mircoPython data representation and calculation small integer > > > 123 # 123 large integer > > > 1 < < 160 > 1461501637330902918203684832716283019655932542976 # floating-point Numbers > > >>> (1 + 2j) * 4j (-8+4j)

MicroPython application

MicroPython currently supports 32-bit ARM processors, such as PyBoard (STM32F405), Support nRF51822 (Micro :bit), Firebeetle-ESP32, WIPY, ESP8266 core master, CC3200 and so on.

More and more manufacturers are starting to adapt to MicroPython. After all, there is a potential user base. Maybe LiteOS will inherit MicroPython support in the future. It will be easier for people from the Internet to get involved in the development of the perception layer of the Internet of Things, which is a good thing for the development of the Internet. I hope there will be more support in the future, such as Hongmeng, which will also support more language packs.

Click on the attention, the first time to understand Huawei cloud fresh technology ~