Research and implementation of key technologies for embedded device management based on FreeRTOS (study 4)

Hardware platform

The hardware selection of the terminal equipment is mainly based on the STM32 chip as the processor, the on-board clock circuit and power circuit, and the external communication serial port, and the AM2302 temperature and humidity sensor is mounted to collect environmental related information in real time, and the collected information is communicated through NB-IoT The module is uploaded to the cloud to realize the entire environmental monitoring function.
insert image description here
In order to meet the requirements of low power consumption and low cost in this article, the main controller
chooses STM32F103RCT6 as the main controller for the hardware platform. This processor is a 32-bit microcontroller based on the ARM Cortex-M3 core STM32 series, and the RAM is 48K , the program memory capacity is 64KB and requires a voltage of 2V~3.6V.

STM32F10 series processors are low in cost, high in performance, and low in power consumption. They are produced and designed by ARM and are often used in various hardware systems.
The STM32F103 processor has three low-power modes of stop, sleep and standby mode, and when the STM32F103 processing reaches the processor's maximum 72MHz operation, it only consumes a circuit of 36mA, and it is as low as 2uA in standby mode.

The wireless communication module
communicates with the main control chip through the serial port. BC28 can limit the size of the device and reduce the cost of manufacturing the device. It is suitable for small wearable devices, smart portable instruments and other terminals that require compact design.

IR Infrared Transmitter
Infrared transmitters and receivers appear on various remote control devices, such as TV, air conditioner remote control. The technology works by a component that sends, receives, and translates blinking infrared signals under a specific protocol.

The most common IR remotes usually use a diode that produces infrared light, and the receiving component is usually a photodiode.
The input pin PWM is connected to the PWM output pin of the main chip.
The PWM pin at the input end is the infrared signal output from the single-chip microcomputer, which is connected to the PWM pin of the single-chip microcomputer to control the state of the transistor.
When the PWM is high, the transistor is turned on and the LED is bright.

insert image description here
The IR Receiver or Infrared Receiver receives information from the IR Transmitter and decodes the signal.
A common application for an IR receiver is a TV remote, the TV's remote sends an infrared signal when any key or button is pressed. The IR receiver on the TV will take these signals and, after decoding them, send them to the control circuitry inside the TV, which will then perform the necessary actions.

Study the infrared remote control protocol of the air conditioner, so that the original can dynamically adjust the environmental parameters under the control of the platform or when the temperature and humidity trigger the threshold.

Software architecture design and implementation

The design of this paper uses the LwM2M protocol for device management, and the following interfaces need to be implemented in the terminal device:

  1. Device registration interface: The purpose of the registration interface is to interconnect the terminal with the LwM2M server, and transmit and store the relevant information of the LwM2M client in a simplified message format on the LwM2M server.
  2. Client/Server interaction interface: the master controller of the device management and service implementation interface is the LwM2M server, the server sends instructions to the terminal, the terminal responds to the instructions and sends the response message to the server
  3. Message reporting interface: Allows the client to actively update its own data to the server.
  4. Device logout interface: make the connection between expired terminal or invalid terminal port and server.

In order to shorten the development cycle, the mobile OneNET cloud platform was selected as the server side for terminal function testing.

Resource Abstraction Model

LwM2M abstracts most of the device functions, so that these functions and data types are described as fixed object-resource types, which shields developers from the specific name and completed functions of the device.

An object can contain multiple instances, and each instance can define multiple device resources.
A device's resource properties can be located by a unique URI.

insert image description here
A server manages multiple device instances, and each instance has its own attributes.
For example, the environmental monitoring terminal in this paper may contain multiple device instances, and each instance has attributes such as humidity, temperature, and switch.

Each of the devices, instances, and resources has a corresponding ID, which is registered to the server at the same time when the resource is registered, and relevant information is retained in the device.

Among them, the instance ID is to distinguish different instances of devices that perform the same function. For example, a device will eventually form a URI in the form of "ObjectID/InstanceID/ResourceID" according to its own attributes. URI is the unique identifier of the device in the management system. Communicate with fixed devices through URI, and perform operations such as reading and writing fixed resources.

abstract model design

LwM2M allows users to define objects and resources according to the equipment used and the functions completed by the equipment.
insert image description here
The definition of a resource requires not only ObjectID, but also InstanceID and ResourceID. These two IDs give the object a description of the number of instances and a description of the data format, type, and allowed operations, respectively.

For example, the temperature monitoring in this article is an objective attribute that can only be read and cannot be changed. Therefore, the object resource types are shown in the table.

insert image description here

Device registration interface

The registration interface of the device is mainly used for the registration function of the device that has not been registered on the platform, and the online notification function of the registered device to the cloud platform.
insert image description here
Among them, the device creation function is named nb_device_create(), and its main function is to describe and fill in the URI, lifetime and specific callback functions of the device. The callback functions mainly include "reading, writing, and executing", respectively, write_callback() , read_callback(), execute_callback().

When sending instructions to the platform, if the connection to the platform is successful, it will return the NO_ERROR status code and change the state to STATE_REGISTERED. If the transmission fails due to network problems, it will return the ERROR_500 status code.

insert image description here
state indicates the current status of the device, such as registration completed, registration failed, unregistered, etc.
The state needs to determine whether to register with the platform when the device completes the task of interacting with the platform, otherwise successful communication cannot be guaranteed.

Client/server interaction interface

According to the interface document of the OneNET cloud platform, this paper designs and implements the client/server interaction interface on the device side. The design of the interactive interface mainly includes three commands of "read", "write" and "execute" and the design of the logical functions completed by these three commands.

Before executing the instructions, it is necessary to distinguish and judge the issued instructions to ensure that the correct instructions are executed.
insert image description here
After receiving the instruction correctly, start to execute the specific logic function.
This article refers to the interface of the "read" command as the device resource reading interface, which is used by the server to obtain real-time device internal data such as sensor resource data, battery power, etc. by issuing commands, so that device-related information can be obtained remotely on the cloud platform .
For example, use the device resource reading interface to read and report the temperature and humidity values ​​obtained by the device in real time.

insert image description here
When the platform issues instructions, it will carry the URI of the query device resource to locate the specific resource. The software design of this paper includes a node structure _nb_node_t. Such a data structure constitutes a linked list. To determine whether an instance exists is to find the relevant Corresponding resources, and according to the resource situation of the sensor, the data structure _nb_value_t of the resource is designed to record data of different data types and data attributes, etc., and each node carries a value of this structure type.

insert image description here
insert image description here
The function of remotely controlling the infrared transmitter to turn on and off the air conditioner uses the device resource writing interface. If the value filled in is false, the infrared command to turn off the air conditioner is sent, otherwise the infrared command to turn on the air conditioner is sent.
insert image description here
When the platform issues instructions, it will carry the URI of the query device resource to locate the specific resource, and determine whether the resource instance exists in the node list.

In order to cope with the need to restart the device due to an error in the device, an execution callback is designed. The function of this callback is to reset and restart the device.

Device information reporting interface

Before the device reports information, it needs to perform status monitoring, that is, whether the device is successfully registered on the platform, and such an operation is a highly multiplexed operation, so this article designs a structure _nb_transaction_t to complete such a device self-inspection Capabilities, which represent preparatory work that needs to be done by the device.

Guess you like

Origin blog.csdn.net/Caramel_biscuit/article/details/131558896