wwwzjy 发表于 2013-9-28 12:45:45

做了个ENC28J60 用网页遥控灯的小东西。代码不知道能不能精简一下。

本帖最后由 wwwzjy 于 2013-9-28 12:50 编辑

不知道为啥,网页只能显示到LED16,再多的话网页就打不开了。






#include <EtherCard.h>

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

static byte mymac[] = {
0x47,0x96,0x69,0xD2,0x03,0x13};
byte Ethernet::buffer;


void setup () {

Serial.begin(57600);
Serial.println("WebLeds Demo");


// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(3);

// Optional set pulse length.
// mySwitch.setPulseLength(320);

// Optional set protocol (default is 1, will work for most outlets)
// mySwitch.setProtocol(2);

// Optional set number of transmission repetitions.
// mySwitch.setRepeatTransmit(15);


if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
else
    Serial.println("Ethernet controller initialized");

if (!ether.dhcpSetup())
    Serial.println("Failed to get configuration from DHCP");
else
    Serial.println("DHCP configuration done");

ether.printIp("IP Address:\t", ether.myip);
ether.printIp("Netmask:\t", ether.mymask);
ether.printIp("Gateway:\t", ether.gwip);
}

void loop() {

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

if(pos) {

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED01") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(1000, 24);
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED02") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(2000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED03") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(3000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED04") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(4000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED05") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(5000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED06") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(6000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED07") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(7000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED08") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(8000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED09") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(9000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(10000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED11") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(11000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED12") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(12000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED13") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(13000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED14") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(14000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED15") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(15000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED16") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(16000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED17") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(17000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED18") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(18000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED19") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(19000, 24);      
    }

    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED20") != 0) {
      Serial.print((char *)Ethernet::buffer + pos);
      mySwitch.send(20000, 24);      
    }



    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><title>WebLeds</title></head><body>"
      "Toggle leds: "

      "<br /><a href=\"/?LED01\">LED01</a>"
      "<br /><a href=\"/?LED02\">LED02</a>"
      "<br /><a href=\"/?LED03\">LED03</a>"
      "<br /><a href=\"/?LED04\">LED04</a>"
      "<br /><a href=\"/?LED05\">LED05</a>"
      "<br /><a href=\"/?LED06\">LED06</a>"
      "<br /><a href=\"/?LED07\">LED07</a>"
      "<br /><a href=\"/?LED08\">LED08</a>"
      "<br /><a href=\"/?LED09\">LED09</a>"
      "<br /><a href=\"/?LED10\">LED10</a>"
      "<br /><a href=\"/?LED11\">LED11</a>"
      "<br /><a href=\"/?LED12\">LED12</a>"
      "<br /><a href=\"/?LED13\">LED13</a>"
      "<br /><a href=\"/?LED14\">LED14</a>"
      "<br /><a href=\"/?LED15\">LED15</a>"
      "<br /><a href=\"/?LED16\">LED16</a>"


   
      "</body></html>"));
    ether.httpServerReply(bfill.position());
}

}


页: [1]
查看完整版本: 做了个ENC28J60 用网页遥控灯的小东西。代码不知道能不能精简一下。