1
我有一個帶有Wiznet以太網盾的Arduino Uno,它可以很好地連接到路由器。但是經過一段時間(大約30秒)後,它會失去與路由器的連接並進入脫機狀態。有人在代碼中看到它的原因嗎?Arduino以太網在30秒後失去連接
#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte ip[] = { 192, 168, 178, 99 };
//byte gateway[] = { 192, 168, 178, 1 };
//byte subnet[] = { 255, 255, 255, 0 };
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // if need to change the MAC address (Very Rare)
EthernetServer server = EthernetServer(80); //port 80
const int ledPin = 2; // LED Pin
////////////////////////////////////////////////////////////////////////
void setup(){
pinMode(ledPin, OUTPUT); //Pins 10,11,12 & 13 are used by the ethernet shield
digitalWrite(ledPin, LOW);
Ethernet.begin(mac);
server.begin();
}
void loop(){
checkForClient(); // listen for incoming clients, and process qequest.
}
void checkForClient(){
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
if(!sentHeader){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
sentHeader = true;
}
char c = client.read();
if(reading && c == ' ') reading = false;
if(c == '?') reading = true; //found the ?, begin reading the info
if(reading){
switch (c) {
case 'on':
digitalWrite(ledPin, HIGH);
break;
case 'off':
digitalWrite(ledPin, LOW);
break;
}
}
if (c == '\n' && currentLineIsBlank) break;
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
}
}
你是什麼意思的「下線」?停止響應http請求,停止響應ARP請求,路由器不再檢測到該端口上的鏈接,或者可能是其他內容? – Craig
不是一個完整的答案,但變量c是一個字符。它永遠不會匹配任何開關情況。 – jdr5ca