#include #include #define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below) #if STATIC // ethernet interface ip address static byte myip[] = { 192,168,0,22 }; // gateway ip address static byte gwip[] = { 192,168,0,1 }; #endif // ethernet mac address - must be unique on your network static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; byte Ethernet::buffer[500]; // tcp/ip send and receive buffer const int ledPin = 7; BufferFiller bfill; //??????? void homePage() { // long t = millis() / 1000; // word h = t / 3600; // byte m = (t / 60) % 60; // byte s = t % 60; // String html = ""; // // int len = html.length() + 1; // char tmp[len]; // //Serial.println(tmp); // html.toCharArray(tmp, len);bfill = ether.tcpOffset(); bfill.emit_p(PSTR( //??????? "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n" "\r\n" "" // http-equiv='refresh' content='1'/>" "RBBB server" //"" "" "")); //??????? delay(1); bfill.emit_p(PSTR( "
"), PSTR("ON"), PSTR("OFF")); bfill.emit_p(PSTR("Go to Google")); // bfill.emit_p(PSTR("")); // delay(1); // // delay(1); // //bfill = ether.tcpOffset(); // for (int i = 0; i < 3; i++) { //// // word pin = 0; // bfill.emit_p(PSTR(""), // PSTR("TOTO"), PSTR("X"), PSTR("O")); // delay(2); //// // } // bfill.emit_p(PSTR("
$F$F$F
")); bfill.emit_p(PSTR("")); delay(1); } void setup() { Serial.begin(9600); Serial.println("\n[backSoon]"); pinMode(ledPin, OUTPUT); if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) Serial.println( "Failed to access Ethernet controller"); #if STATIC ether.staticSetup(myip, gwip); #else if (!ether.dhcpSetup()) Serial.println("DHCP failed"); #endif ether.printIp("IP: ", ether.myip); ether.printIp("GW: ", ether.gwip); ether.printIp("DNS: ", ether.dnsip); } const char okHeader[] PROGMEM = "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n"; const char okCSS[] PROGMEM = "HTTP/1.0 200 OK\r\n" "Content-Type: text/css\r\n" "Pragma: no-cache\r\n"; static void configPage(const char* data) { bfill.emit_p(PSTR("$F\r\n" "

Send a wireless data packet

" "
" "

" "Data bytes (decimal)
" "Destination node " "(1..31, or 0 to broadcast)
" "

" "" "
"), okHeader); } static void cssPage() { const char css[] = "a.button {" "-webkit-appearance: button;" "-moz-appearance: button;" "appearance: button;" " text-decoration: none;" "color: initial;}"; bfill.emit_p(PSTR("$F\r\n$F"), okCSS, css); } static void sendPage(const char* data) { // // pick up submitted data, if present // const char* p = strstr(data, "b="); // byte d = getIntArg(data, "d"); // if (data[6] == '?' && p != 0 && 0 <= d && d <= 31) { // // prepare to send data as soon as possible in loop() // outDest = d & RF12_HDR_MASK ? RF12_HDR_DST | d : 0; // outCount = 0; // // convert the input string to a number of decimal data bytes in outBuf // ++p; // while (*p != 0 && *p != '&') { // outBuf[outCount] = 0; // while ('0' <= *++p && *p <= '9') // outBuf[outCount] = 10 * outBuf[outCount] + (*p - '0'); // ++outCount; // } //#if SERIAL // Serial.print("Send to "); // Serial.print(outDest, DEC); // Serial.print(':'); // for (byte i = 0; i < outCount; ++i) { // Serial.print(' '); // Serial.print(outBuf[i], DEC); // } // Serial.println(); //#endif // // redirect to home page // bfill.emit_p(PSTR( // "HTTP/1.0 302 found\r\n" // "Location: /\r\n" // "\r\n")); // return; // } // else show a send form bfill.emit_p(PSTR("$F\r\n" "

Send a wireless data packet

" "
" "

" "Data bytes (decimal)
" "Destination node " "(1..31, or 0 to broadcast)
" "

" "" "
"), okHeader); } void loop(){ word len = ether.packetReceive(); word pos = ether.packetLoop(len); // check if valid tcp data is received if (pos) { delay(1); // necessary for my system bfill = ether.tcpOffset(); char* data = (char *) Ethernet::buffer + pos; Serial.println(data); // receive buf hasn't been clobbered by reply yet if (strncmp("GET / ", data, 6) == 0) { Serial.println("HomePage"); homePage(); } else if (strncmp("GET /css.css", data, 12) == 0) { Serial.println("CSS"); cssPage(); } else if (strncmp("GET /on", data, 7) == 0) { Serial.println("ON"); digitalWrite(ledPin, LOW); homePage(); } else if (strncmp("GET /off", data, 8) == 0) { Serial.println("OFF"); digitalWrite(ledPin, HIGH); homePage(); } else if (strncmp("GET /c", data, 6) == 0) { Serial.println("Config"); configPage(data); } else if (strncmp("GET /d", data, 6) == 0) { Serial.println("Data"); sendPage(data); } else { bfill.emit_p(PSTR( "HTTP/1.0 401 Unauthorized\r\n" "Content-Type: text/html\r\n" "\r\n" "

401 Unauthorized

")); } ether.httpServerReply(bfill.position()); // send web page data } }