A device power-off millisecond-level detection and seamless switching alarm device and method based on the Internet of Things

CN122533249APending Publication Date: 2026-08-07NANJING INST OF TECH
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
NANJING INST OF TECH
Filing Date
2026-04-29
Publication Date
2026-08-07

AI Technical Summary

Technical Problem

该机制存在固有的时间盲区——心跳间隔通常为数至数分钟,设备断电后服务器需等待至少一个心跳周期才能感知,无法实现即时报警

Benefits of technology

[0032](1)毫秒级检测:直接电压检测,响应时间≤5ms,优于传统心跳机制;

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122533249A_ABST
    Figure CN122533249A_ABST
Patent Text Reader

Abstract

The application discloses a device power-off millisecond-level detection and seamless switching alarm device and method based on Internet of Things. The device comprises a master control communication unit, a commercial power conversion unit, a voltage state detection unit, a backup power supply unit, a power management unit, a nonvolatile storage unit, a network distribution unit and a communication protocol unit. The method comprises system power-on initialization, reading saved network configuration information from the nonvolatile storage unit, sequentially attempting to connect the saved network configuration, starting the intelligent network distribution mode, receiving network information sent by a mobile terminal, establishing a persistent connection with a preset remote server, entering a main loop, and performing voltage state detection, communication processing and command analysis in parallel. The application can realize millisecond-level detection, the power management circuit based on an adjustable precision shunt regulator and a switching tube can switch rapidly without impact, and an analog-digital conversion double-channel detection scheme is provided to further improve the system robustness.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of power monitoring and Internet of Things (IoT) technology, and in particular to an IoT-based device and method for millisecond-level detection and seamless switching alarm of device power failure. Background Technology

[0002] In critical locations such as industrial automation, data centers, and unattended base stations, untimely power outages can lead to production disruptions, data loss, and even safety incidents if not detected promptly. Currently, a common mechanism for monitoring equipment online status is the "heartbeat" mechanism: the device periodically sends heartbeat messages to the server; if the server does not receive a message within a timeout period, it determines the device is offline. This mechanism has an inherent time blind spot—the heartbeat interval is typically several to several minutes; after a power outage, the server needs to wait at least one heartbeat cycle to detect it, failing to provide immediate alarms.

[0003] To address these issues, some solutions attempt to add a power failure detection circuit to the device. However, most solutions fail to consider the power supply of the detection circuit itself after a power outage, causing the detection circuit to lose power synchronously with the main power supply and thus failing to send alarm signals. Other solutions introduce backup batteries, but these generally suffer from drawbacks such as complex circuitry, slow switching speed, inadequate charging management, and high costs. Furthermore, they fail to deeply integrate with modern IoT communication technologies, thus hindering the achievement of fast and reliable remote alarms.

[0004] Therefore, there is an urgent need in this field for a low-cost, highly integrated device that integrates rapid power failure detection, seamless power switching, intelligent network distribution, and real-time remote alarm functions to fill the time blind spot of the traditional heartbeat mechanism and improve the level of intelligent equipment operation and maintenance. Summary of the Invention

[0005] Purpose of the invention: The purpose of this invention is to provide an IoT-based device and method for millisecond-level power failure detection and seamless power switching alarm, so as to achieve millisecond-level power failure response, seamless power switching, convenient network configuration, and persistent remote alarm.

[0006] Technical solution: The IoT-based device power failure millisecond-level detection and seamless switching alarm device of the present invention is characterized by comprising:

[0007] The main control communication unit uses a microcontroller with integrated wireless local area network communication function to realize device control, wireless communication and status reporting;

[0008] The AC mains conversion unit is used to convert AC mains power into DC operating voltage;

[0009] The voltage status detection unit includes a first voltage divider resistor and a second voltage divider resistor connected in series. One end of the first voltage divider resistor is connected to the positive output of the mains power conversion unit, and one end of the second voltage divider resistor is grounded. The common connection point of the two is filtered and then connected to the general-purpose input / output pin of the main control communication unit.

[0010] The backup power unit uses a rechargeable battery to provide operating power to the device when the mains power fails;

[0011] A power management unit is connected between the mains power conversion unit, the backup power unit, and the system power supply terminal. The power management unit includes an adjustable precision parallel regulator and a first switching transistor. The voltage of the backup power unit is sampled by voltage divider and input to the reference terminal of the adjustable precision parallel regulator. The on and off states of the adjustable precision parallel regulator are used to drive the control terminal of the first switching transistor to collaboratively construct a constant voltage charging circuit for the backup power unit when the mains power is normal. When the mains power is interrupted and the voltage of the corresponding node drops sharply, the system power supply is instantly switched to the backup power unit by turning on the first switching transistor.

