为什么我的代码运行三次而不是运行三次?

时间:2019-05-05 10:26:13

标签: arduino ethercard

我在Arduino上设置了服务器,并设置了代码以通过地址栏点亮和熄灭LED,这些地址栏已连接至原型板,除了一件东西外,其他所有东西都正常工作。当我通过浏览器发送命令重置单个二极管时,它应点亮6秒钟,然后熄灭。但是此命令在任何浏览器(Chrome,Mozilla,Opera,Edge)中都连续准确运行了3次。

像这样:LED点亮6秒钟,然后闪烁,然后点亮6秒钟,然后再闪烁一次,点亮6秒钟,然后熄灭。更令我困扰的是,从手机浏览器发送命令时没有问题。这是我的代码:

#include <EtherCard.h>

#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)

// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,0,200 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };

// LED to control output
int ledPin2 = 2;
int ledPin3 = 3;

byte Ethernet::buffer[700];

char const page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
"<head><title>"
"Service Temporarily Unavailable"
"</title></head>"
"<body>"
"<h3>This page is used behind the scene</h3>"
"<p><em>"
"Commands to control LED are transferred to Arduino.<br />"
"The syntax: http://192.168.0.XX/?LED10=OFF or ON"
"</em></p>"
"<a href='/?LED2=R'>Reset 2</a>"
"<a href='/?LED3=R'>Reset 3</a>"
"</body>"
"</html>"
;

void setup() {
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);

    Serial.begin(9600);
    Serial.println("Trying to get an IP...");

    Serial.print("MAC: ");
    for (byte i = 0; i < 6; ++i) {
        Serial.print(mymac[i], HEX);
        if (i < 5)
            Serial.print(':');
    }
    Serial.println();

    if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) {
        Serial.println( "Failed to access Ethernet controller");
    } else {
        Serial.println("Ethernet controller access: OK");
    }

#if STATIC
    Serial.println( "Getting static IP.");
    if (!ether.staticSetup(myip, gwip)) {
        Serial.println( "could not get a static IP");
        blinkLed(); // blink forever to indicate a problem
    }
#else
    Serial.println("Setting up DHCP");
    if (!ether.dhcpSetup()) {
        Serial.println( "DHCP failed");
        blinkLed(); // blink forever to indicate a problem
    }
#endif

    ether.printIp("My IP: ", ether.myip);
    ether.printIp("Netmask: ", ether.netmask);
    ether.printIp("GW IP: ", ether.gwip);
    ether.printIp("DNS IP: ", ether.dnsip);
}

void loop() {

    word len = ether.packetReceive();
    word pos = ether.packetLoop(len);

    // IF LED2=ON turn it ON
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED2=ON") != 0) {
        Serial.println("Received ON command");
        digitalWrite(ledPin2, HIGH);
    }

    // IF LED2=OFF turn it OFF
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED2=OFF") != 0) {
        Serial.println("Received OFF command");
        digitalWrite(ledPin2, LOW);
    }

    // IF LED2=R reset
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED2=R") != 0) {
        Serial.println("Received Reset command");
        digitalWrite(ledPin2, HIGH);
        delay(6000);
        digitalWrite(ledPin2, LOW);
    }

    // IF LED3=ON turn it ON
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED3=ON") != 0) {
        Serial.println("Received ON command");
        digitalWrite(ledPin3, HIGH);
    }

    // IF LED3=OFF turn it OFF
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED3=OFF") != 0) {
        Serial.println("Received OFF command");
        digitalWrite(ledPin3, LOW);
    }

    // IF LED3=R reset
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED3=R") != 0) {
        Serial.println("Received Reset command");
        digitalWrite(ledPin3, HIGH);
        delay(6000);
        digitalWrite(ledPin3, LOW);
    }

    // show some data to the user
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
}

void blinkLed() {
    while (true) {
        digitalWrite(ledPin2, HIGH);
        delay(500);
        digitalWrite(ledPin2, LOW);
        delay(500);
    }
}

0 个答案:

没有答案