• Introduction | recently, cloud + community technology salon “tencent open source technology” a successful ending. This salon invited a number of Tencent technical experts to discuss with many developers about Tencent open source, and in-depth revealed Tencent open source projects TencentOS Tiny, TubeMQ, Kona JDK, TARS and MedicalNet. This article is compiled by Teacher Ye Feng and edited by Tao Tao.
Main points of this paper:


  • TencentOS Tiny Open Source project introduction
  • TencentOS Tiny loT solution
  • TencentOS Tiny development practice
There will be Internet of things related activities online in the near future, a large number of development boards for free! Scan the code to pay attention to “yunjia community”, keep abreast of the latest developments.


TencentOS Tiny Open Source Project Introduction

What is TencentOS Tiny? TencentOS Tiny is Tencent’s open source streamlined real-time operating system for the Internet of Things. In people’s traditional impression, Tencent is mainly engaged in social networking and games, and it deals with the connection between people. In fact, Tencent also has a complete layout in the field of Internet of Things.
As can be seen from the following figure, TencentOS Tiny is the most critical link at the bottom of the whole product matrix in Tencent Internet of Things full stack product matrix, playing a role of diversion for cloud side massive data platform. We also hope TencentOS Tiny can lower the threshold of device side development, improve development efficiency, and help terminal devices and businesses of the Internet of Things to quickly access Tencent Cloud.
As shown in the figure below, TencentOS Tiny has been adapted to mainstream chips and modules, providing a minimal RTOS kernel, including task management, memory management, IPC communication, exception handling, etc. Based on RTOS, it also provides very rich iot components, including low power framework, OTA upgrade, KV storage, debugging tool chain, and more.
In terms of network connection, it also integrates some mainstream Iot protocols, including MQTT, CoAP, LoRaWAN, etc. At the same time, we provide very rich application cases, users can expand based on these cases, quickly realize the Application of the Internet of things.
In general, TencentOS Tiny has the following advantages: small size, low power consumption, rich IoT components, reliable security framework, good portability and convenient debugging means, which can meet the differentiated requirements of the terminal side of the Internet of Things.
TencentOS Tiny was officially open source on September 18 this year, and was ranked second on the GitHub hot list in a week after its release. Currently, TencentOS Tiny has received star 3500+, Fork 800+, and has cooperated with mainstream MCU and hardware manufacturers at home and abroad, and currently supports more than 50+ hardware platforms.

2. Landing solution based on TencentOS Tiny system

TencentOS Tiny has implemented some iot solutions through cooperation with both inside and outside the company. Here are two typical solutions to illustrate.
The first is the smart Container solution. TencentOS TINY provides control capability, and with the central control system and cloud AI, it completes the process of scanning code, opening cabinet, fetching goods and automatic settlement, realizing the scene of no sales. TencentOS Tiny provides gravity sensing capability to assist AI decision making and improve AI recognition rate in real situations where there may be uncontrollable situations, such as blocking goods.
The second solution is AI smart farming. In this solution, TencentOS Tiny mainly serves two aspects. In the environmental perception side, it collects environmental data such as temperature and humidity, ph, oxygen content and light intensity and reports them to the IoT cloud platform. Then, the cloud AI makes a series of instructions for greenhouse regulation according to the environmental data.
For example, if the light intensity is not enough, the decision algorithm will deliver the expected light intensity value, and the control side receives the command to adjust through the light supplement lamp. Finally, under the condition of optimal resource use, the effect of increasing crop yield can be achieved. TencentOS Tiny provides low power consumption capabilities to extend device life and ensure long-term stable operation. The ability to provide multi-scheme network equipment and secure links at the same time ensures data security.

Iii. Development practice based on TencentOS Tiny

Next, a small development practice is used to give you a better understanding of TencentOS Tiny. Based on the agricultural solution mentioned above, we also do a small end-to-end agricultural scenario solution this time.
The realization content includes: environment perception function, temperature, humidity and light intensity collection of the venue, and do simple equipment control. For example, turn on the lights, turn on the motor, and then upload the data to the cloud, while there will be a small program to achieve the environment status query and remote control.
The function of this exercise is very simple. The main purpose of this exercise is to let you know: How does TencentOS Tiny go from end-to-end to cloud and finally to applet? How did you get through? And what is the development benefit of using these products?
Before practice, it is necessary to introduce the whole process of practice and some preparatory work to everyone.