[0012] A non-volatile storage unit is used to store at least one set of network configuration information that has been successfully connected;

[0013] The distribution network unit, integrated into the main control communication unit, supports receiving and saving new network configurations via the intelligent distribution network protocol;

[0014] The communication protocol unit, executed by the main control communication unit, is used to establish a persistent connection with the remote server and send corresponding alarm signals when a power outage or recovery event is detected.

[0015] Furthermore, the power management unit also includes an optocoupler isolation unit. The positive output of the mains power conversion unit is connected to the anode of the input side of the optocoupler through a current-limiting resistor. The cathode of the input side of the optocoupler is connected to the cathode of the adjustable precision parallel voltage regulator. The output side of the optocoupler is connected to the grounding point and the control terminal of the first switching transistor, respectively.

[0016] Furthermore, the first switching transistor is an N-channel enhancement-mode MOSFET; when the voltage of the backup power unit is lower than the set full-charge voltage, the adjustable precision parallel regulator is turned off.

[0017] Furthermore, the adjustable precision parallel voltage regulator is turned off, the optocoupler is turned off, and the control terminal of the first switch receives a high level and is turned on, forming a charging circuit; when the voltage of the backup power unit reaches or exceeds the full charging voltage, the adjustable precision parallel voltage regulator is turned on, driving the optocoupler to be turned on, pulling the control terminal of the first switch down to near zero potential, and the first switch is turned off, cutting off the charging circuit.

[0018] Furthermore, the microcontroller's internal non-volatile storage unit stores at least three sets of historical network configuration information; after the device is powered on, it attempts to connect to the stored network configurations in sequence, and if all connections fail, it automatically starts the intelligent network configuration mode. After a successful connection, the new configuration is saved to the non-volatile storage unit.

[0019] Furthermore, after the persistent connection is established, the communication protocol unit sends a heartbeat message at fixed intervals; when the voltage status detection unit detects that the pin level changes from high to low and is confirmed by software debouncing, it immediately constructs and sends a power failure alarm message containing the device identifier, event type and timestamp, and at the same time starts the retransmission mechanism to retransmit a preset number of times at machine cycle intervals; when the pin level is detected to change from low to high, a power recovery message is sent and retransmission is terminated.

[0020] Furthermore, the main control communication unit identifies the on / off state of the mains power by detecting changes in the level of this pin.

[0021] The IoT-based device power failure millisecond-level detection and seamless switching alarm method of the present invention includes the following steps:

[0022] S1: System power-on initialization, reads the saved network configuration information from the non-volatile storage unit;

[0023] S2: Try to connect to the saved network configurations one by one. If the connection is successful, proceed to S4; otherwise, execute S3.

[0024] S3: Activate intelligent network configuration mode, receive network information sent by mobile terminal, and save it to non-volatile storage unit after successful connection;

[0025] S4: Establish a persistent connection with a preset remote server;

[0026] S5: Enter the main loop and execute voltage status detection, communication processing, and command parsing in parallel;

[0027] S6: When the level of the general input / output pin changes from high to low and is confirmed by debouncing, a power failure alarm signal is immediately sent through a persistent connection and repeated at a preset interval until the preset number of times is reached.

[0028] S7: When the level of the pin is detected to change from low to high, immediately send a power recovery signal and terminate the retransmission of the power failure alarm signal.

[0029] Furthermore, the anti-jitter confirmation in step S6 adopts a combination of edge-triggered interrupt and timer: after the level change triggers the interrupt, a preset duration timer is started, and after the expiration, the status is sampled again to confirm that the status is consistent before it is determined to be a valid event.

[0030] Furthermore, after each valid power outage event is determined, the current time information is written into the non-volatile storage unit and reported to the server when the network is restored or the next power-on occurs.

[0031] Beneficial effects: Compared with the prior art, the present invention has the following significant advantages:

[0032] (1) Millisecond-level detection: direct voltage detection with a response time of ≤5ms, which is superior to the traditional heartbeat mechanism;

[0033] (2) Seamless power switching: Based on the power management circuit of adjustable precision parallel regulator and switching transistor, the switching is fast and shock-free;

[0034] (3) Intelligent network distribution and automatic reconnection: Supports intelligent network distribution protocol and can store multiple historical network configurations;

