/* * RobotWeb.cpp * * Created on: 7-feb.-2012 * Author: Jan Baeyens */ #include "RobotWeb.h" #define ID_MowSpeed "0" #define ID_DriveSpeed "1" #define ID_SpeedDirection "2" #define ID_GPSLocation "3" #define ID_state "4" #define ID_leftDrivePower "5" #define ID_RightDrivePower "6" #define ID_leftMowerPower "7" #define ID_RightMowerPower "8" #define MAKELABELRESPONSE(Param) "_" ID_##Param "=" #Param ";" #define MAKELABELEDIT(Param) "n_" ID_##Param "=" #Param ";" #define MAKEHTTRESPONSE(Param) ID_##Param "=" +(String( Param)) + ";" #define PRINTIFCHANGED(Param) static int pref##Param=Param; if(Param != pref##Param){ server.print(MAKEHTTRESPONSE(Param)); pref##Param=Param;} /* This command is set as the default command for the server. It * handles only GET and ignores POST requests. For a GET, it returns "index.html located * on the SD card. */ void DefaultWebCmd(WebServer &server, WebServer::ConnectionType type, char *, bool) { if (type == WebServer::GET) { SHOW_DEBUG_INFO_LN(DEBUGLEVEL_INFO, "Received a get and returning the full page"); File indexFile; indexFile = SD.open("index.htm"); if (indexFile) { int Written=0; while (indexFile.available()) { server.write(indexFile.read()); if((++Written)>90) {delay(300); Written=0; } } indexFile.close();// close the file: } delay(300); } } /* This command is set as the param url for the server. It * handles both GET and POST requests. For a GET, it returns the value of the paremeter getted * For a POST, it sets the value of the parameter posted */ void ParamCmd(WebServer &server, WebServer::ConnectionType type, char * ww, bool b) { String InValue(ww); String Param = InValue.substring(0, InValue.indexOf("=")); if (type == WebServer::GET) // I use get to read values { SHOW_DEBUG_INFO_LN(DEBUGLEVEL_INFO, "Arduino received a param get request"); SHOW_DEBUG_INFO_VAR(DEBUGLEVEL_INFO, ww); if (Param.equals("MowSpeed")) server.print(MowSpeed); else if (Param.equals("DriveSpeed")) server.print(DriveSpeed); else if (Param.equals("SpeedDirection")) server.print(SpeedDirection); else if (Param.equals("GPSLocation")) server.print("GPSLocation value"); else if (Param.equals("state")) server.print("state value"); else if (Param.equals("leftDrivePower")) server.print("leftDrivePowerConsumption value"); else if (Param.equals("RightDrivePower")) server.print("RightDrivePowerConsumption value"); else if (Param.equals("leftMowerPower")) server.print("leftMowerPowerConsumption value"); else if (Param.equals("RightMowerPower")) server.print("RightMowerPowerConsumption value"); else { server.print("unknown value "); server.print(Param); } } else if (type == WebServer::POST) // I use post to set values { int NewValue = InValue.substring(InValue.indexOf("=") + 1).toInt(); SHOW_DEBUG_INFO_LN(DEBUGLEVEL_INFO, "Arduino received a param set request "); SHOW_DEBUG_INFO_VAR(DEBUGLEVEL_INFO, ww); SHOW_DEBUG_INFO_PARAM_LN(DEBUGLEVEL_INFO, NewValue); if (Param.equals("MowSpeed")) MowSpeed = NewValue; else if (Param.equals("DriveSpeed")) DriveSpeed = NewValue; else if (Param.equals("SpeedDirection")) SpeedDirection = NewValue; else { server.print("Unsupported value "); server.print(Param); } server.print(";"); } } /* This command is set as the /params? url for the server. It * handles GET and ignores POST requests. For a GET, it returns a simple * page with the paramnames and the param values in a ID_0=Paramame;0=ParamValue;. */ void GetAllParamsCmd(WebServer &server, WebServer::ConnectionType type, char * ww, bool b) { if (type == WebServer::GET) // I use get to read values { SHOW_DEBUG_INFO_LN(DEBUGLEVEL_INFO, "Arduino received a ALLparam get request"); SHOW_DEBUG_INFO_VAR(DEBUGLEVEL_INFO, ww); // send the output labels server.print(MAKELABELRESPONSE(MowSpeed)); server.print(MAKELABELRESPONSE(DriveSpeed)); server.print(MAKELABELRESPONSE(SpeedDirection)); server.print(MAKELABELRESPONSE(GPSLocation)); server.print(MAKELABELRESPONSE(state)); server.print(MAKELABELRESPONSE(leftDrivePower)); server.print(MAKELABELRESPONSE(RightDrivePower)); server.print(MAKELABELRESPONSE(leftMowerPower)); server.print(MAKELABELRESPONSE(RightMowerPower)); //send the input labels server.print(MAKELABELEDIT(MowSpeed)); server.print(MAKELABELEDIT(DriveSpeed)); server.print(MAKELABELEDIT(SpeedDirection)); // send the values server.print(MAKEHTTRESPONSE(MowSpeed)); server.print(MAKEHTTRESPONSE(DriveSpeed)); server.print(MAKEHTTRESPONSE(SpeedDirection)); server.print(MAKEHTTRESPONSE(GPSLocation)); server.print(MAKEHTTRESPONSE(state)); server.print(MAKEHTTRESPONSE(leftDrivePower)); server.print(MAKEHTTRESPONSE(RightDrivePower)); server.print(MAKEHTTRESPONSE(leftMowerPower)); server.print(MAKEHTTRESPONSE(RightMowerPower)); } } /* This command is set as the /change url for the server. It * handles GET and ignores POST requests. For a GET, it returns the values of the params that have changd since last reguest * the values are returned in the format 0=ParamValue;=SecondParamValue;; */ void GetChangedParams(WebServer &server, WebServer::ConnectionType type, char * ww, bool b) { if (type == WebServer::GET) // I use get to read values { SHOW_DEBUG_INFO_LN(DEBUGLEVEL_INFO, "Arduino received a ALLChangedparam get request"); SHOW_DEBUG_INFO_VAR(DEBUGLEVEL_INFO, ww); PRINTIFCHANGED(MowSpeed); PRINTIFCHANGED(DriveSpeed); PRINTIFCHANGED(SpeedDirection); PRINTIFCHANGED(GPSLocation); PRINTIFCHANGED(state); PRINTIFCHANGED(leftDrivePower); PRINTIFCHANGED(RightDrivePower); PRINTIFCHANGED(leftMowerPower); PRINTIFCHANGED(RightMowerPower); server.print(";");//Add this as the first time no values have been send } } /* * Constructor of the RobotWeb. This constructor set the web event handlers (link a function to a url) */ RobotWeb::RobotWeb() : WebServer(WEBSERVERPREFIX, 80) { setDefaultCommand(&DefaultWebCmd); /* register our default command (activated with the request of * http://x.x.x.x/WEBSERVERPREFIX */ addCommand("Param", &ParamCmd); // register the command for http://x.x.x.x/WEBSERVERPREFIX/Param?paramname[=000] addCommand("Params", &GetAllParamsCmd); // get all the params addCommand("Change", &GetChangedParams); // only get the changed params } /** This method opens the connection and starts the web server * */ int RobotWeb::begin(uint8_t *mac_address) { // setup the Ethernet library to talk to the Wiznet board if (Ethernet.begin(mac_address) == 0) { SHOW_DEBUG_INFO_LN(DEBUGLEVEL_INFO, "Failed to configure Ethernet using DHCP"); return 0; }; SHOW_DEBUG_INFO(DEBUGLEVEL_INFO, "My IP address: "); for (byte thisByte = 0; thisByte < 4; thisByte++)// print the value of each byte of the IP address: { SHOW_DEBUG_INFO_PARAM(DEBUGLEVEL_INFO, Ethernet.localIP()[thisByte]); } SHOW_DEBUG_INFO_LN(DEBUGLEVEL_INFO, ""); WebServer::begin();/* start the server to wait for connections */ return 1; } /* This method has to be called as part of the loop of your sketch. It reads the Incomming info and triggers action if needed * */ void RobotWeb::Loop() { // Let the web server do whatever it should do WebServer::loop(); }