Data storage and sending method and device and Internet of Things system
By adopting buffer queue-based data storage and sending methods in the Internet of Things system, the problem that the ESP32 module cannot process in time when faced with bursts of large amounts of data or disordered high-frequency data requests is solved, and the data processing efficiency and system stability are improved.
Patent Information
- Application Number
- CN202510144843.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-10
- Publication Date
- 2025-03-14
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In the Internet of Things system, when facing bursting large amounts of data or disordered high-frequency data requests, the ESP32 module cannot process data in a timely and quickly and efficient manner, resulting in untimely and even partial data loss, affecting the performance and response speed of the entire Internet of Things system.
The data storage and transmission method based on the buffer queue is adopted to realize the orderly storage and periodic transmission of data by buffering the tail pointer and head pointer in the buffer queue structure. The buffer queue structure includes a data buffer, a head pointer, a tail pointer and a queue length variable. It triggers data transmission through a timer interrupt timing to avoid data accumulation.
It effectively improves data processing efficiency, reduces the risk of data loss, ensures the stable and efficient operation of the Internet of Things system, and avoids system failures or abnormalities caused by data processing lag.
Smart Images

Figure CN119629140A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a data storage and transmission method, device and Internet of Things system. Background Art
[0002] With the rapid development of IoT technology, wireless communication modules are widely used in various IoT devices to realize data interaction between devices and networks. However, in actual application scenarios, when wireless communication modules such as ESP32 are processing data, due to their internal resource limitations and the need to handle multiple complex tasks at the same time, such as network connection management, multiple protocol parsing, data transmission and reception, etc., they are unable to process data in a timely, fast and effective manner when facing the pressure of sudden large amounts of data or disordered high-frequency data requests, resulting in untimely data processing and even partial data loss, which in turn affects the performance and response speed of the entire IoT system. Summary of the invention
[0003] In view of the problems existing in the prior art, the present invention provides a data storage and transmission method, device and Internet of Things system.
[0004] The present invention provides a data storage and transmission method, the method comprising: Based on a tail pointer included in a buffer queue structure, the cached data is stored in a data buffer included in the buffer queue structure in the form of a buffer queue, and the tail pointer is updated based on the length of the cached data; Based on the head pointer included in the buffer queue structure, the data to be sent is obtained and sent out periodically, and the head pointer is updated based on the length of the data to be sent; Among them, the buffer queue structure includes the data buffer, the head pointer, the tail pointer and the queue length variable. The data buffer is used to store the data to be sent, the head pointer points to the starting position of the data to be sent in the buffer queue, the tail pointer points to the end position of the latest data stored in the buffer queue, and the queue length variable records the length of the data currently stored in the buffer queue.
[0005] Optionally, storing the cached data in a data buffer area included in the buffer queue structure in the form of a buffer queue based on a tail pointer included in the buffer queue structure comprises: When it is determined that the queue length variable is less than the maximum queue length of the buffer queue, the cached data is sequentially stored in the data buffer based on the tail pointer.
[0006] Optionally, based on the head pointer included in the buffer queue structure, obtaining the data to be sent and sending it out periodically includes: When it is determined that there is data to be sent in the buffer queue, the data to be sent is obtained based on the head pointer included in the buffer queue structure and is sent out periodically.
[0007] Optionally, acquiring the data to be sent and sending it out periodically includes: Data transmission is triggered by timer interrupt.
[0008] Optionally, the time interval of the timer interruption is configured according to the data processing capability and data sending frequency of the data receiving device.
[0009] Optionally, the method further comprises: When the head pointer and the tail pointer reach the end of the data buffer, a circular process is performed to make them point to the beginning of the data buffer again, thereby realizing a circular queue function.
[0010] Optionally, the method further comprises: When the data buffer is full, returning a prompt message; and / or When the data buffer is empty, a prompt message is returned.
[0011] The present invention also provides a data sending device, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, wherein the processor implements the data storage and sending method as described above when executing the computer program.
[0012] The present invention further provides an Internet of Things system, comprising a data sending device and a data receiving device linked based on a communication interface; wherein the data sending device adopts the above-mentioned data sending device.
[0013] Optionally, the data sending device is a STM32F413 single chip microcomputer, and the data receiving device is an ESP32 module.
[0014] The data storage and transmission method, device and Internet of Things system provided by the present invention. The method includes storing cached data in the form of a buffer queue in a data buffer based on a tail pointer included in a buffer queue structure, and updating the tail pointer based on the length of the cached data; based on a head pointer included in a buffer queue structure, obtaining data to be sent and periodically sending it out, and updating the head pointer based on the length of the data to be sent; the buffer queue structure includes a data buffer, a head pointer, a tail pointer and a queue length variable, the buffer is used to store the data to be sent, the head pointer points to the starting position of the current data to be sent in the buffer queue, the tail pointer points to the end position of the latest data stored in the buffer queue, and the queue length variable records the length of the currently stored data. Based on this method, the data receiving device no longer faces the pressure of sudden large amounts of data or disordered high-frequency data requests, which effectively improves the data processing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0016] Figure 1 A schematic diagram of a data storage and sending method flow chart provided in an embodiment of the present invention.
[0017] Figure 2 A flow chart of the enqueue operation function provided for the implementation of the present invention.
[0018] Figure 3 An Internet of Things system provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0019] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0020] Figure 1 A schematic diagram of a data storage and transmission method flow chart provided in an embodiment of the present invention, such as Figure 1 As shown, the method includes: Step 100: based on a tail pointer included in a buffer queue structure, storing cache data in a data buffer area included in the buffer queue structure in the form of a buffer queue, and updating the tail pointer based on the length of the cache data; Step 101: based on the head pointer included in the buffer queue structure, obtain the data to be sent and send it out periodically, and update the head pointer based on the length of the data to be sent; Among them, the buffer queue structure includes the data buffer, the head pointer, the tail pointer and the queue length variable. The data buffer is used to store the data to be sent, the head pointer points to the starting position of the data to be sent in the buffer queue, the tail pointer points to the end position of the latest data stored in the buffer queue, and the queue length variable records the length of the data currently stored in the buffer queue.
[0021] In each embodiment of the present invention, the data transmitting device can be a single-chip microcomputer STM32F413, and the data receiving device can be ESP32 as an example for illustration, but it is not limited thereto. On the basis of this example, the purpose of the embodiment of the present invention is to provide a transmission buffer queue function design based on the single-chip microcomputer STM32F413, through which it can effectively solve the problem that it cannot process data in a timely, fast and effective manner in the face of the pressure of a large amount of sudden data or disordered high-frequency data requests, resulting in the phenomenon that data processing is not timely or even causes partial data loss, thereby affecting the performance and response speed of the entire Internet of Things system. The technical solution provided by the embodiment of the present invention can improve the overall efficiency of data processing and transmission, reduce the risk of data loss, and ensure the stable and efficient operation of the Internet of Things system.
[0022] In the technical solution provided by the embodiment of the present invention, a set of sending buffer queue functions can be designed based on the single-chip microcomputer STM32F413, and the functions mainly play a role in the system architecture composed of STM32F413 and ESP32.
[0023] Buffer queue structure definition: First, define a buffer queue structure for sending data in the STM32F413 program code. The structure contains the following key member variables: Data buffer: defines an array to store the data to be sent to ESP32. The size of the array is set appropriately based on the maximum amount of data expected in the actual application. For example, it can be set to be able to accommodate [X] bytes of data.
[0024] Head pointer: points to the starting position of the data to be sent in the buffer queue, initialized to 0, indicating the head position of the queue.
[0025] Tail pointer: points to the end position of the latest data stored in the buffer queue, initialized to 0, dynamically updated as data is stored, and used to indicate the position where data can continue to be stored.
[0026] Queue length variable: records the length of data stored in the current buffer queue, to facilitate operations such as determining whether the queue is full or whether there is data to be sent, initialized to 0.
[0027] An Enqueue Function is also designed to store data from other modules (such as sensor acquisition modules, etc.) into the buffer queue structure defined above. The steps to implement this function are as follows: First, check the current state of the buffer queue and determine whether the queue is full by judging the relationship between the queue length variable and the preset maximum queue length (i.e., the data buffer size). If the queue length is equal to the preset maximum queue length, it means that the queue is full, and the function returns a prompt message (such as returning a specific error code to indicate that the queue is full and no more data can be stored).
[0028] If the queue is not full, the incoming data to be sent will be stored in the data buffer in sequence. Every time a byte of data is stored, the tail pointer is updated to point to the next location where data can be stored (this is achieved through operations such as tail pointer increment, and when the tail pointer reaches the end of the data buffer, a loop is performed to point it back to the beginning of the buffer, thereby realizing the function of a circular queue). At the same time, the queue length variable is updated accordingly, with its value increased by 1 to reflect the actual length of data stored in the current queue.
[0029] A dequeue function is also designed to take out the data in the buffer queue and send it to the ESP32 module for subsequent processing. The execution process is as follows: At the beginning of the function, the state of the buffer queue is checked first, and a prompt message is returned if the data buffer is empty. Specifically, it is determined whether the queue length variable is 0. If it is 0, it means that there is no data to be sent in the queue, and the function returns a prompt message (such as returning a specific error code indicating that no data is available).
[0030] If there is data in the queue, take out data of corresponding length from the head of the buffer queue (indicated by the head pointer), send the taken out data to the ESP32 module through the communication interface between STM32F413 and ESP32 (such as UART, SPI and other suitable communication interfaces, determined according to the actual hardware connection situation), and then update the head pointer to point to the starting position of the next data to be sent (the same loop processing is performed. When the head pointer reaches the end of the buffer, the loop processing is performed to point it back to the beginning of the data buffer to maintain the continuity of the circular queue and realize the circular queue function), and reduce the queue length variable by 1 (if one byte is sent) to reflect the amount of data remaining in the queue after part of the data has been sent.
[0031] Based on the buffer queue structure defined above, in an embodiment of the present invention, in the data storage phase, based on the tail pointer included in the buffer queue structure, the cached data is stored in the data buffer included in the buffer queue structure in the form of a buffer queue, and the tail pointer is updated based on the length of the cached data. In the data sending phase, based on the head pointer included in the buffer queue structure, the data to be sent is obtained and sent out periodically, and the head pointer is updated based on the length of the data to be sent, thereby realizing the process of data from caching to sending.
[0032] In the embodiment of the present invention, based on the tail pointer included in the buffer queue structure, the cached data is stored in the data buffer in the form of a buffer queue, and the tail pointer is updated based on the length of the cached data; based on the head pointer included in the buffer queue structure, the data to be sent is obtained and sent out periodically, and the head pointer is updated based on the length of the data to be sent; the buffer queue structure includes a data buffer, a head pointer, a tail pointer and a queue length variable, the buffer is used to store the data to be sent, the head pointer points to the starting position of the current data to be sent in the buffer queue, the tail pointer points to the end position of the latest data stored in the buffer queue, and the queue length variable records the length of the currently stored data. Based on this method, the data receiving device no longer faces the pressure of sudden large amounts of data or disordered high-frequency data requests, which effectively improves the data processing efficiency.
[0033] Figure 2 The enqueue operation function flow chart provided for the implementation of the present invention is as follows: Figure 2As shown, based on the above-mentioned embodiment, in the data storage stage, first determine whether the pointer is less than the tail pointer. If so, continue to determine whether the queue length variable is less than the maximum queue length of the buffer queue (it can be understood that if the pointer is greater than the tail pointer, it will overflow in the memory, that is, the data buffer initially provided is not sufficient), and when the queue length variable is less than the maximum queue length of the buffer queue, execute to store the data in the data buffer, specifically based on the tail pointer, store the cached data in the data buffer in sequence. When the queue length variable is not less than the maximum queue length of the buffer queue, point the current pointer to the head pointer position, overwrite the original data, and return a prompt message. After executing to store the data in the data buffer, update the queue length variable, specifically, the new queue length variable is the original queue length variable plus the stored data length.
[0034] In the data sending stage, the data to be sent is obtained and sent out periodically, including: triggering data sending by timer interrupt timing.
[0035] In order to further improve the real-time performance and efficiency of data processing, the embodiment of the present invention introduces an interrupt-driven mechanism in STM32F413 in combination with timing processing to control the execution of the buffer queue function. For example, a timer interrupt is set to trigger the interrupt service program at a certain time interval (the time interval is reasonably configured according to factors such as the data processing capability of ESP32 and the data transmission frequency requirements in the actual application scenario, such as setting it to trigger an interrupt once every [specific time period]). In the interrupt service program, the dequeue operation function is called to try to send the data in the buffer queue to ESP32, so that the data can flow to ESP32 continuously and orderly at a certain rhythm, avoiding the problem of slow processing caused by data accumulation. At the same time, when there is new data that needs to be stored in the buffer queue (for example, other modules generate new data to be sent), the queue operation function can be called through an external interrupt or a corresponding trigger mechanism to store the data in the queue in time for sending.
[0036] The following is a specific implementation example of the sending buffer queue function based on the single-chip microcomputer STM32F413 of the present invention, which can be appropriately adjusted according to specific needs in actual applications.
[0037] Hardware preparation: First, prepare the STM32F413 MCU development board and the ESP32 wireless communication module, and connect them through a suitable communication interface (such as the UART interface) to ensure that the hardware connection is stable and reliable and that data can be transmitted normally. At the same time, according to the specific IoT application scenario, connect the corresponding sensor module and other external devices to the STM32F413 to generate the data to be sent.
[0038] Software programming implementation: 1. Environment construction: Create a project for STM32F413 in a development environment (such as Keil, IAR and other commonly used embedded development environments), configure the corresponding compilation options, chip model and other basic settings, and ensure that the project can be compiled and run normally.
[0039] 2. Buffer queue structure definition and initialization: According to the buffer queue structure design described in the above invention content, a suitable programming language (such as C, C++, etc.) is used in the STM32F413 code to define the structure. The sample code is as follows (taking C language as an example): typedef struct { uint8_t dataBuffer[BUFFER_SIZE]; / / Define the data buffer, BUFFER_SIZE is the preset maximum buffer size int head; / / head pointer int tail; / / tail pointer int length; / / Queue length variable }SendBufferQueue; SendBufferQueue sendQueue; / / Define a buffer queue instance / / Initialization function void initBufferQueue() { sendQueue.head = 0; sendQueue.tail = 0; sendQueue.length = 0; } Enqueue operation function implementation: Write the code to implement the enqueue operation function. The example is as follows: int enqueueData(uint8_t data, int dataLength) { if (sendQueue.length == BUFFER_SIZE) { return -1; / / Return -1 means the queue is full and data cannot be stored } for (int i = 0; i <dataLength; i++) { sendQueue.dataBuffer[sendQueue.tail] = data[i]; sendQueue.tail = (sendQueue.tail + 1) % BUFFER_SIZE; / / Loop to update the tail pointer sendQueue.length++; } return 0; / / Return 0 means the data is successfully stored in the queue } Dequeue operation function implementation: Below is a code example of a dequeue operation function, which is used to send data to the ESP32.
[0040] int dequeueData() { if (sendQueue.length == 0) { return -1; / / Return -1 means there is no data to send in the queue } uint8_t dataToSend = sendQueue.dataBuffer[sendQueue.head]; / / Send data to ESP32 through UART and other interfaces. Here, we assume that the UART sending function is used and needs to be adjusted according to the actual interface. UART_SendByte(dataToSend); sendQueue.head = (sendQueue.head + 1) % BUFFER_SIZE; / / Loop to update the head pointer sendQueue.length--; return 0; / / Return 0 means the data is sent successfully } Interrupt and timing processing configuration: In the relevant configuration code of STM32F413, configure the timer interrupt, set the appropriate interrupt time interval (for example, use the relevant register configuration of the timer to trigger an interrupt every [specific time period]), and call the dequeue operation function in the interrupt service program. The sample interrupt service program code snippet is as follows: void TIMER_IRQHandler() { if (TIMER_GetITStatus(TIMER_INTERRUPT_FLAG) == SET) { dequeueData(); / / Call the dequeue operation function to send data TIMER_ClearITPendingBit(TIMER_INTERRUPT_FLAG); / / Clear interrupt flag } } System debugging and testing: Download the written code to the STM32F413 single-chip microcomputer development board, start the whole system, apply corresponding stimuli to the sensor module (such as simulating environmental changes to make the sensor generate data), observe the data interaction between STM32F413 and ESP32, monitor the data processing efficiency, data loss and other indicators of ESP32, compare the situation before using the buffer queue function of the present invention, verify whether the present invention effectively solves the problem of slow data processing of ESP32, and make necessary parameter adjustments and code optimization according to the test results to ensure that the system performance reaches the best state.
[0041] The embodiment of the present invention further provides a data sending device, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, wherein the processor implements the data storage and sending methods provided in the above embodiments when executing the computer program. For details, please refer to the method embodiment, which will not be repeated here.
[0042] Figure 3 The Internet of Things system provided by the embodiment of the present invention is as follows: Figure 3As shown, the Internet of Things system includes a data sending device and a data receiving device based on a communication interface link; wherein the data sending device adopts the data sending device provided in the above embodiment to implement the data storage and sending method provided in the above embodiments. In one embodiment, the data sending device can be an STM32F413 single-chip microcomputer and the data receiving device can be an ESP32 module for introduction. The Internet of Things system can also include other external devices connected to the STM32F413, such as IO input, IO output, LCD display, file storage, and temperature and humidity measurement modules.
[0043] In an embodiment of the present invention, the STM32F413 performs buffer management on the data generated by the external device through the sending buffer queue function and sends it to the ESP32 module. The ESP32 module performs subsequent network sending, protocol processing and other operations on the received data. The STM32F413 and ESP32 are connected through a suitable communication interface to realize data transmission.
[0044] After STM32F413 pre-processes and manages the buffer of data through the above-mentioned send buffer queue function, an efficient cooperation mechanism is established between STM32F413 and ESP32. After receiving data from STM32F413, ESP32 can perform subsequent network transmission, protocol processing and other operations in the order received based on its relatively stable and less urgent data processing rhythm. Since the data has been buffered and scheduled in order by STM32F413, ESP32 no longer needs to face the pressure of sudden large amounts of data or disordered high-frequency data requests, thereby effectively improving the overall data processing efficiency and solving the original problem of slow data processing.
[0045] The data storage and transmission method and the Internet of Things system provided by the embodiments of the present invention have the following beneficial effects: 1. Improve data processing efficiency: The data to be sent to ESP32 is managed in an orderly manner through the buffer queue, which prevents ESP32 from directly facing the influx of messy and high-frequency data, allowing it to process data at a more reasonable rhythm, thereby significantly improving the overall data processing speed and reducing the time delay for data waiting to be processed.
[0046] 2. Reduce the risk of data loss: The buffer queue can temporarily store data when ESP32 is temporarily busy and cannot receive data in time, preventing data from being lost due to failure to process in time, thus ensuring data integrity and system reliability.
[0047] 3. Enhanced system stability: The data interaction between STM32F413 and ESP32 is made smoother and more orderly, which reduces the data processing burden of ESP32, thereby improving the stability of the entire IoT system in the long-term operation process and reducing the occurrence of system failures or abnormal situations caused by problems such as data processing jams.
[0048] 4. Easy to implement and expand: The design of the present invention is mainly based on software-level function design and implementation on the single-chip microcomputer STM32F413. It does not require large-scale changes to the ESP32 hardware. It has low implementation cost and good scalability, and is easy to apply to various ESP32-based Internet of Things projects.
[0049] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0050] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A data storage and transmission method, characterized in that: The method comprises: Based on a tail pointer included in a buffer queue structure, the cached data is stored in a data buffer included in the buffer queue structure in the form of a buffer queue, and the tail pointer is updated based on the length of the cached data; Based on the head pointer included in the buffer queue structure, the data to be sent is obtained and sent out periodically, and the head pointer is updated based on the length of the data to be sent; Among them, the buffer queue structure includes the data buffer, the head pointer, the tail pointer and the queue length variable. The data buffer is used to store the data to be sent, the head pointer points to the starting position of the data to be sent in the buffer queue, the tail pointer points to the end position of the latest data stored in the buffer queue, and the queue length variable records the length of the data currently stored in the buffer queue.
2. The data storage and transmission method according to claim 1, characterized in that: Based on the tail pointer included in the buffer queue structure, the cached data is stored in the data buffer area included in the buffer queue structure in the form of a buffer queue, including: When it is determined that the queue length variable is less than the maximum queue length of the buffer queue, the cached data is sequentially stored in the data buffer based on the tail pointer.
3. The data storage and transmission method according to claim 1, characterized in that: Based on the head pointer included in the buffer queue structure, the data to be sent is obtained and sent out periodically, including: When it is determined that there is data to be sent in the buffer queue, the data to be sent is obtained based on the head pointer included in the buffer queue structure and is sent out periodically.
4. The data storage and transmission method according to claim 1 or 3, characterized in that: The step of obtaining the data to be sent and periodically sending it out includes: Data transmission is triggered by timer interrupt.
5. The data storage and transmission method according to claim 4, characterized in that: The time interval of the timer interruption is configured according to the data processing capability and data sending frequency of the data receiving device.
6. The data storage and transmission method according to claim 1, characterized in that: The method further comprises: When the head pointer and the tail pointer reach the end of the data buffer, a circular process is performed to make them point to the beginning of the data buffer again, thereby realizing a circular queue function.
7. The data storage and transmission method according to claim 1, characterized in that: The method further comprises: When the data buffer is full, returning a prompt message; and / or When the data buffer is empty, a prompt message is returned.
8. A data sending device, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the computer program, the data storage and transmission method according to any one of claims 1 to 7 is implemented.
9. An Internet of Things system, characterized in that: It comprises a data sending device and a data receiving device linked based on a communication interface; wherein the data sending device adopts the data sending device as claimed in claim 8.
10. The Internet of Things system according to claim 9, characterized in that: The data sending device is a STM32F413 single chip microcomputer, and the data receiving device is an ESP32 module.
Citation Information
Patent Citations
Method and device for de-queuing data in queue manager
CN102437929A
Data asynchronization storage and access mechanism applied to industrial control system
CN103514261A
Annular buffer queue data receiving mechanism based on single-chip microcomputer
CN112235206A
Queue element read-write method and device, equipment and medium
CN115878325A
Data processing method and device, equipment, storage medium and vehicle
CN118740928A