[0035] (4) High reliability alarm: persistent connection + repeated alarm signal transmission to overcome network interruption;

[0036] (5) Low cost and high integration: With a microcontroller with integrated wireless communication function as the core, there are very few peripheral components, making it easy to mass-produce;

[0037] (6) Dual-channel enhancement: Provides a dual-channel detection scheme for analog-to-digital conversion to further improve the robustness of the system. Attached Figure Description

[0038] Figure 1 This is a system composition block diagram of the present invention;

[0039] Figure 2 This is a circuit schematic diagram of Embodiment 1 of the present invention;

[0040] Figure 3 This is the PCB layout of Embodiment 1 of the present invention;

[0041] Figure 4 This is a flowchart illustrating the Wi-Fi connection and network configuration process of the present invention.

[0042] Figure 5 This is the flowchart of the main program of the system of the present invention. Detailed Implementation

[0043] The technical solution of the present invention will be further described below with reference to the accompanying drawings.

[0044] The IoT-based device for millisecond-level power failure detection and seamless switching alarm as described in this invention includes:

[0045] The main control communication unit uses a microcontroller with integrated wireless local area network communication function to realize device control, wireless communication and status reporting;

[0046] The AC mains conversion unit is used to convert AC mains power into DC operating voltage;

[0047] The voltage status detection unit includes a first voltage divider resistor and a second voltage divider resistor connected in series. One end of the first voltage divider resistor is connected to the positive output of the mains power conversion unit, and one end of the second voltage divider resistor is grounded. The common connection point of the two is filtered and then connected to the general-purpose input / output pin of the main control communication unit.

[0048] The backup power unit uses a rechargeable battery to provide operating power to the device when the mains power fails;

[0049] A power management unit is connected between the mains power conversion unit, the backup power unit, and the system power supply terminal. The power management unit includes an adjustable precision parallel regulator and a first switching transistor. The voltage of the backup power unit is sampled by voltage divider and input to the reference terminal of the adjustable precision parallel regulator. The on and off states of the adjustable precision parallel regulator are used to drive the control terminal of the first switching transistor to collaboratively construct a constant voltage charging circuit for the backup power unit when the mains power is normal. When the mains power is interrupted and the voltage of the corresponding node drops sharply, the system power supply is instantly switched to the backup power unit by turning on the first switching transistor.

[0050] A non-volatile storage unit is used to store at least one set of network configuration information that has been successfully connected;

[0051] The distribution network unit, integrated into the main control communication unit, supports receiving and saving new network configurations via the intelligent distribution network protocol;

[0052] The communication protocol unit, executed by the main control communication unit, is used to establish a persistent connection with the remote server and send corresponding alarm signals when a power outage or recovery event is detected.

[0053] Example 1:

[0054] This example provides a typical implementation scheme for a device that provides rapid power failure detection and alarm based on ESP-12F and TL431. For example... Figure 1The diagram shows the system composition, including a main control communication unit (a microcontroller with integrated wireless communication functions, such as ESP-12F U1), an AC-DC conversion unit, a voltage status detection unit (voltage divider resistors R17 / R18 and filter capacitor C10), a backup power supply unit (rechargeable lithium battery BT1), a power management unit (adjustable precision parallel regulator, switching transistors, and optocouplers), a non-volatile memory unit (internal EEPROM), a network distribution unit (SmartConfig), and a communication protocol unit (WebSocket). The main control communication unit receives the voltage status detection unit's level signal via GPIO13 and interacts with the power management unit via control pins. When the mains power is normal, the power management unit supplies power to the system and charges the battery. When the mains power fails, the power management unit switches to battery power within milliseconds, and the main control communication unit sends an alarm signal to a remote server via WebSocket.

[0055] 1. Hardware Configuration

[0056] like Figure 2 The diagram shows the detailed electrical connections for components such as U1 (ESP-12F module), U4 / U5 (RT9013-33 step-down chip), R17 / R18 voltage divider circuit, U2 (TL431 adjustable precision parallel regulator), Q2 (IRFZ44N MOSFET), Q1 (MMBT4401 transistor), U3 (LTV-217 optocoupler), BT1 lithium battery, and LED indicator lights. All component reference numbers and parameters are labeled, corresponding one-to-one with the component list in Table 1. Figure 3 The diagram shows the physical layout of the components on the PCB board, including the positions and routing of major chips, resistors and capacitors, connectors, screw holes, and more. This reflects the structural design of the actual product. The circuit in this embodiment includes a main control communication module, a mains power conversion module, a voltage detection circuit, a backup power supply, a power management circuit, and indicator light circuits. The component reference numbers, models / parameters, packages, and quantities are detailed in Table 1. Figure 2 Circuit principle Figure 1 One-to-one correspondence. The main circuit modules are described below:

