Chr*_*cha 5 c avr arduino avr-gcc
我正在使用带有以太网盾的Arduino Uno.
发送许多HTTP请求后,client.println(...),客户端连接时开始失败.失败的时间似乎是随机的,并且循环的序列读数可以在~1000到~7000之间的任何地方变化.
该错误与以太网发送缓冲区溢出无关(遵循此建议)
这是失败的代码:
#include <Ethernet.h>
#include <SPI.h>
// Network constants
byte mac[] = {0x00, 0x23, 0xdf, 0x82, 0xd4, 0x01};
byte ip[] = {/*REDACTED*/};
byte server[] = {/*REDACTED*/};
int port = /*REDACTED*/;
Client client(server, port);
// State
int sequence;
void setup(){
Ethernet.begin(mac, ip);
Serial.begin(9600);
sequence = 0;
delay(1000);
}
void loop(){
httpPut("/topic/:test/publish?sessionId=SESenanhygrp");
Serial.println(sequence++);
}
void httpPut(char* url){
if (!client.connect()) {
Serial.println("EXCEPTION: during HTTP PUT. Could not connect");
return;
}
client.print("PUT");
client.print(" ");
client.print(url);
client.println(" HTTP/1.0");
client.println();
while(!client.available()){
delay(1);
}
while(client.available()) {
char c = client.read();
Serial.print(c);
}
while(client.connected()){
Serial.println("Waiting for server to disconnect");
}
client.stop();
}
Run Code Online (Sandbox Code Playgroud)
错误发生在以下段中
if (!client.connect()) {
Serial.println("EXCEPTION: during HTTP PUT. Could not connect");
return;
}
Run Code Online (Sandbox Code Playgroud)
v22 中的 Arduino 以太网库存在一个错误(如Linux/Windows V0022/1.0 已解决的以太网问题中所述)。
对我来说,解决方案是使用Ethernet2库(由tinker.it 的Peter 编写)。该代码需要进行一些小的修改,但现在一切似乎都工作正常。我已经成功发送超过 40000 条 HTTP 消息,没有任何问题。(偶尔会出现单条消息无法发送的情况,但这种错误率小于4%。)