Hi, I'm having some problems with the response from a GET operation.
The problem is that when I read the console, the GET is sort of preformed twice. The first time it's ok. But the second time the values are gone.
The console response:
first time the client connects -> site requesting the values
the second time -> site posting values
I think the site is requesting values, then posting new one's and then requesting them again. Is there a way I can "remember" the item & value from the post?
server is at 192.168.1.177
GET /verlichting HTTP/1.0
Test =0
Request= lights
Item=
Value =
Test =0
Request= lights
Item=
Value =
{room1="0"}{room2="0"}{room3="0"}{room4="0"}
client disonnected
GET /lights/?values=/{room1="0"}{room2="0"}{room3="0"}{room4="0"} HTTP/1.0
Test =1
Request= lights
Item= ?values=
Value = {room1="0"}{room2="0"}{room3="0"}{room4="0"}
Test =0
Request= lights
Item=
Value =
{room1="0"}{room2="0"}{room3="0"}{room4="0"}
client disonnected
GET /lights HTTP/1.0
Request= lights
Item=
Value =
Request= lights
Item=
Value =
{room1="0"}{room2="0"}{room3="0"}{room4="0"}
client disonnected
The code:
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if(c != '\n' && c != '\r' && index < 255){ // Reads until either an eol character is reached or the buffer is full
clientline[index++] = c;
continue;
}
String urlString = String(clientline);
// extract the operation
String op = urlString.substring(0,urlString.indexOf(' '));
// we're only interested in the first part...
urlString = urlString.substring(urlString.indexOf('/'), urlString.indexOf(' ', urlString.indexOf('/')));
urlString.toCharArray(clientline, 255);
Serial.println();
char *i;
char *request = strtok_r(clientline,"/", &i);
char *item = strtok_r(NULL, "/", &i);
char *value = strtok_r(NULL, "/", &i);
Serial.print("Request= "); Serial.println(request);
Serial.print("Item= "); Serial.println(item);
Serial.print("Value = "); Serial.println(value);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
//client.println("<meta http-equiv=\"refresh\" content=\"5\">");
String restBegin = "{";
String restEnd = "\"}";
String restResponse = String();
if (strcmp(request, "heating")==0){
client.println("
");
String restHeating = "recived heating request";
restResponse = restBegin + restHeating + restEnd;
Serial.println(restResponse);
client.println(restResponse);
client.println("
");
client.println("</html>");
}
if (strcmp(request, "lights")==0){
if (strcmp(item, NULL)==0){
client.println("
");
// Building a REST response
String restRoom1 = "room1=\""; String restRoom2 = "room2=\"";
String restRoom3 = "room3=\""; String restRoom4 = "room4=\"";
restResponse = restBegin + restRoom1 + room1[4] + restEnd;
restResponse += restBegin + restRoom2 + room2[4] + restEnd;
restResponse += restBegin + restRoom3 + room3[4] + restEnd;
restResponse += restBegin + restRoom4 + room[4] + restEnd;
Serial.println(restResponse);
client.println(restResponse);
client.println("
");
client.println("</html>");
}
if (strcmp(item, "?values=")){
Serial.print("Values entering console: "); Serial.println(value);
}
}
else {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
Serial.println();