[0057] Main control communication module: adopts ESP-12F integrated chip U1, supports Wi-Fi 4 protocol, processor main frequency 80MHz, with onboard antenna, its 3.3V power supply pin is connected to the system power supply DC_3.3V, and GPIO13 is configured as voltage detection input.

[0058] AC-DC power conversion module (model 220V to 5V_700mA_Youxin Electronics), input L, N connect to 220V AC, output +5V; DC-DC step-down chips U4 and U5 (model RT9013-33GB-MS), U4 input +4.7V, output +3.3V, provides power to U1 in the power-off state, U5 input +5V, output +3.3V, provides power to U1 in the normal state.

[0059] Voltage detection circuit: A voltage divider is formed by resistors R17 (6.8kΩ) and R18 (10kΩ) connected in series. The upper end of R17 is connected to +5V, and the lower end of R18 is grounded. The midpoint of the voltage divider is connected to GPIO13 of U1. Under normal power supply, the midpoint voltage of the voltage divider is 3.0V, and U1 reads a high level; when the power is off, the voltage drops to 0V, and it reads a low level.

[0060] Backup power supply: 3.7V / 250mAh polymer lithium battery BT1, connected via connector CN1, with built-in protection board.

[0061] Power Management Circuit: A lithium battery charging management and status indication circuit is built using discrete components. The core is a voltage comparator based on a TL431 precision adjustable parallel voltage regulator. Optical isolation feedback controls the switching of the MOSFET to achieve constant voltage charging, and two LEDs indicate the charging status. The circuit is powered by DC +5V. R7 (6.8kΩ) and R8 (1kΩ) form a voltage divider sampling network to divide the voltage of the lithium battery BT1 (connected between the 5V positive terminal and the anode of the TL431) and send it to the reference terminal (R terminal) of the TL431. C1 (10nF) is used to filter out high-frequency noise in the sampled voltage. R6 (10kΩ) provides bias for the TL431. When the battery voltage is lower than the full charge threshold (4.2V), the voltage divider point is lower than 2.5V, TL431 is cut off, its cathode (K) is in a high-resistivity state, no current flows through the LED side of optocoupler U3 (LTV-217-BG), and the output side of U3 is cut off; at this time, the gate of MOSFET Q2 obtains a high level (N-channel enhancement type, high level conduction) through voltage division of R2 (1k) and R5 (50k), the MOSFET conducts, providing a charging circuit for the battery, and at the same time the source voltage of the MOSFET increases, which turns on transistor Q1 through R3 (2.2k), lighting up LED2 (current limited by R1), indicating the "charging" state, while LED1 is turned off because its gate is at a high level. When the battery voltage rises to the full charge threshold, the voltage divider point reaches 2.5V, TL431 conducts, and current flows through R9 (330Ω) to the LED of U3, turning on the output side of U3. This pulls the gate of the MOSFET low to ground, turning off the MOSFET and cutting off the charging circuit. At this time, the base of Q1 loses bias and turns off, LED2 goes out, and LED1 lights up through R1 and the conducting output side of U3, indicating a "fully charged" state. R4 is connected between the source of the MOSFET and ground, serving both as a pull-down resistor to ensure a stable source potential when the MOSFET is off and as a current detector. The voltage divider points of R2 and R5 are connected to both LED1 and the MOSFET gate, working with an optocoupler to allow the gate voltage to switch clearly between high and low levels, preventing the MOSFET from operating in the linear region. The entire circuit controls the optocoupler through the on / off state of TL431, thereby controlling the switching of the MOSFET and achieving automatic constant voltage charging and dual-color status indication of the lithium battery.

[0062] Indicator circuit: LED_GREEN is connected in series with R1 and the COL terminal of U3. When the BT1 voltage exceeds 4.2V, the output side of U3 conducts to form a loop, and the indicator light illuminates, indicating that the battery is fully charged; LED_RED is connected in series with R1 and the C terminal of Q1. When the BT1 voltage is lower than 4.2V, Q1 conducts, and the E terminal of Q1 is grounded, forming a loop, and the indicator light illuminates, indicating that it is charging; The onboard LED of U1 is controlled by GPIO2, providing different indications when connected to WiFi.

