树莓派出现ConnectionRefusedError: [Errno 111]连接拒绝问题求助
Hey there, let's work through these issues you're facing with your Pi Camera setup on Raspberry Pi. You've got two distinct problems to address: a GPIO channel warning and the frustrating ConnectionRefusedError: [Errno 111] Connection refused when connecting to your IoT server. Let's break them down:
1. Fixing the GPIO Channel In-Use Warning
That runtime warning you're seeing (This channel is already in use, continuing anyway) means the GPIO pin assigned to your LED is either being used by another process, or your previous script didn't clean up the pin properly. Here's how to tackle it:
- Quickly suppress the warning: Add
GPIO.setwarnings(False)right before yourGPIO.setup(LED, GPIO.OUT)line. This hides the message if you're confident the pin is safe to reuse, though it doesn't fix the root cause. - Find conflicting processes: Run this terminal command on your Pi to check what's using the GPIO pin:
Kill any unexpected processes withps aux | grep gpiosudo kill <process-id>to free up the pin. - Add cleanup to your script: Include
GPIO.cleanup()in afinallyblock or on script exit to ensure pins are released properly every time your code stops running.
2. Resolving ConnectionRefusedError (IoT Server Connection)
Since your local computer can reach the IoT server via traceroute but your Pi can't, the issue is tied to network configuration or server access controls. Let's walk through key checks:
- Test specific port connectivity: Traceroute only checks basic network reachability (ICMP), not the specific TCP/UDP port your camera uses. Test if the Pi can connect to the server's target port with:
If this fails, the port is either blocked or the server isn't listening for your Pi's IP.telnet <your-server-ip> <server-port> # Or use netcat if telnet isn't installed nc -zv <your-server-ip> <server-port> - Check Pi's firewall settings: Your Raspberry Pi might have a firewall (like
ufw) blocking outgoing connections. Disable it temporarily to test:
If that works, re-enable it and add a rule to allow traffic to your server's port:sudo ufw disablesudo ufw allow out to <your-server-ip> port <server-port> - Verify server access controls: Ensure your IoT server isn't restricting access to specific IPs (like your local computer's address). Check the server's firewall, security groups, or access lists to confirm your Pi's public/private IP is whitelisted.
- Rule out Pi network stability: If you're using WiFi, switch to an Ethernet cable to eliminate wireless issues. You can also restart the Pi's network service:
sudo systemctl restart dhcpcd - Double-check code config: Confirm your script uses the correct server IP and port (no typos!). If your local computer relies on port forwarding to reach the server, make sure the Pi is on the same network or has access to the forwarded port.
内容的提问来源于stack exchange,提问作者jewstin

