A method for gateway 4G connection keep-alive in data collection
By separating the dialer daemon process and the 4G connection detection thread during data acquisition, and automatically restoring the 4G connection after detecting anomalies, the problem of unstable 4G network connection was solved, and more stable data transmission was achieved.
Patent Information
- Application Number
- CN202311126086.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-04
- Publication Date
- 2026-11-20
- Estimated Expiration
- 2043-09-04
AI Technical Summary
During data collection, 4G network connections are prone to interruption due to signal fluctuations. Existing technologies using ping domain names or IP detection methods frequently misjudge, leading to unstable connection recovery.
The dialer daemon and the 4G connection detection thread are separated. The connection anomaly is detected by pinging the domain name or IP of the MQTT service, and the gateway is restarted after 5 consecutive failures. The 4G connection is automatically restored in conjunction with the dialer daemon.
It improves the stability and recovery efficiency of 4G connections, avoids connection interruptions caused by signal fluctuations and misjudgments, and ensures the continuity of data collection.
Smart Images

Figure CN117222053B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of data acquisition, and in particular to the technical field of gateway 4G connection keep-alive. Background Technology
[0002] In data acquisition, the network connection methods for uploading the collected device data to the IoT platform mainly include wired and wireless (WIFI, 4G, 5G), among which using 4G network to upload the collected data is a relatively common method.
[0003] Compared to wired connections, 4G networks, while more flexible as they are not limited by network cables, are prone to connection interruptions due to signal fluctuations, leading to data upload failures. To avoid this, a common method is to ping a fixed domain name or IP address repeatedly, resetting the 4G module or restarting the dial-up program if the ping fails. However, this still presents the problem of misjudgments due to changes in domain names or IP addresses, as well as 4G signal fluctuations.
[0004] The present invention aims to provide a method for restoring 4G network connectivity as much as possible and for relatively accurately determining 4G network connection interruption in data acquisition scenarios. Summary of the Invention
[0005] The purpose of this invention is to solve the problems in the prior art and propose a method for keeping the gateway 4G connection alive during data acquisition. This method can separate the dialing program daemon process and the 4G connection detection thread of the acquisition program, making the program's responsibilities more singular and clear. The daemon process is only responsible for ensuring the startup of the dialing program, while the 4G connection detection thread is responsible for detecting 4G connection anomalies and restoring the connection after anomalies are detected.
[0006] To achieve the above objectives, this invention proposes a method for keeping a gateway 4G connection alive during data acquisition, comprising the following steps; Step 1: When the gateway starts, the data collection program is launched, and the data collection program launches the 4G connection detection thread; Step 2: The data acquisition program initializes the 4G connection detection count to 0 and sets the gateway restart flag to false; Step 3: Increment the 4G connection detection count by 1; Step 4: Obtain the domain name for the MQTT service used for data uploading; Step 5: Check if the data can be obtained in Step 4; S50: If the domain name of the MQTT service used for data transmission can be obtained, then after obtaining the domain name or IP of the MQTT service by ping, proceed to step 6. S51: If the domain name of the MQTT service used for data transmission cannot be obtained, after obtaining the IP address of the MQTT service, use ping to obtain the domain name or IP address of the MQTT service, then proceed to step 6. Step 6: Check if the ping can be completed within 60 seconds; S61: If a ping is detected within 60 seconds, pause for 60 seconds, set the detection count to 0, set the gateway restart flag to false, and then proceed to the next detection, proceeding to step 3; S62: If a ping fails within 60 seconds, increment the 4G connection detection count by 1, and then proceed to step 7. Step 7: Check if the number of tests is greater than or equal to 5; S71: If the number of detections is greater than or equal to 5, proceed to step 8; S72: If the number of tests is not greater than or equal to 5, pause for 60 seconds, then proceed to the next test and proceed to step 3; Step 8: Check if the restart flag is true; S81: If the restart flag is true, proceed directly to step 9; S82: If the restart flag is not true, first set the 4G connection detection count to 0, set the restart flag to true, then send an AT command to the serial port of the 4G module and pause for 60 seconds, then close the 4G dialing program and pause for 240 seconds, wait for the 4G dialing program daemon to automatically start the 4G dialing program, then perform the next detection and proceed to step 3. Step 9: Restart the entire gateway.
[0007] Preferably, in Step 6, the 60 seconds can be adjusted according to the actual 4G network conditions.
[0008] Preferably, in step 61, the 60-second pause time is adjustable. The 60-second pause takes into account the fluctuation of 4G signal, avoiding frequent detection when the signal is poor and frequent detection when pinging can be completed quickly.
[0009] Preferably, in step 7, the number of tests is adjustable to 5, and multiple tests are used to avoid misjudgments caused by 4G signal fluctuations.
[0010] Preferably, in step Step8, true indicates that after taking recovery measures, the next detection cycle will begin.
[0011] Preferably, in step S82, after sending the AT command to the serial port of the 4G module, the 4G module is restarted to resolve issues such as 4G module malfunctions and failure to recognize the SIM card. Then, a 60-second pause is made to wait for the 4G module to restart completely. The 60-second time is adjustable. Then, the dialing program is closed to prevent the dialing program from malfunctioning during the restart of the 4G module.
[0012] Preferably, the AT command is sent as AT+CFUN=1,1\r\n.
[0013] Preferably, in S82, the 240-second time is adjustable, and the 240-second pause is used to wait for the dialer daemon to start the dialer and for the dialer to complete the dialing.
[0014] Preferably, in S82, the 4G dialing program daemon process includes the following steps; S821: When the gateway starts, the dialer daemon process is started; S822: The daemon process obtains the PID of the dialer process; S823: Detect whether the PID can be obtained; S8231: If the PID can be obtained, pause for 60 seconds and then proceed to the next detection, and then proceed to step S822; S8232: If the PID cannot be obtained, the dialing program is started asynchronously, paused for 60 seconds, and then proceeded to the next detection, and then to step S822.
[0015] The beneficial effects of this invention are as follows: The separation of the dial-up program daemon and the 4G connection detection thread in the data collection program makes the program's responsibilities more singular and clear. The daemon is only responsible for ensuring the dial-up program starts; the 4G connection detection thread is responsible for detecting 4G connection anomalies and recovering from them. The 4G connection is confirmed by pinging the domain name or IP address of the MQTT service used for data transmission, rather than a fixed domain name and IP address. This allows the method to automatically adapt to gateways deployed in different environments (different data transmission platforms, different gateway geographical locations). Conversely, taking pinging (www.baidu.com) as an example, the domain name resolution will vary depending on the geographical location. The output IPs may differ, possibly due to service interruptions or IP changes, causing ping failures. After restarting the 4G module, close the dialer and allow the dialer daemon to automatically restart it to restore the 4G connection. This minimizes the risk of dialer malfunctions caused by the 4G module restart. If, after restarting the 4G module, pinging fails after five consecutive attempts, restart the gateway. This provides an opportunity for the 4G connection to recover without restarting, preventing data collection interruptions. Furthermore, if the 4G connection cannot be restored, restarting the gateway eliminates the impact of high CPU and memory usage on the 4G connection, making a 4G connection possible.
[0016] The features and advantages of the present invention will be described in detail through embodiments and in conjunction with the accompanying drawings. Attached Figure Description
[0017] Figure 1 This invention relates to a method for keeping a gateway 4G connection alive during data acquisition, specifically the 4G connection detection process of the acquisition program. Figure 2 This invention relates to a method for keeping a gateway 4G connection alive during data acquisition, specifically the dialer daemon process. Detailed Implementation
[0018] See Figure 1 , Figure 2 The present invention provides a method for keeping a gateway 4G connection alive during data acquisition, comprising the following steps; Step 1: When the gateway starts, the data collection program is launched, and the data collection program launches the 4G connection detection thread; Step 2: The data acquisition program initializes the 4G connection detection count to 0 and sets the gateway restart flag to false; Step 3: Increment the 4G connection detection count by 1; Step 4: Obtain the domain name for the MQTT service used for data uploading; Step 5: Check if the data can be obtained in Step 4; S50: If the domain name of the MQTT service used for data transmission can be obtained, then after obtaining the domain name or IP of the MQTT service by ping, proceed to step 6. S51: If the domain name of the MQTT service used for data transmission cannot be obtained, after obtaining the IP address of the MQTT service, use ping to obtain the domain name or IP address of the MQTT service, then proceed to step 6. Step 6: Check if the ping can be completed within 60 seconds; S61: If a ping is detected within 60 seconds, pause for 60 seconds, set the detection count to 0, set the gateway restart flag to false, and then proceed to the next detection, proceeding to step 3; S62: If a ping fails within 60 seconds, increment the 4G connection detection count by 1, and then proceed to step 7. Step 7: Check if the number of tests is greater than or equal to 5; S71: If the number of detections is greater than or equal to 5, proceed to step 8; S72: If the number of tests is not greater than or equal to 5, pause for 60 seconds, then proceed to the next test and proceed to step 3; Step 8: Check if the restart flag is true; S81: If the restart flag is true, proceed directly to step 9; S82: If the restart flag is not true, first set the 4G connection detection count to 0, set the restart flag to true, then send an AT command to the serial port of the 4G module and pause for 60 seconds, then close the 4G dialing program and pause for 240 seconds, wait for the 4G dialing program daemon to automatically start the 4G dialing program, then perform the next detection and proceed to step 3. Step 9: Restart the entire gateway.
[0019] In Step 6, the 60 seconds can be adjusted according to the actual 4G network conditions.
[0020] In Step 61, the 60-second pause time is adjustable. The 60-second pause takes into account the fluctuation of 4G signal, avoiding frequent testing when the signal is poor and frequent testing when pinging can be completed quickly.
[0021] In Step 7, the number of tests is adjustable to 5, and multiple tests are used to avoid misjudgments caused by 4G signal fluctuations.
[0022] In Step 8, true indicates that after taking recovery measures, the next detection cycle will begin.
[0023] In step S82, after sending the AT command to the serial port of the 4G module, the 4G module is restarted to resolve issues such as 4G module malfunctions and failure to recognize the SIM card. Then, a 60-second pause is performed to wait for the 4G module to restart completely. The 60-second time is adjustable. Then, the dialing program is closed to prevent the dialing program from malfunctioning during the 4G module restart.
[0024] The AT command mentioned above refers to sending AT+CFUN=1,1\r\n. In S82, the 240-second time is adjustable. The 240-second pause is used to wait for the dialer daemon to start the dialer and for the dialer to complete the dialing.
[0025] In S82, the 4G dialing program daemon process includes the following steps; S821: When the gateway starts, the dialer daemon process is started; S822: The daemon process obtains the PID of the dialer process; S823: Detect whether the PID can be obtained; S8231: If the PID can be obtained, pause for 60 seconds and then proceed to the next detection, and then proceed to step S822; S8232: If the PID cannot be obtained, the dialing program is started asynchronously, paused for 60 seconds, and then proceeded to the next detection, and then to step S822.
[0026] The working process of this invention: This invention discloses a method for keeping a gateway 4G connection alive during data acquisition. The process for the data acquisition program to detect 4G connectivity is as follows: 1. Initialize 4G connection detection count to 0, and set restart flag to false; 2. Obtain the domain name of the MQTT service used for data uploading; if the domain name cannot be obtained, obtain the IP address. 3. Ping the domain name or IP address of the MQTT service. The timeout is 60 seconds, which can be adjusted according to the actual 4G network conditions. 4. If a ping is successful, set the 4G connection detection count to 0 and the restart flag to false, indicating that the 4G connection has been restored; if a ping is unsuccessful, increment the 4G connection detection count by 1 and then pause for 60 seconds. The 60-second time is adjustable to take into account the fluctuation of 4G signal and avoid frequent detection when the signal is poor, as well as frequent detection when a ping can be achieved quickly. 5. If the number of connection detections is greater than or equal to 5 (the number of detections is adjustable; multiple detections are used to avoid misjudgments caused by 4G signal fluctuations), and the restart flag is false, the 4G connection detection count is set to 0, and the restart flag is set to true (indicating that after taking recovery measures, the next detection cycle will begin). An AT command (AT+CFUN=1,1\r\n) is sent to the serial port of the 4G module to restart the 4G module and resolve issues such as 4G module abnormalities and failure to recognize the SIM card. Wait 60 seconds (the time is adjustable and is used to wait for the 4G module to restart completely), close the dialer program to prevent abnormalities during the 4G module restart, and wait 240 seconds (the time is adjustable and is used to wait for the dialer program daemon to start the dialer program and for the dialer program to complete the dialing process). If the restart flag is true, the gateway is restarted to avoid the impact of high CPU or memory usage on the gateway's 4G connection, ensuring that the 4G connection is restored as much as possible.
[0027] The dialer daemon process is protected as follows: Every 60 seconds, retrieve the PID of the dialer process. If the PID cannot be retrieved, start the dialer asynchronously and pause for 60 seconds to wait for the dialer to finish starting and to avoid the daemon process frequently checking and consuming too much CPU resources. The pause time can be adjusted according to actual needs.
[0028] This invention separates the dialer daemon process and the 4G connection detection thread of the data collection program, making the program's responsibilities more singular and clear. The daemon process is only responsible for ensuring the dialer program starts; the 4G connection detection thread is responsible for detecting 4G connection anomalies and recovering from them. The 4G connection is confirmed by pinging the domain name or IP address of the MQTT service used for data transmission, rather than a fixed domain name and IP address. This allows the method to automatically adapt to gateways deployed in different environments (different data transmission platforms, different deployment environments of data transmission platforms, different geographical locations of gateways). Conversely, taking pinging (www.baidu.com) as an example, the IP address resolved by the domain name will be different depending on the geographical location, and the IoT card in the SIM card will have IP restrictions. If you consistently ping the same IP address resolved by the domain name, the IP address may change and become invalid. The geographical location of the gateway may also affect the network response of that IP. After restarting the 4G module, close the dialer and use the dialer daemon to automatically start the dialer to restore the 4G connection. This can minimize the chance of the dialer malfunctioning due to the 4G module restart. If you still cannot ping after restarting the 4G module, restart the gateway. This operation gives the 4G connection a chance to recover without restarting, avoiding interruption of data collection. On the other hand, if the 4G connection cannot be restored, restarting the gateway can eliminate the impact of high CPU and memory usage on the 4G connection and restore the 4G connection as much as possible.
[0029] The above embodiments are illustrative of the present invention and are not intended to limit the present invention. Any simple modifications to the present invention are within the scope of protection of the present invention.
Claims
1. A method for keeping a gateway 4G connection alive during data acquisition, characterized in that: Includes the following steps; Step 1: When the gateway starts, the data collection program is launched, and the data collection program launches the 4G connection detection thread; Step 2: The data acquisition program initializes the 4G connection detection count to 0 and sets the gateway restart flag to false; Step 3: Increment the 4G connection detection count by 1; Step 4: Obtain the domain name for the MQTT service used for data uploading; Step 5: Check if the data can be obtained in Step 4; S50: If the domain name of the MQTT service used for data transmission can be obtained, then after obtaining the domain name or IP of the MQTT service by ping, proceed to step 6. S51: If the domain name of the MQTT service used for data transmission cannot be obtained, after obtaining the IP address of the MQTT service, use ping to obtain the domain name or IP address of the MQTT service, then proceed to step 6. Step 6: Check if the ping can be completed within 60 seconds; S61: If a ping is detected within 60 seconds, pause for 60 seconds, set the detection count to 0, set the gateway restart flag to false, and then proceed to the next detection, proceeding to step 3; S62: If a ping fails within 60 seconds, increment the 4G connection detection count by 1, and then proceed to step 7. Step 7: Check if the number of tests is greater than or equal to 5; S71: If the number of detections is greater than or equal to 5, proceed to step 8; S72: If the number of tests is not greater than or equal to 5, pause for 60 seconds, then proceed to the next test and proceed to step 3; Step 8: Check if the restart flag is true; S81: If the restart flag is true, proceed directly to step 9; S82: If the restart flag is not true, first set the 4G connection detection count to 0, set the restart flag to true, then send an AT command to the serial port of the 4G module and pause for 60 seconds, then close the 4G dialing program and pause for 240 seconds, wait for the 4G dialing program daemon to automatically start the 4G dialing program, then perform the next detection and proceed to step 3. Step 9: Restart the entire gateway.
2. The method for keeping a gateway 4G connection alive during data acquisition as described in claim 1, characterized in that: In Step 6, the 60 seconds can be adjusted according to the actual 4G network conditions.
3. A method for keeping a gateway 4G connection alive during data acquisition as described in claim 2, characterized in that: In Step 61, the 60-second pause time is adjustable. The 60-second pause takes into account the fluctuation of 4G signal, avoiding frequent detection when the signal is poor and frequent detection when pinging can be completed quickly.
4. A method for keeping a gateway 4G connection alive during data acquisition as described in claim 1, characterized in that: In Step 7, the number of tests is adjustable up to 5. Multiple tests are used to avoid misjudgments caused by 4G signal fluctuations.
5. A method for keeping a gateway 4G connection alive during data acquisition as described in claim 1, characterized in that: In Step 8, true indicates that after taking recovery measures, the next detection cycle will begin.
6. A method for keeping a gateway 4G connection alive during data acquisition as described in claim 1, characterized in that: In step S82, after sending the AT command to the serial port of the 4G module, the 4G module is restarted to resolve issues such as 4G module malfunction and inability to recognize the SIM card. Then, a 60-second pause is performed to wait for the 4G module to restart completely. The 60-second time is adjustable. Then, the dialing program is closed to prevent the dialing program from malfunctioning during the restart of the 4G module.
7. A method for keeping a gateway 4G connection alive during data acquisition as described in claim 1, characterized in that: The AT command is sent as AT+CFUN=1,1\r\n.
8. A method for keeping a gateway 4G connection alive during data acquisition as described in claim 1, characterized in that: In S82, the 240-second time is adjustable. The 240-second pause is used to wait for the dialer daemon to start the dialer and for the dialer to complete the dialing.
9. A method for keeping a gateway 4G connection alive during data acquisition as described in claim 1, characterized in that: In S82, the 4G dialing program daemon process includes the following steps; S821: When the gateway starts, the dialer daemon process is started; S822: The daemon process obtains the PID of the dialer process; S823: Detect whether the PID can be obtained; S8231: If the PID can be obtained, pause for 60 seconds and then proceed to the next detection, and then proceed to step S822; S8232: If the PID cannot be obtained, the dialing program is started asynchronously, paused for 60 seconds, and then proceeded to the next detection, and then to step S822.
Citation Information
Patent Citations
Online keep-alive method of wireless communication equipment
CN110290545A
Network detection method and device and related equipment
CN111884879A