[0063] Other auxiliary components: Filter capacitor C10 (10pF) is connected in parallel with R18; C2~C9 are power supply decoupling capacitors, distributed near each chip; R10~R13 are power-on configuration resistors for U1; R16 is a buffer resistor between analog ground and digital ground; D1~D3 are Schottky diodes to prevent power surges.

[0064] System module connection instructions:

[0065] From a system connection perspective, the AC mains power is input to the AC-DC step-down unit via fuse F1 and varistor R15, outputting a DC 5V main power supply. This 5V power supply is divided into three paths: the first path is fed into the DC-DC step-down chip U4 via input filter capacitor C4, and the output of U4 is filtered by output filter capacitors C5 and C6 to generate DC 3.3V, providing the normal operating voltage for the main control chip U1; the second path is connected to the voltage detection circuit, and after being divided by series voltage divider resistors R17 (6.8kΩ) and R18 (10kΩ), the midpoint of the voltage divider is connected to the GPIO13 pin of U1 via filter capacitor C10. U1 determines the on / off status of the main power supply in real time by detecting the high or low level of this pin; the third path is connected to the backup battery module as the energy source for charging and status indication.

[0066] The current flow of the backup battery module is as follows: the DC 5V positive terminal is directly connected to the positive terminal of the lithium battery BT1, the negative terminal of BT1 is connected to the drain of the N-channel MOSFET Q2, the source of Q2 is grounded through the sampling resistor R4, and the gate of Q2 is controlled by a control network consisting of TL431 U2, optocoupler U3, and resistors R2 and R5. When the voltage of BT1 is lower than the full charge threshold (4.2V), U2 and U3 are cut off, and the gate of Q2 is turned on by obtaining a high level through the voltage divider of R2 and R5, forming a charging circuit: 5V → BT1 positive terminal → BT1 internal → BT1 negative terminal → Q2 drain → Q2 source → R4 → GND. At the same time, the source potential of Q2 rises, driving the transistor Q1 to conduct through R3, lighting up LED_RED to indicate that charging is in progress. When the BT1 voltage reaches the full charge threshold, U2 turns on, driving U3 to turn on, pulling the gate of Q2 low to ground, Q2 turns off, and cutting off the charging circuit; at the same time, the output side of U3 provides a current path for LED_GREEN: 5V→R1→LED_GREEN→U3 output side→GND, LED_GREEN lights up, indicating that it is fully charged.

[0067] Regarding the control signal flow: U1's GPIO13 serves as a voltage detection input. When the main power supply fails, the level of this pin changes from high to low, triggering an internal interrupt in U1. U1 sends a power failure alarm signal to a remote server via WebSocket and retransmits it through machine cycles (preferably 10 times). Simultaneously, after the main power supply fails, the system automatically switches to the backup battery: the input of U5 (RT9013-33) switches from U4's 4.7V to BT1's 3.7V, and U5 outputs a stable 3.3V to maintain U1's operation. Pins 15 and 16 of U1 are connected to the RXD and TXD pins of the programming module, respectively, for firmware programming and debugging.

[0068] Table 1: List of Main Components in Example 1

