221 lines
6.4 KiB
C++
Executable File
221 lines
6.4 KiB
C++
Executable File
#include <EtherCard.h>
|
||
#include <Client.h>
|
||
|
||
|
||
#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 = "<img src=\"http://www.ac-grenoble.fr/ien.vienne1-2/spip/IMG/bmp_Image004.bmp\">";
|
||
//
|
||
// 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"
|
||
"<meta></meta>" // http-equiv='refresh' content='1'/>"
|
||
"<html><head><title>RBBB server</title>"
|
||
//"<link rel=""stylesheet"" type=""text/css"" href=""css.css"">"
|
||
"</head>"
|
||
"<body>")); //???????
|
||
delay(1);
|
||
bfill.emit_p(PSTR(
|
||
"<form method=""get"" action=""/on""><button type=""submit"">$F</button></FORM><form method=""get"" action=""/off""><button type=""submit"">$F</button></FORM>"),
|
||
PSTR("ON"), PSTR("OFF"));
|
||
|
||
bfill.emit_p(PSTR("<a href=""http://google.com"" class=""button"">Go to Google</a>"));
|
||
// bfill.emit_p(PSTR("<TABLE>"));
|
||
// delay(1);
|
||
//
|
||
// delay(1);
|
||
// //bfill = ether.tcpOffset();
|
||
// for (int i = 0; i < 3; i++) {
|
||
//// // word pin = 0;
|
||
// bfill.emit_p(PSTR("<TR><TD>$F</TD><TD>$F</TD><TD>$F</TD></TR>"),
|
||
// PSTR("TOTO"), PSTR("X"), PSTR("O"));
|
||
// delay(2);
|
||
////
|
||
// }
|
||
// bfill.emit_p(PSTR("</TABLE></body></html>"));
|
||
bfill.emit_p(PSTR("</body></html>"));
|
||
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"
|
||
"<h3>Send a wireless data packet</h3>"
|
||
"<form>"
|
||
"<p>"
|
||
"Data bytes <input type=text name=b size=50> (decimal)<br>"
|
||
"Destination node <input type=text name=d size=3> "
|
||
"(1..31, or 0 to broadcast)<br>"
|
||
"</p>"
|
||
"<input type=submit value=Send>"
|
||
"</form>"), 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"
|
||
"<h3>Send a wireless data packet</h3>"
|
||
"<form>"
|
||
"<p>"
|
||
"Data bytes <input type=text name=b size=50> (decimal)<br>"
|
||
"Destination node <input type=text name=d size=3> "
|
||
"(1..31, or 0 to broadcast)<br>"
|
||
"</p>"
|
||
"<input type=submit value=Send>"
|
||
"</form>"), 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"
|
||
"<h1>401 Unauthorized</h1>"));
|
||
}
|
||
|
||
ether.httpServerReply(bfill.position()); // send web page data
|
||
|
||
}
|
||
}
|
||
|
||
|