You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino WebClient与MotionDetector运行一段时间后停止故障求助

Troubleshooting Your Arduino Lock-Up Issue

Hey Daniel, let's figure out why your Arduino setup stops working after running for a while—this is a common pain point with network-connected projects, so let's break down the likely culprits and fixes:

Key Issues in Your Code

1. Recursive loop() Call (Critical!)

In your setup() function, you're manually calling loop(); at the end. The Arduino framework automatically runs loop() in an infinite cycle after setup() finishes. Manually calling it creates a recursive loop that will eventually overflow the stack, causing the Arduino to crash. This is almost certainly one of the main reasons your code stops working.

Fix: Remove the loop(); line from setup().

2. Unhandled Network Buffer Overflow

Your getMovement() and getNoMovement() functions send a GET request but never read the server's response. The EthernetClient has a limited buffer, and if it fills up with unread data, subsequent network operations will fail or hang the device.

Fix: Modify both functions to read the server response and properly clean up connections:

//getMovement - sends a GET request when motion is detected
void getMovement() {
  // Clean up any existing connection first
  if (client.connected()) {
    client.stop();
  }

  if (client.connect(server, 8080)) {
    // Use proper HTTP syntax (add Host header and Connection: close)
    client.println("GET /GetARoomMaven/motion?roomId=1&movement=1 HTTP/1.1");
    client.println("Host: 192.168.50.93");
    client.println("Connection: close");
    client.println();
    Serial.println("Movement Request Sent");

    // Read server response to empty the buffer
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // Optional: Uncomment to debug response
        // Serial.print(c);
      }
    }
    // Give time for connection to close
    delay(100);
    client.stop();
  } else {
    Serial.println("Movement connection failed");
    client.stop(); // Ensure any partial connection is closed
  }
}

//getNoMovement - sends a GET request when motion had stopped
void getNoMovement() {
  if (client.connected()) {
    client.stop();
  }

  if (client.connect(server, 8080)) {
    client.println("GET /GetARoomMaven/motion?roomId=1&movement=0 HTTP/1.1");
    client.println("Host: 192.168.50.93");
    client.println("Connection: close");
    client.println();
    Serial.println("No Movement Request Sent");

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
      }
    }
    delay(100);
    client.stop();
  } else {
    Serial.println("No Movement connection failed");
    client.stop();
  }
}

Note: I replaced & with & in the URL—HTML entities aren't needed in HTTP requests.

3. Missing Network Configuration Stability

Your Ethernet.begin() call only uses the MAC address. Adding your static IP and DNS will make the network connection more reliable, especially after long runs.

Fix: Update the Ethernet initialization in setup():

Ethernet.begin(mac, ip, myDns);

4. No Watchdog Timer for Recovery

Even with fixes, occasional glitches (like network timeouts or sensor interference) can hang the Arduino. A watchdog timer will automatically restart the device if it stops responding.

Fix: Add watchdog support:

  1. Include the library at the top:
#include <avr/wdt.h>
  1. Initialize it in setup():
wdt_enable(WDTO_8S); // Restarts Arduino if no "feed" for 8 seconds
  1. "Feed" the watchdog in loop() to keep it running:
void loop(){
  wdt_reset(); // Reset watchdog timer at start of loop
  // ... rest of your loop code
}

Additional Hardware Checks

  • Power Supply: Ethernet modules draw significant current. Ensure your Arduino is powered by a stable 5V/2A supply (not just USB from a computer) to avoid brownouts that cause crashes.
  • PIR Sensor Wiring: Double-check the sensor's power and ground connections—loose wires can cause intermittent issues that lead to lock-ups.

Try these fixes one by one (start with removing the recursive loop() call, as that's critical) and see if your setup runs reliably long-term!

内容的提问来源于stack exchange,提问作者Daniel H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:53:08