[0069] 1 AC-DC module AC-DC5V700mA_Uxin Electronics AC-DC5V_1 (Custom) 1 2 BT1 Polymer lithium battery_502030 —— 1 3 C1 10nF C0603 1 4 C4 1uF C0603 1 5 C7 1uF C0603 1 6 C10 10pF C0603 1 7 C2 100nF C0603 1 8 C6 100nF C0603 1 9 C9 100nF C0603 1 10 C11 100nF C0603 1 11 C3 10uF C0603 1 12 C5 10uF C0603 1 13 C8 10uF C0603 1 14 C12 10uF C0603 1 15 CN1 VH3.96-2P ZZ CONN-TH_2P-P3.96_C9900035759 1 16 D1 1N4148W SOD-123F_L2.7-W1.6-LS3.8-RD 1 17 D2 1N4148W SOD-123F_L2.7-W1.6-LS3.8-RD 1 18 D3 1N4148W SOD-123F_L2.7-W1.6-LS3.8-RD 1 19 F1 JFC2410-1200TS F2410 1 20 H2 PZ254V-11-04P HDR-TH_4P-P2.54-VM 1 21 H1 PZ254V-11-02P HDR-TH_2P-P2.54-VM 1 22 H3 PZ254V-11-02P HDR-TH_2P-P2.54-VM 1 23 LED_GREEN KT-0805G LED0805-R-RD 1 24 LED_RED KT-0805G LED0805-R-RD 1 25 Q1 MMBT4401LT1G SOT-23-3_L2.9-W1.3-P1.90-LS2.4-BR 1 26 Q2 IRFZ44NPBF TO-220-3_L10.0-W4.5-P2.54-L 1 27 R3 2.2K R0603 1 28 R4 0.5R R0603 1 29 R5 50K R0603 1 30 R1 1K R0603 1 31 R2 1K R0603 1 32 R8 1K R0603 1 33 R9 330R R0603 1 34 R15 14D471K RES-TH_L16.5-W5.9-P7.50-D0.8-S3.00 1 35 R14 0R R0603 1 36 R16 0R R0603 1 37 R7 6.8K R0603 1 38 R17 6.8K R0603 1 39 R6 10K R0603 1 40 R10 10K R0603 1 41 R11 10K R0603 1 42 R12 10K R0603 1 43 R13 10K R0603 1 44 R18 10K R0603 1 45 R19 10kΩ R0603 1 46 R20 1.5kΩ R0603 1 47 SCREW1 M3 screws M3 screws 1 48 SCREW2 M3 screws M3 screws 1 49 SCREW3 M3 screws M3 screws 1 50 SCREW4 M3 screws M3 screws 1 51 U1 ESP-12F (2.4GHz WiFi module) WIFIM-SMD_ESP-12F-ESP8266MOD 1 52 U2 TL431 SOT-23-3_L2.9-W1.4-P1.90-LS2.6-BR 1 53 U3 LTV-217-BG SOP-4_L4.4-W2.8-P1.27-LS7.0-BR 1 54 U4 RT9013-33GB-MS SOT-23-5_L2.9-W1.6-P0.95-LS2.8-BR 1 55 U5 RT9013-33GB-MS SOT-23-5_L2.9-W1.6-P0.95-LS2.8-BR 1

[0070] In specific embodiments of the present invention, the main control communication unit is illustrated using the ESP-12F module as an example, the adjustable precision parallel regulator is illustrated using the TL431 as an example, the switching transistor is illustrated using the IRFZ44N as an example, and the optocoupler isolation unit is illustrated using the LTV-217 as an example. However, those skilled in the art should understand that any integrated wireless communication microcontroller, adjustable precision parallel regulator, switching transistor, and optocoupler device with the same function can be equivalently replaced without departing from the protection scope of the present invention.

[0071] 2. Software Design

[0072] like Figure 4 The device shown reads the saved Wi-Fi configurations (up to three sets) from the EEPROM after power-on and attempts to connect to them sequentially. If all connections fail, it initiates SmartConfig network configuration mode and waits for the mobile phone to send network configuration information. The complete logic for saving the new configuration to the EEPROM upon successful connection conforms to the GB / T 1526-1989 standard. Figure 5 The complete process shown includes power-on initialization (GPIO13 interrupt, 10ms timer, EEPROM, Wi-Fi stack), configuration loading, execution of the Wi-Fi connection sub-process, connection to the WebSocket server, main loop event detection (power status monitoring, WebSocket communication, SmartConfig processing, serial port command processing), power-off / recovery interrupt handling, and alarm signal retransmission (preferably 10 retransmissions). This process conforms to the GB / T 1526-1989 standard. The software flow includes:

[0073] Power-on initialization: U1 executes internal firmware, initializes the serial port, configures GPIO13 as input mode and enables dual-edge interrupts, configures timers, EEPROM, and Wi-Fi stack, and prints the current WiFi saved list on the serial port.

[0074] Wi-Fi Connection: Reads the Wi-Fi configuration list (up to three groups) stored in the EEPROM. Attempts to connect sequentially. If a connection to a particular configuration is successful, the current connection state is saved, and the process jumps to a WebSocket connection. If all connections fail, SmartConfig network configuration is initiated, and the Wi-Fi SSID / password is sent via the mobile app. The device receives the SSID and attempts to connect. Upon successful connection, the SSID / password is stored in the EEPROM.

[0075] WebSocket connection: Initiate a WebSocket handshake based on the preset server address, and send a heartbeat packet every 10 seconds after a successful connection.

