Hardware Reference
In-Depth Information
Remember what a client GET or
POST request looks like. Here's the
GET request:
8
while (client.connected()) {
if (client.available()) {
// look for whatever comes before the /. It should be GET or POST:
if(finder.getString("","/", requestTypeString,typeLength)){
// Do something different for GET or POST:
if(String(requestTypeString) == "GET " ) {
requestType = 1;
}
else if(String(requestTypeString) == "POST ") {
requestType = 2;
}
GET /index.htm HTTP/1.0
And here's a POST request that
provides a variable:
POST /response.htm HTTP/1.0
thermostat=23
Put this while() block inside the if
statement that checks to see whether
the client exists. It checks to see
whether the client is connected, and
whether it has sent any bytes. If so, it
looks for the GET or POST requests,
and gets the file requested as well. If it
gets a POST, it extracts the thermostat
variable. Then it calls sendFile() to send
the file to the client.
// gather what comes after the / into an array,
// it's the filename the client wants:
requestedFileLength = finder.getString("", " ",
fileString, fileStringLength);
// now you're done with the GET/POST line, process what you got:
switch (requestType) {
case 1: // GET
// do nothing with GET except send the file, below
break;
case 2: //POST
// skip the rest of the header,
// which ends with newline and carriage return:
finder.find("\n\r");
// if the client sends a value for thermostat, take it:
if (finder.find("thermostat")) {
int newThermostat = finder.getValue('=');
// if it's changed, save it:
if (thermostat != newThermostat) {
thermostat = newThermostat;
// constrain it to a range from 20 to 40 degrees:
thermostat = constrain(thermostat, 20, 40);
// save it to EEPROM:
EEPROM.write(thermostatAddress, thermostat);
}
}
break;
}
// whether it's GET or POST, give them the string they asked for.
// if there's nothing after the /,
// then the client wants the index:
if (requestedFileLength < 2) {
sendFile(client, "index.htm");
}
// otherwise send whatever file they asked for:
else {
sendFile(client, fileString);
}
}
8 Wait, this sendFile()
call looks different from
the one on page page 375!
The change is coming up.
ยป
 
Search WWH ::




Custom Search