获取正确IP但无法向Domoticz发送请求,求问题排查
Arduino向Domoticz发送HTTP请求失败的排查方案
1. 核对网络库的引脚配置
你使用的ENC28J60模块通过PIN10作为CS引脚、ICSP端口(对应SPI的SCK=13、MOSI=11、MISO=12)连接,需确保网络库初始化时指定了正确的CS引脚:
- 若用
EtherCard库,初始化代码要写成:ether.begin(sizeof Ethernet::buffer, mymac, 10);(最后一个参数是CS引脚号10) - 若用
UIPEthernet库,初始化时需调用Ethernet.begin(mac, ip, dns, gateway, subnet, 10);(最后一个参数指定CS引脚)
引脚配置不匹配会导致模块无法正常发送数据,这也是你之前按手册接11-13引脚无法工作的核心原因。
2. 完善HTTP请求的触发逻辑
基础网络示例仅完成了DHCP初始化,没有主动发起HTTP请求的代码。以下是适配ENC28J60的请求示例(以EtherCard库为例):
#include <EtherCard.h> // 自定义MAC地址,可修改为你模块的实际MAC static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 }; byte Ethernet::buffer[700]; static uint32_t timer; void setup(){ Serial.begin(9600); // 初始化ENC28J60,指定CS引脚为10 if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) { Serial.println("ENC28J60初始化失败"); while(1); } // DHCP获取IP if (!ether.dhcpSetup()) { Serial.println("DHCP分配IP失败"); while(1); } ether.printIp("Arduino IP: ", ether.myip); // 首次发送请求 sendDomoticzRequest(); } void sendDomoticzRequest(){ const char* domoticzIp = "192.168.1.204"; uint16_t domoticzPort = 8084; // 构建标准HTTP GET请求 char reqBuf[] = "GET /json.htm?type=command¶m=udevice&idx=3&svalue=5000 HTTP/1.1\r\n" "Host: 192.168.1.204:8084\r\n" "Connection: close\r\n\r\n"; // 连接Domoticz并发送请求 if (ether.clientConnect(domoticzIp, domoticzPort)) { ether.clientWrite(reqBuf, strlen(reqBuf)); Serial.println("请求已发送"); } else { Serial.println("无法连接到Domoticz"); } } void loop(){ ether.packetLoop(ether.packetReceive()); // 每5秒重复发送请求(可根据需求调整) if (millis() - timer > 5000) { timer = millis(); sendDomoticzRequest(); } }
若使用UIPEthernet库,可参考Arduino WiFi库的Client类写法,核心是调用client.connect()建立连接后,通过client.print()发送请求内容。
3. 验证网络连通性
虽然能ping通Arduino,但仍需确认Arduino能访问Domoticz设备:
- 在
setup()中添加ping测试代码(EtherCard库):if (ether.pingIp(ether.parseIp("192.168.1.204"))) { Serial.println("Ping Domoticz成功"); } else { Serial.println("Ping Domoticz失败"); } - 若ping失败,检查网关配置是否正确(可通过
ether.printIp("GW: ", ether.gwip)打印网关IP,确认与路由器IP一致),同时检查ICSP引脚接线是否牢固,避免SPI通信不稳定。
4. 调整Wireshark过滤规则
确保Wireshark未过滤Arduino的流量:设置过滤条件为ip.addr == <你的Arduino IP>,观察是否有TCP SYN包(连接请求)发出。若没有SYN包,说明代码未触发连接逻辑或模块发送功能异常。
5. 检查Domoticz访问权限
确认Domoticz未启用IP白名单限制,或已将Arduino的IP加入允许访问列表,避免请求被Domoticz拦截。
内容的提问来源于stack exchange,提问作者Lucas
相关产品推荐
相关产品推荐

