Sharing of the whole process of smart door lock development

This article is shared from China Mobile's OneOS WeChat public account " Smart Door Lock Development Whole Process Sharing ", author: Xiao O.

<<<Python is recognized as a simple and easy-to-learn language in computers. It has a simple and friendly syntax, rich library resources and high-quality documentation, and has strong and active community support. It is chosen as an entry language; while embedded software development has high comprehensive requirements for knowledge, and entry is difficult, not only requires solid basic computer knowledge and basic hardware capabilities, but also needs to master complex and difficult assembly, C language and various The use of this kind of chip platform debugging environment, many small partners want to get started but have no way to start. Python and embedded software development were originally unrelated, but MicroPython appeared and combined the two very well.
"MicroPython", as the name implies, is "micro Python", which is a lightweight implementation of Python3 that includes part of the standard library of Python and is optimized for resource-constrained operating environments such as microcontrollers. MicroPython is currently open source maintenance on GitHub, and complies with the most open MIT open source license. Due to the low difficulty of getting started, there are various applications based on MicroPython, including smart watches, electronic badges, motor control, gateways, smart cars, etc.
OneOS currently also supports MicroPython components, which can be easily enabled through the configuration tool. At present, the Wandu development board has fully realized MicroPython adaptation. Interested students can experience MicroPython on the Wandu development board. >>>

Recently, Xiao O conducted an actual battle with MicroPython - the development of a smart door lock solution based on MicroPython. Without further ado, let’s look at the results:

Click to jump to the video

All of the above functions are developed using MicroPython, and hardware control can be achieved by calling the functional interface provided by MicroPython. Application development engineers do not need to pay attention to the underlying hardware principles. They only need to understand the corresponding functional interfaces and focus on business logic. The rapid development of such a product reduces the threshold for product development.

The following Xiao O will take you to understand the process of realization in depth.

Click to jump to the video

Introduction to the overall plan

This solution is a cloud-pipe-terminal integration solution.

Cloud: Access the China Mobile IoT OneNET platform through the LwM2M protocol, and the OneNET backend can push data to the business platform. Moreover, this solution supports the China Mobile Internet of Things security system, and the business platform can invoke the capabilities of the security service platform for data encryption and decryption and signature verification.
Guan: The terminal device is connected to the Internet of Things through the NB-IoT network.
Terminal: The terminal supports fingerprint, password, bluetooth, door card unlocking methods, built-in NB module, eSIM chip and plug-in card can be selected from two, support voice broadcast, temperature and humidity monitoring, tamper monitoring, door status monitoring and other functions

Standby current is less than 50uA. The applet supports status viewing, configuration delivery and Bluetooth unlocking functions, and can be used for secondary development.

hardware solution

The terminal hardware uses HC32F460 as the main control chip, the module uses M5310-A , and other devices such as fingerprint modules and sensors also use mainstream devices in the smart door lock industry, and the hardware cost is basically the same as the market products. The hardware design adopts the form of core board + expansion board, and the functions of each module can be tailored as optional functions.

software solution

The program software is divided into two parts: firmware and application scripts.

firmware development

The firmware part is based on OneOS+MicroPython , and encapsulates the MicroPython layer peripheral function interface for each functional module, which is provided in the form of machine and device libraries.