1. Customize the development board

First, this practice is based on TencentOS Tiny’s custom development board, which is very extensible. As shown in the figure below, the sensor extension on the left allows you to select sensors as needed. We chose an extension board for the agricultural scene, which included temperature, humidity and light intensity sensors, just enough to simulate our scene.
On the right you can select the communication module you need, supporting NB-iot, 2G, WiFi, etc. Here we choose esp8266’s WiFi module. Our TencentOS Tiny is running on a STM32L4 MCU. We hope TencentOS Tiny can collect data from the sensor and upload it to the cloud through the communication module.
2. TencentOS tiny Startup process
To make TencentOS Tiny run on this development board. You need to know the startup process: after the chip is powered on, the board level initialization will be done, and then the kernel initialization. Then create tasks and write task logic; Finally, start the kernel and start the task scheduling.
TencentOS Tiny’s task creation process is very simple. You need to configure the task priority and the task stack size. It also supports the use of ARM interface standard CMSIS to create tasks.
Here is a question. In fact, our function may be simple, writing a long loop or running naked form can also be implemented. Why do we have to use RTOS?
In fact, it can be in simple scenes, but the device side will have some real-time requirements. In the example shown below on the left, we need to implement a control function that can turn on the fill light in real time when the light intensity is too low.
This example may not be extreme enough, but suppose this is a smoke sensor that detects a potential fire and needs to be alerted immediately, in large loop order. If there is logic to delay at this time, this real time cannot be guaranteed.
If we use the multi-task of RTOS, extract the business logic with high real-time requirements to the task of high priority, and let the scheduling of high-priority preemption of RTOS ensure real-time performance.
TencentOS Tiny is a high-priority scheduling policy by default, which means that as long as the high-priority task does not voluntarily abandon the CPU, it will always hog the CPU.
Of course, TencentOS also supports the scheduling policy based on time slice. Multitasking also allows for asynchronous synchronization mechanisms such as mutex, semaphores, events, and queues, the ability to decouple business logic, and the ability to enjoy components provided by RTOS, all of which reduce development.

3. The communication module is networked

Internet of things solutions, the most important is to be connected. The development board we use is based on module communication, complex network protocols are handled by professional modules, and business logic is also run on MCU. If the business logic needs network communication, it can be achieved through the AT command of the serial port to control the traffic module.
The advantage of this is that business development does not need to pay attention to the complex network protocol stack at the bottom and can focus on the business logic. The possible problem is: because the AT instruction set of communication module may be different, if the module is replaced, it needs to be re-adapted, and there is no unified standard for the adaptation process, and there is no unified convention for business-oriented communication interface.
TencentOS Tiny provides an AT adaptation framework to address these issues. The AT framework implements a common interaction logic between MCU and module asynchronously sending and receiving, and a simple and unified interface provided by the business-oriented SAL layer. This way, if you need to adapt a new module, This is essentially an AT-based framework to implement the abstract interface of the SAL layer.
After this adaptation, the application development provides a simple and unified similar Socket interface (CONNECT, Send, RECV), without paying attention to the low-level details. In addition, we may replace other communication modules in product development. After interface abstraction, you do not need to modify the following business logic, but only need to re-adapt another module.

4.. Data on the cloud

After networking through the communications module, the next step is to put that data into the cloud. TencentOS Tiny now also integrates some mainstream iot protocols, including SDK of Tencent Cloud. You only need to do some configuration on the side and cloud, and then you can enjoy the cloud products and services provided by Tencent Cloud, including: device management, device shadow (saving the latest status of devices, and application layer isolation), etc. Data can also be forwarded to other Tencent cloud services through the rule engine, including cloud database, message queue, big data processing and so on, generating more value.
The end – to – end link is apps, mainly mobile apps. Applets are actually a good choice for iot applications because there is often a need for device interaction for iot applications. For example, if you want to connect a speaker without Internet connection to WiFi, you need to provide WiFi hotspot, WiFi password and other information. However, there is no input for Internet of Things devices. In this case, the APP needs to have bluetooth capability. Relying on the interface provided by wechat, small programs can easily call the hardware capabilities of mobile phones such as Bluetooth, WiFi and NFC to realize the scene of device interaction.
In addition
The small program itself is easy to spread, no installation, at your fingertips, and so on. These flexible and portable characteristics are also very good
The entrance.Moreover now small program combined with cloud development can reduce a lot of development costs, such as cloud development can provide some basic skills, including cloud, cloud storage, cloud database function, also be exempted from the domain name registration and record filing process, provide the elasticity of the cloud scale, natural CDN acceleration, native integration WeChat SDK, WeChat open interface without authentication, etc., Saves a lot of interface development work.
Therefore, in general, the small program & cloud development relying on the device capabilities provided by wechat and the cloud resources provided by Tencent Cloud can help the Internet of Things application launch and iteration with high efficiency and low cost.

