You may have read one of my last postings: Building a basic webinterface for your ESP8266 . If you have had a look into the code you will have noticed that you can adjust the measuring unit in the configuration. But what if you have a guest who comes from the United States of America and you have configured to display Celsius? We will spend some time on this scenario and I will show you how you can solve this problem.
The easiest way to solve this problem would be to offer different URLs. For example /temperature/c and temperature/f. I think it is a proper solution for this simple scenario, but what happens if we want to do some more exciting stuff with our parameter? If we want to query specific endpoints we can use GET-parameters. You may have noticed them in some URLs. These parameters take place after a question mark. For example: /getData?nodeID=13
First we need to be able to check if one or more parameters are available. The following function returns the quantity of available parameters. (The server-variable's type is ESP8266WebServer.)
server.args()
So now we know how many parameters are available but we have no idea of their name and their value. For demonstration-purposes we can loop through all items and list them via the serial-interface:
for (int i = 0; i < server.args(); i++) {
Serial.print(String(i) + " "); //print id
Serial.print("\"" + String(server.argName(i)) + "\" "); //print name
Serial.println("\"" + String(server.arg(i)) + "\""); //print value
}
This example shows that we can get the value through the id. But this practice should not be the preferred way. The library provides a function which gives you the possibility to query the value by giving the name of the parameter.
server.arg("measuringUnit"); //provides the value of the "measuringUnit"-parameter
As you can see we are using the same function with a string instead of an integer. The first question which came to my mind was "What happens if this parameter does not exist?". The answer is that the returned string is empty. To determine the existence there is another function, which returns a boolean instead of a string.
server.hasArg("measuringUnit");
So now we have listed all the important functions and can use them in our code.
This is not the only way to send data to our node. If we want to send complex data we should switch to POST-data. I am going to write an additional post on this topic.