//////////////////////////////////////////////////////////////////////// // HTTP Server section //////////////////////////////////////////////////////////////////////// void startServer() { server.on ("/", handleRoot); server.on ("/slower", handleSlower); server.on ("/faster", handleFaster); server.on ("/start", handleStart); server.on ("/stop", handleStop); server.on ("/sync", handleSync); server.on ("/debug", handleDebug); server.on ("/lightdatadebug", handlelightdataDebug); server.on ("/protocoldebug", handleprotocolDebug); server.on ("/mode", handlemode); server.on ("/mode0", handlemode0); server.on ("/mode1", handlemode1); server.on ("/mode2", handlemode2); server.on ("/mode3", handlemode3); server.on ("/mode4", handlemode4); server.on ("/mode5", handlemode5); server.on ("/mode6", handlemode6); server.on ("/mode7", handlemode7); server.on ("/mode8", handlemode8); server.on ("/mode9", handlemode9); server.begin(); if (debug) Serial.println ( "HTTP server started" ); } //////////////////////////////////////////////////////////////////////////////////////////// void handleRoot() { int modecount = 0; Serial.println("starting handleroot"); strcpy(htmlpage, "\ \ \ \ \ \ \ \ \ \ \ \ \ "); strcat(htmlpage, "\ \

Christmas Lights: Controller \ "); IPAddresstochararray(controlIPaddress,controladdressaschar); strcat(htmlpage,controladdressaschar); strcat(htmlpage, "\

\ "); strcat(htmlpage, "\ \

\ "); strcat(htmlpage,modename[lightmode]); strcat(htmlpage, "\

\ "); strcat(htmlpage, "\
\

\
\ "); strcat(htmlpage," Interval: "); itoa(lightinterval[lightmode], numasstring, 10); strcat(htmlpage,numasstring); strcat(htmlpage, "\
\

\ "); strcat(htmlpage, "
\

Control\

\
\
\ "); if (operating) strcat(htmlpage, "\
\ "); else strcat(htmlpage, "\
\ "); strcat(htmlpage, "\
\
\ "); strcat(htmlpage, "
\

Debug Information

\
\
\
\
\ "); strcat(htmlpage, "\ "); Serial.println(htmlpage); server.send ( 200, "text/html", htmlpage ); } //////////////////////////////////////////////////////////////////////////////////////////// void handleSlower() { controlIPaddress = localIPaddress; if (lightinterval[lightmode] < lightintervalupperbound[lightmode]) { lightinterval[lightmode] = lightinterval[lightmode] + lightincrement[lightmode]; setintervalbuffer(); if (debug) { Serial.print("Slower pressed: "); Serial.println(intervalbuffer); } if (sync) assumecontrol(); setlighttimer(); } redirectclient(); } //////////////////////////////////////////////////////////////////////////////////////////// void handleFaster() { controlIPaddress = localIPaddress; if (lightinterval[lightmode] > lightintervallowerbound[lightmode]) { lightinterval[lightmode] = lightinterval[lightmode] - lightincrement[lightmode]; setintervalbuffer(); if (debug) { Serial.print("Faster pressed: "); Serial.println(intervalbuffer); } if (sync) assumecontrol(); setlighttimer(); } redirectclient(); } //////////////////////////////////////////////////////////////////////////////////////////// void handleStart() { controlIPaddress = localIPaddress; if (debug) Serial.println("Start selected "); starttimers(); if (sync) { assumecontrol(); broadcast("s"); } operating = 1; redirectclient(); } void handleStop() { controlIPaddress = localIPaddress; if (debug) Serial.println("Stop selected "); stoptimers(); if (sync) { assumecontrol(); broadcast("e"); } operating = 0; redirectclient(); } //////////////////////////////////////////////////////////////////////////////////////////// //sync or not. //default is set to sync. //if we opt out if the sync, then we need to check when starting and stopping the timers, and ignore the transmissions from other nodes. void handleSync() { controlIPaddress = localIPaddress; if (debug) Serial.println("Sync selected "); // assumecontrol(); stoptimers(); sync = !sync; // if (sync) // { // assumecontrol(); // } starttimers(); redirectclient(); } //////////////////////////////////////////////////////////////////////////////////////////// // The debug handlers don't take control on the web page being used. void handleDebug() { if (debug) Serial.println("Debug off."); else Serial.println("Debug on."); debug = !debug; redirectclient(); } void handlelightdataDebug() { if (lightdatadebug) Serial.println("Light Data Debug off."); else Serial.println("Light Data Debug on."); lightdatadebug = !lightdatadebug; redirectclient(); } void handleprotocolDebug() { if (protocoldebug) Serial.println("Protocol Debug off."); else Serial.println("Protocol Debug on."); protocoldebug = !protocoldebug; redirectclient(); } //////////////////////////////////////////////////////////////////////////////////////////// void handlemode() { if (debug) Serial.println(modename[lightmode]); lightmode++; if (sync) { if (lightmode >= numberofmodes) lightmode = 0; // reportparameters(); setmodebuffer(); setintervalbuffer(); starttimers(); assumecontrol(); IPAddresstochararray(localIPaddress,controladdressaschar); } redirectclient(); } //////////////////////////////////////////////////////////////////////////////////////////// void changemode() { if (debug) Serial.println(modename[lightmode]); controlIPaddress = localIPaddress; if (sync) { if (lightmode >= numberofmodes) lightmode = 0; setmodebuffer(); setintervalbuffer(); starttimers(); assumecontrol(); IPAddresstochararray(localIPaddress,controladdressaschar); } redirectclient(); } //////////////////////////////////////////////////////////////////////////////////////////// void handlemode0() { lightmode=0; changemode(); } void handlemode1() { lightmode=1; changemode(); } void handlemode2() { lightmode=2;; changemode(); } void handlemode3() { lightmode=3; changemode(); } void handlemode4() { lightmode=4; changemode(); } void handlemode5() { lightmode=5; changemode(); } void handlemode6() { lightmode=6; changemode(); } void handlemode7() { lightmode=7; changemode(); } void handlemode8() { lightmode=8; changemode(); } void handlemode9() { lightmode=9; changemode(); } //////////////////////////////////////////////////////////////////////////////////////////// void handleClient() { server.handleClient(); } void redirectclient() { server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect }