I originally asked this question. Regarding a webserver Refresh issue on arduino.
I am totally new to web development and ethernet. The answer from @Majenko is correct, but I do not understand how to implement it. Should I change the homepage()
function's first line to HTTP/1.1 307 temporary redirect
and use META REFRESH
?
Or do the homepage()
first using HTTP/1.1 200 OK
and then for all the following requests, use HTTP/1.1 307 temporary redirect
?
-
@Majenko I would like you to answer this please.Mohsin Anees– Mohsin Anees2016年08月07日 11:15:16 +00:00Commented Aug 7, 2016 at 11:15
-
This might be of use to you: stackoverflow.com/help/merging-accountsMajenko– Majenko2016年08月07日 11:34:11 +00:00Commented Aug 7, 2016 at 11:34
1 Answer 1
You need to make a decision on what to respond with depending on what the request is you have received.
If you receive a "command" URL (which you already check for by looking for certain sub-strings) then you need to respond with the 307
response. If you don't receive a "command" URL (so anything except the things you are specifically looking for) then you respond with the homepage.
A slight re-formatting of your decision code to use else if
instead of just if
would make it easier:
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging
// homepage(); << This is now moved elsewhere.
//controls the Arduino if you press the buttons
if (readString.indexOf("?resetcount1") > 0) {
count1 = 0;
redirect();
} else if (readString.indexOf("?resetcount2") > 0) {
count2 = 0;
redirect();
} else if (readString.indexOf("?resetcount3") > 0) {
count3 = 0;
redirect();
} else if (readString.indexOf("?resetcount4") > 0) {
count4 = 0;
redirect();
} else if (readString.indexOf("?togglerelay1") > 0) {
toggleRelay1();
redirect();
} else if (readString.indexOf("?togglerelay2") > 0) {
toggleRelay2();
redirect();
} else if (readString.indexOf("?togglerelay3") > 0) {
toggleRelay3();
redirect();
} else if (readString.indexOf("?togglerelay4") > 0) {
toggleRelay4();
redirect();
} else { // If none of them match then send the homepage.
homepage();
}
}
redirect()
is as simple as:
void redirect() {
client.println("HTTP/1.1 307 Temporary Redirect");
client.println("Location: /");
client.println("Connection: Close");
client.println();
client.stop();
}