The official website of MicroPython gives information on the standard libraries it supports, including JSON library, regular expression library, SSL/TLS library, mathematical function library, etc. These libraries can also be flexibly tailored according to application requirements in the process of firmware development , interested friends can refer to:
( https://os.iot.10086.cn/doc/micropython/4.libraries/micropython%20standard%20libraries/micropython%20standard%20libraries.html )

The Machine library is mainly a package for on-chip hardware resource capabilities, such as PIN, IIC, UART, PWM, etc. The following takes IIC as an example to show you the related interface functions.

class machine.I2C

The machine.I2C class is a hardware class under the machine module, which is used for I2C configuration and control, and provides operation methods for I2C devices.

Constructor

instance method

For the specific usage of each method, please refer to the OneOS official website

(https://os.iot.10086.cn/doc/micropython/4.libraries/micropython%20specific%20libraries/machine/I2C/I2C.html)

The device library is a package for board-level hardware capabilities, such as temperature and humidity sensor collection, battery voltage monitoring, fingerprint collection and comparison, and motor control. We have encapsulated some classes for some common board-level functions, but these classes will not be used in a project, so it needs to be flexibly adjusted according to the hardware design during the production process of the firmware. The following takes the motor as an example to show you the relevant interfaces.

class device.Motor

The device.Motor class is a hardware class under the device module, which is used to configure and control the motor drive module, and provides operation methods for the motor drive device.

constant

Constructor

instance function

For the specific usage of each method, please refer to the OneOS official website

(https://os.iot.10086.cn/doc/micropython/4.libraries/micropython%20specific%20libraries/device/Motor/Motor.html)

The functions of machine and device in the firmware are implemented based on the OneOS driver framework. The software needs to complete the OneOS adaptation and support the driver framework components, and then implement the MicroPyhton library functions based on the driver framework. The underlying capability development involving the combination of software and hardware is completed in the process of firmware development, and MicroPython API is provided for the application layer.

Application Script Development

The application part is developed based on the MicroPython API provided by the firmware. The development of application scripts does not need to build a compilation environment for the chip, nor does it require a dedicated integrated development environment. It only needs to use software with text editing functions. In the development process, the compilation process is omitted. After the script is modified, it can be directly downloaded for functional verification.

In this solution, in order to cooperate with the tailoring capability of the hardware, the application script is also developed in a modular way, and the coupling between the modules is extremely low, which can facilitate the addition and deletion of the functions of the modules. Each module is a separate PY file, and all modules are managed through the event control framework and the state control framework.

event control framework

The event control framework defines the event types of all modules, and is responsible for managing the registration and deregistration functions of the handler functions corresponding to the events of each module.

The event control framework dynamically maintains two tables, one is the Event Map table, which stores all the events that the current event control framework can handle and the handler functions corresponding to the events. When other modules are registered, the events they want to register and the corresponding processing functions will be written into this table. One event can correspond to the processing functions of one or more different modules; the other table is the Event FIFO, when an actual event occurs , the corresponding event will be written into the Event FIFO by each module, and the event control framework will enter and retrieve the Event from the Event FIFO, consult the Event Map table, and call the corresponding processing function in turn.

event control framework

In this scheme, system, key, fingerprint, RFID, alarm, audit, module communication and Bluetooth communication are defined, and there are 84 events in 8 categories.

State Control Framework

The state control framework defines the state of the system and modules, and manages the initialization, modification and query of the state in a unified manner.

The state control framework maintains a table, which records the current state of the system and other modules, and the initial state is initialized by the module when it is registered. The state control framework provides modification and query interfaces for use by other modules.

In this scheme, the state control framework mainly records the system's working state, door lock state, button state, module connection state, sensor state , etc.

other modules

Other modules mainly revolve around the event control framework and the state control framework, and realize the realization of event handling functions, event registration, state modification and other functions.

Taking the Motor module as an example, we can look at the relevant code in detail:

Summarize

Finally, let’s talk about some feelings about using OneOS MicroPython for smart door lock application development:

  1. Application development and debugging no longer need to build a compilation environment, and it can be developed by using software with text editing function, which is relatively simple. MicroPython's REPL can verify the correctness of code statements at any time, and can also view various information such as running status variables at any time.
  2. Since the existence of the MicroPython interpreter naturally isolates the driver and the business, under the premise of interface standards, firmware and applications can be developed in parallel, greatly reducing the cost of communication and time, shortening the development cycle of product prototypes, and forming "1 solution + 1" firmware + n applications" mode.
  3. The flexibility of the Python language itself makes programming no longer as careful as using C. Should variables be integer or floating point, 8-bit or 16-bit? Does the variable exceed the range after operation? Is the pointer initialized? The string processing function is not powerful enough and need to write one? These problems do not need to be considered in python development. If you are familiar with Python syntax, you can even use advanced functions such as closure functions and generators. In addition to MicorPython's own library that can be called directly, with the help of Python's ecology, some existing Python library resources can also be ported for use.
  4. The status of C language in embedded development is difficult to shake. MicorPython cannot replace C language for embedded development. We hope that the combination of MicroPython and C can better cope with some application scenarios and solve the current fragmentation of IoT needs. , change frequently.

The sharing of the MicroPython smart door lock solution has ended so far. If you have a small partner who wants to learn embedded but is difficult to get started, you can contact us for further understanding. At the same time, you are welcome to explore more applicable scenarios of MicroPython with us.

OneOS is a lightweight operating system launched by China Mobile for the Internet of Things. It features tailorable, cross-platform, low power consumption, and high security. It supports mainstream CPUs such as ARM Cortex-M/R/A, MIPS, and RISC-V. The architecture is compatible with POSIX, CMSIS and other standard interfaces, supports Micropython language development, and provides graphical development tools, which can effectively improve development efficiency and reduce development costs, helping customers develop stable, reliable, safe and easy-to-use IoT applications. Official website address: https://os.iot.10086.cn/
OneOS software address: http://www.oschina.net/p/cmcc-oneos
OneOS project address: https://gitee.com/cmcc-oneos/ OneOS
OneOS technical exchange group: 158631242

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324132377&siteId=291194637