Python light application: startup method

1 Introduction

Python light applications are developed based on MicroPython. MicroPython inherits the elegant and brief syntax of Python and provides a convenient embedded hardware operation library.

As an IoT development board launched by Alibaba Cloud Intelligent IoT, HaaS100 is adapted to the MicroPython operating engine, provides a variety of rich hardware operation interfaces, and provides Alibaba Cloud IoT platform and cloud AI-related capabilities.

Using this development board, you can easily implement hardware control, cloud AI, and cloud interoperability through Python programs.

For more information about Python light applications, please refer to the following link:

Overview of Python Light Applications

This article will introduce how to choose the most suitable startup method to improve the efficiency of Python light application development.

 

2. Effect video

Python light application startup method

 

Two execution methods are shown in the above video:

  • Interactive
  • File execution

Next, we will expand the explanation based on these two methods, so that developers can correctly choose the most suitable startup method in daily development.

 

3. Environmental preparation

3.1, hardware

  1. One HaaS100 development board
  2. Power supply one
  3. micro usb one
  4. sdcard one

3.2 Software

  1. The latest HaaS100 open source code
  2. Serial tool

 

4. Firmware preparation

Can follow the Python application of light to get started quickly in a way, direct download firmware burning, also can download the source code, compiled programming operation.

The functions of the main program finally burned into the machine are as follows:

  • Register a Python command
  • Determine whether to enter interactive or file mode according to the incoming parameters

 

4.1, code analysis

After downloading the latest open source code of HaaS100, go to the application/example directory, you can see the sample project of the Python light application:

py_engine_demo This demo entry procedure is appdemo.c. Two things are mainly done in this code:

  1. Register python command
  2. Start the python virtual machine

 

Register python command

static struct cli_command command = {

    .name     = "python", /*命令名称*/

    .help     = "start micropython ", /*命令的帮助描述信息*/

    .function = handle_identity_cmd   /* 命令的实现函数*/

};



int application_start(int argc, char *argv[])

{

    # 调用系统cli命令注册接口

    int  ret = aos_cli_register_command(&command);

    if (ret) {

        printf("register micropython command failed \n");

    }else{

        printf("register micropython command succeed \n");

    }

}

 

Start the python virtual machine

int python_main_entry(void *p)

{

    //printf("global_argc = %d;global_argv = %p;%s;\n", global_argc, global_argv, __func__);

    tick_t   sleep_time;

    sleep_time = krhino_ms_to_ticks(10); /*  10ms to ticks */

    #sleep 10ms 确保新的task 在默认task之后执行

    krhino_task_sleep(sleep_time); /*  sleep 10ms */


    mpy_thread_args* args = (mpy_thread_args*)p;

    if (args == NULL)

    {

        printf("%s:args is illegal\n", __func__);

        return -1;

    }

    # 启动python 虚拟机

    mpy_init();

    mpy_run(args->argc, args->argv);

    # 退出虚拟机,释放内存

    free_mpy_thread_args(args);


    return 0;

}


static void handle_identity_cmd(char *pwbuf, int blen, int argc, char **argv)

{

    //printf("argc = %d;argv = %p; %s;\n", argc, argv, __func__);

    # 申请需要传递到新的task 相关信息的内存

    mpy_thread_args* args = alloc_mpy_thread_args(argc, argv);

    # 启动一个新的task,去执行python命令

    aos_task_new("python_main_entry", python_main_entry, (void*)args, 1024*20);

}

 

4.2, compile

In the root directory of AliOS Things code, execute the following command

aos make py_engine_demo@haas100 -c config

aos make

After compiling, in out / py_engine_demo @ haas100 / binary / directory, it will generate a [email protected] while the platform / mcu / haas1000 / release / write_flash_tool / ota_bin / directory will generate a new file littlefs.bin 

 

4.3, burn

Refer to the Python application of light quick start in programming aspects. If it is a windows computer, the firmware package for burning is located in the platform/mcu/haas1000/release/ directory

 

 

5. Interactive operation

After the system is turned on, enter Python in the serial command line without any parameters, and enter the interactive mode by default.

In this mode, you can enter Python code to execute in real time, and display the execution results. Because it is very convenient, just like a human-machine dialogue, one input and one output, so this mode is called friendly_repo, and there is a raw_repl corresponding to friendly_repo.

  • friendly_repl

Man-machine dialogue mode, convenient for quick debugging and programming

Exit: ctrl+d

Switch: ctrl+a twice to enter raw_repl mode

 

  • raw_repl

Native mode, inconvenient for interaction, convenient for file stream transmission and execution

Switch: ctrb+b switch to friendly_repl mode

In raw_repl mode, the file stream can be directly pushed to the machine for execution through the serial port. This part of this article will not be introduced for the time being.

 

6. File execution

File execution is divided into two situations:

  • The file is on the HaaS device
  • The file is on the computer (PC)

 

6.1. Execute files on HaaS equipment

Currently, on the HaaS100 file system, files can be placed in the built-in /data partition or the external /sdcard.

data

The file on the /data partition is prefabricated during the compilation phase. The advantage is that it will always exist. The disadvantage is that if it is updated or modified, it needs to be compiled or burned. /platform/mcu/haas1000/prebuild/data/ is a more directory of the data partition. Normally, all files in this directory will be compiled into littlefs.bin and accessed in the system through /data/ + specific path.

By default, the /data directory contains two subdirectories

  • /data/lib

Python official library directory, please do not modify

  • /data/python

Executable python case source files

 

sdcard

The default path of the sd card on the device is /sdcard. Compared with data, its advantage is that it does not need to be flashed, and the space is large enough. Edit directly on the PC and save it to the sdcard, and then plug in the device (currently does not support hot swap, the machine needs to be restarted after plugging in)

 

6.2, execute the file on the PC

Executing files on the PC can also be divided into two methods, file streaming execution and file download execution. Because the file flow execution is more complicated and requires a series of tool support, this article will not introduce the file flow execution for the time being.

File download refers to downloading the file on the PC to the data or sdcard of the device, and then executing the downloaded file through Python. Currently, the HaaS100 device already supports the tftp function, so we can download files from the PC to the device via tftp. The tftp function method is as follows:

  • Device-side networking (pay attention to replace ssid and password)

python   /data/python/network/ConnectWifi.py  "SSID"  "PassWord"

After the connection is successful, the ip address of the device will be printed in the log

  • Start the tftp server on the device

tftp server start

  • The PC and the device are connected to the same LAN to ensure that the PC can ping the device ip

Start the tftp client on the  PC side, and pass the PC file to the specified path on the device side through the put command (it can be done through graphical tools or scripts)

The following is a simple script: send_file.sh

#ip="192.168.1.173"

ip=$1

filename=$2

#filename="test.py"

tftp ${ip}  << !

binary

put ${filename} ${ip}:/sdcard/${filename}

quit

Assuming the device ip is: 192.168.1.173, the /sdcard file that needs to be pushed to the machine is test.py, enter it in the computer terminal

bash send_file.sh 192.168.1.173 test.py

 

Developer support

MicroPython inherits the easy-to-learn and easy-to-use features of Python, and at the same time provides a basic library package based on embedded hardware, allowing developers to easily conduct embedded development in real time through an interactive environment, making embedded development simple and convenient .

For more information about MicroPython embedded development, you can scan the code to join the Dingding developer group or scan the code on WeChat to follow the official account of the HaaS technology community

 

Guess you like

Origin blog.csdn.net/HaaSTech/article/details/115216834