diff options
Diffstat (limited to 'Webduino/examples/Web_Parms')
-rw-r--r-- | Webduino/examples/Web_Parms/Web_Parms.ino | 287 |
1 files changed, 287 insertions, 0 deletions
diff --git a/Webduino/examples/Web_Parms/Web_Parms.ino b/Webduino/examples/Web_Parms/Web_Parms.ino new file mode 100644 index 0000000..4b848a5 --- /dev/null +++ b/Webduino/examples/Web_Parms/Web_Parms.ino @@ -0,0 +1,287 @@ +/* Web_Parms_1.pde - very simple Webduino example of parameter passing and parsing */ + +/* + * This is mostly a tool for testing and debugging the library, but can + * also be used as an example of coding for it. + * + * To use it, enter one of the following USLs into your browser. + * Replace "host" with the IP address assigned to the Arduino. + * + * http://host/ + * http://host/index.html + * + * These return a "success" HTTP result and display the parameters + * (if any) passed to them as a single string, without attempting to + * parse them. This is done with a call to defaultCmd. + * + * + * http://host/raw.html + * + * This is essentially the same as the index.html URL processing, + * but is done by calling rawCmd. + * + * + * http://host/parsed.html + * + * This invokes parsedCmd, which displays the "raw" parameter string, + * but also uses the "nexyURLparam" routine to parse out the individual + * parameters, and display them. + */ + + +#define WEBDUINO_FAIL_MESSAGE "<h1>Request Failed</h1>" +#include "SPI.h" // new include +#include "avr/pgmspace.h" // new include +#include "Ethernet.h" +#include "WebServer.h" + +#define VERSION_STRING "0.1" + +/* CHANGE THIS TO YOUR OWN UNIQUE VALUE. The MAC number should be + * different from any other devices on your network or you'll have + * problems receiving packets. */ +static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; + + +/* CHANGE THIS TO MATCH YOUR HOST NETWORK. Most home networks are in + * the 192.168.0.XXX or 192.168.1.XXX subrange. Pick an address + * that's not in use and isn't going to be automatically allocated by + * DHCP from your router. */ +static uint8_t ip[] = { 192, 168, 1, 210 }; + +// ROM-based messages used by the application +// These are needed to avoid having the strings use up our limited +// amount of RAM. + +P(Page_start) = "<html><head><title>Web_Parms_1 Version " VERSION_STRING "</title></head><body>\n"; +P(Page_end) = "</body></html>"; +P(Get_head) = "<h1>GET from "; +P(Post_head) = "<h1>POST to "; +P(Unknown_head) = "<h1>UNKNOWN request for "; +P(Default_head) = "unidentified URL requested.</h1><br>\n"; +P(Raw_head) = "raw.html requested.</h1><br>\n"; +P(Parsed_head) = "parsed.html requested.</h1><br>\n"; +P(Good_tail_begin) = "URL tail = '"; +P(Bad_tail_begin) = "INCOMPLETE URL tail = '"; +P(Tail_end) = "'<br>\n"; +P(Parsed_tail_begin) = "URL parameters:<br>\n"; +P(Parsed_item_separator) = " = '"; +P(Params_end) = "End of parameters<br>\n"; +P(Post_params_begin) = "Parameters sent by POST:<br>\n"; +P(Line_break) = "<br>\n"; + + + +/* This creates an instance of the webserver. By specifying a prefix + * of "", all pages will be at the root of the server. */ +#define PREFIX "" +WebServer webserver(PREFIX, 80); + + + +/* commands are functions that get called by the webserver framework + * they can read any posted data from client, and they output to the + * server to send data back to the web browser. */ +void helloCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) +{ + /* this line sends the standard "we're all OK" headers back to the + browser */ + server.httpSuccess(); + + /* if we're handling a GET or POST, we can output our data here. + For a HEAD request, we just stop after outputting headers. */ + if (type == WebServer::HEAD) + return; + + server.printP(Page_start); + switch (type) + { + case WebServer::GET: + server.printP(Get_head); + break; + case WebServer::POST: + server.printP(Post_head); + break; + default: + server.printP(Unknown_head); + } + + server.printP(Default_head); + server.printP(tail_complete ? Good_tail_begin : Bad_tail_begin); + server.print(url_tail); + server.printP(Tail_end); + server.printP(Page_end); + +} + + +void rawCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) +{ + /* this line sends the standard "we're all OK" headers back to the + browser */ + server.httpSuccess(); + + /* if we're handling a GET or POST, we can output our data here. + For a HEAD request, we just stop after outputting headers. */ + if (type == WebServer::HEAD) + return; + + server.printP(Page_start); + switch (type) + { + case WebServer::GET: + server.printP(Get_head); + break; + case WebServer::POST: + server.printP(Post_head); + break; + default: + server.printP(Unknown_head); + } + + server.printP(Raw_head); + server.printP(tail_complete ? Good_tail_begin : Bad_tail_begin); + server.print(url_tail); + server.printP(Tail_end); + server.printP(Page_end); + +} + +#define NAMELEN 32 +#define VALUELEN 32 + +void parsedCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) +{ + URLPARAM_RESULT rc; + char name[NAMELEN]; + int name_len; + char value[VALUELEN]; + int value_len; + + /* this line sends the standard "we're all OK" headers back to the + browser */ + server.httpSuccess(); + + /* if we're handling a GET or POST, we can output our data here. + For a HEAD request, we just stop after outputting headers. */ + if (type == WebServer::HEAD) + return; + + server.printP(Page_start); + switch (type) + { + case WebServer::GET: + server.printP(Get_head); + break; + case WebServer::POST: + server.printP(Post_head); + break; + default: + server.printP(Unknown_head); + } + + server.printP(Parsed_head); + server.printP(tail_complete ? Good_tail_begin : Bad_tail_begin); + server.print(url_tail); + server.printP(Tail_end); + + if (strlen(url_tail)) + { + server.printP(Parsed_tail_begin); + while (strlen(url_tail)) + { + rc = server.nextURLparam(&url_tail, name, NAMELEN, value, VALUELEN); + if (rc == URLPARAM_EOS) + server.printP(Params_end); + else + { + server.print(name); + server.printP(Parsed_item_separator); + server.print(value); + server.printP(Tail_end); + } + } + } + if (type == WebServer::POST) + { + server.printP(Post_params_begin); + while (server.readPOSTparam(name, NAMELEN, value, VALUELEN)) + { + server.print(name); + server.printP(Parsed_item_separator); + server.print(value); + server.printP(Tail_end); + } + } + server.printP(Page_end); + +} + +void my_failCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) +{ + /* this line sends the standard "we're all OK" headers back to the + browser */ + server.httpFail(); + + /* if we're handling a GET or POST, we can output our data here. + For a HEAD request, we just stop after outputting headers. */ + if (type == WebServer::HEAD) + return; + + server.printP(Page_start); + switch (type) + { + case WebServer::GET: + server.printP(Get_head); + break; + case WebServer::POST: + server.printP(Post_head); + break; + default: + server.printP(Unknown_head); + } + + server.printP(Default_head); + server.printP(tail_complete ? Good_tail_begin : Bad_tail_begin); + server.print(url_tail); + server.printP(Tail_end); + server.printP(Page_end); + +} + + + + +void setup() +{ + /* initialize the Ethernet adapter */ + Ethernet.begin(mac, ip); + + /* setup our default command that will be run when the user accesses + * the root page on the server */ + webserver.setDefaultCommand(&helloCmd); + + /* setup our default command that will be run when the user accesses + * a page NOT on the server */ + webserver.setFailureCommand(&my_failCmd); + + /* run the same command if you try to load /index.html, a common + * default page name */ + webserver.addCommand("index.html", &helloCmd); + + /*This command is called if you try to load /raw.html */ + webserver.addCommand("raw.html", &rawCmd); + webserver.addCommand("parsed.html", &parsedCmd); + + /* start the webserver */ + webserver.begin(); +} + +void loop() +{ + char buff[64]; + int len = 64; + + /* process incoming connections one at a time forever */ + webserver.processConnection(buff, &len); +} |