5. Final practice

Then start to practice, the intermediate use of source engineering, compilation environment and so on have been ready. First of all, to achieve the first function, we collect data and do real-time feedback of equipment control. Based on reading the sensor values, we have to do a control logic: if the light intensity is too low, we turn on the lights, turn on the fan; If the light intensity returns we turn off the lights and then turn off the fans.
And then we do the second task, which is to quickly upload the data from these sensors to the cloud, which is an example of end-to-end cloud docking. Here the business logic is also very simple, the first is through the module for networking, networking success, will be an MQTT connection. We choose to connect to Tencent IoT Hub, and then subscribe to the device theme for receiving downlink messages. The following is a logic for sending and receiving.
Because devices access the cloud requires authentication and certification, so there are some configuration needs to be configured in the clouds, open the console of tencent cloud, is tencent cloud to receive Internet communication platform, the need to create a product first, then there is the new equipment, access to the device key, and then make a copy of the key, in the end side configuration.
AT this time, the AT module has been connected to the Internet, and then connected to WiFi. After the connection is successful, the AT module will report the current environment status to the cloud platform. When you open the cloud platform, you can see that the device is already online and the reported environment information has been received in the shadow of the device. Meanwhile, the Internet of Things platform of Tencent Cloud also provides debugging tools, so we can try whether the downlink works. We also use a simple JSON interaction to control the light on.
At this point, we have very convenient access to the cloud through the iot components provided by TencentOS Tiny.
Then docking small program, will open the small program, small program mainly to achieve two very simple functions: the first is to refresh the query, and then to achieve a simple control. The main is to achieve two cloud functions, one is the query cloud function, we need to build a first.
For APP, there is no need to pay attention to the link of the whole device, because the device has been uploaded to the shadow of the device. What we need to pay attention to is how to use Tencent’S API, that is, how to call the API device? How to interact with the device, that’s what Tencent’s Cloud iot platform does.
Read the API documentation, Tencent cloud has provided a very complete API, while Tencent cloud provides some more convenient tools, it can generate some mainstream language API SDK. For small programs, we use Node JS, need some request parameters, request OK means that the API can be used directly, automatically generated API SDK code, and then can directly use the small program cloud function, the same way we can achieve control.
Through TencentOS Tiny and Internet of Things communication platform and small program cloud development can quickly achieve end-to-end small applications, its function is very simple. Another benefit is that it acts as a decoupling for development, so for APP development, you only need to focus on the cloud API; For embedded development, I can just upload the data to the cloud, which is also very low-cost.
TencentOS Tiny itself is open source and free, and cloud development, including Tencent Cloud, has free quotas for iot products. For some start-up teams, it’s a quick and low-cost way to get things up and running.

Q & A

Q: Hello, teacher, I would like to ask whether it is possible for developers to design tasks and programs directly based on TencentOS Tiny’s development template to make other solutions and small programs? What do you mean by portability of TencentOS Tiny at the beginning?
A: TencentOS Tiny provides A lot of rich cases of Internet of Things for the user layer. Users can directly use these cases for extension, as long as the hardware support is ok. Currently, the mainstream chip modules at home and abroad are supported. Even if it is not supported, TencentOS Tiny’s components are loosely-coupled and easy to port. If you have embedded experience, the kernel can be ported in half an hour, and there is a detailed migration document on GitHub for reference.

introduction

Ye Feng, Tencent engineer, currently responsible for TencentOS Tiny IoT solution-related projects, including smart retail, smart agriculture, etc.