You are on page 1of 4

/* Web_Demo.

pde -- sample code for Webduino server library */ /* * To use this demo, enter one of the following USLs into your browser. * Replace "host" with the IP address assigned to the Arduino. * * http://host/demo * * This URL brings up a display of the values READ on digital pins 0-9 * and analog pins 0-5. This is done with a call to defaultCmd. * * * http://host/demo/form * * This URL also brings up a display of the values READ on digital pins 0-9 * and analog pins 0-5. But it's done as a form, by the "formCmd" function, * and the digital pins are shown as radio buttons you can change. * When you click the "Submit" button, it does a POST that sets the * digital pins, re-reads them, and re-displays the form. * */ #include "Ethernet.h" #include "WebServer.h" // no-cost stream operator as described at // http://sundial.org/arduino/?page_id=119 template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } // CHANGE THIS TO YOUR OWN UNIQUE VALUE static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // CHANGE THIS TO MATCH YOUR HOST NETWORK static uint8_t ip[] = { 192, 168, 1, 64 }; #define PREFIX "/demo" 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 server void jsonCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) { if (type == WebServer::POST) { server.httpFail(); return; } server.httpSuccess(false, "application/json"); if (type == WebServer::HEAD) return; int i;

} else server << (val ? "HIGH" : "LOW"). } void outputPins(WebServer &server. ++i) { // ignore the pins we use to talk to the Ethernet chip int val = digitalRead(i).radioButton(pinName. ++i) { int val = analogRead(i). "1". . server << "\"a" << i << "\": " << val. server << "\"d" << i << "\": " << val << ". }" "</style>" "</head>" "<body>". "0". for (i = 0. text-decoration: underline }" "P { font-size: 10pt. for (i = 0. ". ++i) { // ignore the pins we use to talk to the Ethernet chip int val = digitalRead(i). } for (i = 0. i <= 9. pinName + 1. server. "On". server << "<h1>Digital Pins</h1><p>". ".printP(htmlHead). !val). server. i <= 9. itoa(i. } server << " }". if (i != 5) server << ". bool addContr ols = false) { P(htmlHead) = "<html>" "<head>" "<title>Arduino Web Server</title>" "<style type=\"text/css\">" "BODY { font-family: sans-serif }" "H1 { font-size: 14pt. server << "Digital " << i << ": ".server << "{ ". if (addControls) server << "<form action='" PREFIX "/form' method='post'>". WebServer::ConnectionType type. server << " ". int i. 10). i <= 5.httpSuccess(). server. if (addControls) { char pinName[4].radioButton(pinName. val). "Off". pinName[0] = 'd'. server.

WebServer::ConnectionType type. pinMode(9. int val = strtoul(value. type.addCommand("json". true). server. i <= 5. Ethernet. 16). } void defaultCmd(WebServer &server. i <= 9. char name[16].begin(mac. bool tail_complete) { outputPins(server. 16. } server << "</p>". type. OUTPUT). . &jsonCmd). ++i) pinMode(i. false). if (addControls) server << "<input type='submit' value='Submit'/></form>". for (i = 0.readPOSTparam(name.setDefaultCommand(&defaultCmd). WebServer::ConnectionType type. digitalWrite(pin. ip). webserver. server << "Analog " << i << ": " << val << "<br/>". val). server << "</body></html>". } else outputPins(server. webserver. } void formCmd(WebServer &server. 10). webserver. do { repeat = server. char *url_tai l. char *url_tail. NULL. INPUT). if (name[0] == 'd') { int pin = strtoul(name + 1. bool tail_complete) { if (type == WebServer::POST) { bool repeat. } server << "</p><h1>Analog Pins</h1><p>".httpSeeOther(PREFIX "/form"). value[16]. 10). } } while (repeat).begin(). NULL. } void setup() { // set pins 0-8 for digital input for (int i = 0.server << "<br/>". value. ++i) { int val = analogRead(i).

// if you wanted to do other work based on a connecton. } void loop() { // process incoming connections one at a time forever webserver.processConnection().webserver.addCommand("form". it would go here } . &formCmd).