[0076] The main loop sequentially performs power status detection, WebSocket event handling, SmartConfig processing, serial port command processing, and periodic status serial port output; power status monitoring is interrupt-driven. A change in GPIO13 level triggers an interrupt, and a 10ms timer is started in the interrupt service routine. After the timer expires, the GPIO13 level is read to confirm the event is valid. If a power failure (high → low) is confirmed, an alarm sending function is immediately called; if a recovery (low → high) is confirmed, a recovery signal is sent and retransmission is stopped.

[0077] Power event transmission: Construct a JSON message. When the GPIO13 pin detects a change in level from high (normal power supply) to low (battery power supply) and the change is stable for more than the debouncing time, send the following via WebSocket: {"event": "power_loss","device": "ESP-12F","time": XXXX,"battery": true}. "event" is fixed to "power_loss" to indicate a power failure event, "device" is the device identifier (ESP-12F in this case), "time" represents the system uptime (milliseconds) at the time of the event, and "battery" is fixed to true to indicate that the power supply has been switched to the backup power supply. After successful transmission, a retransmission timer is started, and the message is retransmitted once per machine cycle until a recovery event is received or the timer ends. When the GPIO13 pin detects that the level changes from low (battery powered) to high (normal power powered) and remains stable for more than the debouncing time, the following message is sent via WebSocket: {"event": "power_restored","device": "ESP-12F","time": XXXX,"outage_duration_ms": XXXX}. "event" is fixed as "power_restored" to indicate a power recovery event, "device" is the device identifier, which is ESP-12F in this case, "time" represents the system uptime (milliseconds) when the event occurred, and "outage_duration_ms" represents the duration of the power outage (milliseconds).

[0078] 3. Test Results

[0079] In actual testing, in this embodiment, the average time from power failure to WebSocket message transmission is approximately 15.5ms (including 10ms of anti-jitter; this can be reduced to less than 5ms if anti-jitter is disabled), meaning the power switching time is less than 5ms. The power failure alarm signal is preferably retransmitted 10 times to ensure reliable server reception. The lithium battery can maintain continuous device operation for over 2.4 hours on a full charge. The Wi-Fi connection success rate exceeds 99% in open environments, and SmartConfig network configuration takes an average of 7 seconds.

[0080] Example 2

[0081] This example demonstrates a practical application of the device based on Embodiment 1. The mains power supply portion of this device can be directly connected to a socket via a wire to monitor the power supply status of that socket. Alternatively, the mains power supply portion of this device can be connected to the mains power supply portion of a heating device via a wire, allowing for testing of the device's reliability and usability by simulating a power outage on the heating device. By attaching this device to any device that draws power from the mains, it enables rapid detection and reporting of unexpected power outages, overcoming the limitation of requiring 24 / 7 monitoring by personnel for devices with real-time power supply monitoring needs.

[0082] In addition, this embodiment adds a power failure record storage area to the EEPROM, and writes a timestamp to the EEPROM each time a power failure occurs, so as to realize the power failure history tracing.

[0083] Actual test results:

[0084] Industrial applicability

[0085] All components of this invention are standard commercially available products, with a simple manufacturing process and low cost. It can be widely used in various applications requiring real-time monitoring of equipment power supply status. The device is compact and can be directly installed at the equipment's power inlet for immediate use. After small-batch trial production and field testing, the device has demonstrated stable performance, meeting industrial-grade reliability requirements and possessing significant potential for large-scale application.

Claims

1. A device based on the Internet of Things (IoT) for millisecond-level detection and seamless switching alarm during device power failure, characterized in that, include: The main control communication unit uses a microcontroller with integrated wireless local area network communication function to realize device control, wireless communication and status reporting; The AC mains conversion unit is used to convert AC mains power into DC operating voltage; The voltage status detection unit includes a first voltage divider resistor and a second voltage divider resistor connected in series. One end of the first voltage divider resistor is connected to the positive output of the mains power conversion unit, and one end of the second voltage divider resistor is grounded. The common connection point of the two is filtered and then connected to the general-purpose input / output pin of the main control communication unit. The backup power unit uses a rechargeable battery to provide operating power to the device when the mains power fails; A power management unit is connected between the mains power conversion unit, the backup power unit, and the system power supply terminal. The power management unit includes an adjustable precision parallel regulator and a first switching transistor. The voltage of the backup power unit is sampled by voltage divider and then input to the reference terminal of the adjustable precision parallel regulator. The on and off states of the adjustable precision parallel regulator are used to drive the control terminal of the first switching transistor to collaboratively construct a constant voltage charging circuit for the backup power unit when the mains power is normal. When the mains power is interrupted and the voltage of the corresponding node drops sharply, the system power supply is instantly switched to the backup power unit by turning on the first switching transistor. A non-volatile storage unit is used to store at least one set of network configuration information that has been successfully connected; The distribution network unit, integrated into the main control communication unit, supports receiving and saving new network configurations via the intelligent distribution network protocol; The communication protocol unit, executed by the main control communication unit, is used to establish a persistent connection with the remote server and send corresponding alarm signals when a power outage or recovery event is detected.

2. The IoT-based device power failure millisecond-level detection and seamless switching alarm device according to claim 1, characterized in that, The power management unit also includes an optocoupler isolation unit. The positive output of the mains power conversion unit is connected to the anode of the input side of the optocoupler isolation unit via a current-limiting resistor. The cathode of the input side of the optocoupler isolation unit is connected to the cathode of the adjustable precision parallel voltage regulator. The output side of the optocoupler isolation unit is connected to the grounding point and the control terminal of the first switching transistor, respectively.

3. The IoT-based device power failure millisecond-level detection and seamless switching alarm device according to claim 1, characterized in that, The first switching transistor is an N-channel enhancement-mode MOSFET; when the voltage of the backup power unit is lower than the set full charge voltage, the adjustable precision parallel regulator is turned off.

4. The IoT-based device power failure millisecond-level detection and seamless switching alarm device according to claim 1, characterized in that, The adjustable precision parallel voltage regulator is turned off, the optocoupler is turned off, and the control terminal of the first switch receives a high level and is turned on, forming a charging circuit. When the voltage of the backup power unit reaches or exceeds the full charging voltage, the adjustable precision parallel voltage regulator is turned on, driving the optocoupler to be turned on, pulling the control terminal of the first switch down to near zero potential, and the first switch is turned off, cutting off the charging circuit.

5. The IoT-based device power failure millisecond-level detection and seamless switching alarm device according to claim 1, characterized in that, The microcontroller's internal non-volatile memory unit stores at least three sets of historical network configuration information. After the device is powered on, it attempts to connect to the stored network configurations in sequence. If all connections fail, the intelligent network configuration mode is automatically started. If the connection is successful, the new configuration is saved to the non-volatile memory unit.

6. The IoT-based device power failure millisecond-level detection and seamless switching alarm device according to claim 1, characterized in that, After the persistent connection is established, the communication protocol unit sends a heartbeat message at fixed intervals. When the voltage status detection unit detects that the pin level changes from high to low and confirms it through software debouncing, it immediately constructs and sends a power failure alarm message containing the device identifier, event type, and timestamp. At the same time, it starts a retransmission mechanism to retransmit a preset number of times at machine cycle intervals. When the pin level is detected to change from low to high, a power recovery message is sent and retransmission is terminated.

7. The IoT-based device power failure millisecond-level detection and seamless switching alarm device according to claim 1, characterized in that, The main control communication unit identifies the on / off state of the mains power by detecting changes in the level of this pin.

8. A method for millisecond-level detection and seamless switching alarm of device power failure based on the Internet of Things, applied to the device described in any one of claims 1 to 7, characterized in that, Includes the following steps: S1: System power-on initialization, reads the saved network configuration information from the non-volatile storage unit; S2: Try to connect to the saved network configurations one by one. If the connection is successful, proceed to S4; otherwise, execute S3. S3: Activate intelligent network configuration mode, receive network information sent by mobile terminal, and save it to non-volatile storage unit after successful connection; S4: Establish a persistent connection with a preset remote server; S5: Enter the main loop and execute voltage status detection, communication processing, and command parsing in parallel; S6: When the level of the general input / output pin changes from high to low and is confirmed by debouncing, a power failure alarm signal is immediately sent through a persistent connection and repeated at a preset interval until the preset number of times is reached. S7: When the level of the pin is detected to change from low to high, immediately send a power recovery signal and terminate the retransmission of the power failure alarm signal.

9. The method for millisecond-level detection and seamless switching alarm of device power failure based on the Internet of Things as described in claim 8, characterized in that, The debouncing confirmation in step S6 adopts a combination of edge-triggered interrupt and timer: after the level change triggers the interrupt, a preset duration timer is started. After the timer expires, the status is sampled again to confirm that the status is consistent before it is determined to be a valid event.

10. The method for millisecond-level detection and seamless switching alarm of device power failure based on the Internet of Things according to claim 8, characterized in that, After each valid power outage event is determined, the current time information is written to the non-volatile storage unit and reported to the server when the network is restored or the next power is restored.