HTTP form submit & ESP.restart()

Hi,

When I try to to press a button inside an HTML form to restart the esp8266, it does restart, but the HTML page then attempts to load for a while and then eventually gets stuck on the URI of the submit form instead of the root.

I have tried various approaches. Adding delays sometimes work, sometimes does not, it is not consistent in behavior. Using window.stop() or meta tags to reload automatically does not seem to work either and to be honest if even meta tags did work I would not want the page to reload constantly.

So my question is as follows: when triggering the ESP.restart() via the form button, how can I go back to the root html page in a stopped status (not loading indefinitely or stuck on stuck on ip/TEST in my case)?

My code is below:

#include <WiFiManager.h>
#include <ESP8266WebServer.h>       
#include <ESP8266WiFi.h>

const char s0[] PROGMEM = "<!DOCTYPE html><head></head><html><body> \n";
const char s5[] PROGMEM = "<label><font color=\"black\" size=\"5px\"><u><b>TEST</b></u></font></label>

\n\n";
const char s10[] PROGMEM = "<form action=\"/TEST\" method=\"POST\">\n";
const char s15[] PROGMEM = "<label>Press button.</label>

\n";
const char s20[] PROGMEM = "<input type=\"submit\" style=\"color:black;font-weight:bold;\" value=\"Restart now\"> </form>

\n";
const char s25[] PROGMEM = "<label>It will re-start Now.</label>\n\n";
const char s30[] PROGMEM = "</body></html>";
const char* const s_tbl[] PROGMEM = { s0, s5, s10, s15, s20, s25, s30};

int html_count = 0;
int html_lines() {
  html_count = sizeof(s_tbl) / sizeof(s_tbl[0]); return html_count;
}

const int htmlbufsiz = 7 * 160;
char htmlbuf[htmlbufsiz];
void hmpg() {
  strcpy(htmlbuf,"");
  for (int c = 0; c < html_lines(); c++) {
    strcat_P(htmlbuf, (char*)pgm_read_dword(&(s_tbl[c])));
  }
}

ESP8266WebServer server(80);
void handleRoot() {
  server.send(200,"text/html", htmlbuf);
}
void handleNotFound(){
  server.send(404, "text/plain", "404: Not found");
}
void handleTEST(){
  server.sendHeader("Location","/");
  server.send(303);
  ESP.restart();
}


void setup() {
  Serial.begin(74880);
  
  WiFiManager wifiManager;
  wifiManager.autoConnect("AccessPoint");
  
  server.begin();
  Serial.println("HTTP server started");
  
  hmpg();
  server.sendHeader("Location","/");
  server.send(303);
  
  server.on("/TEST", HTTP_POST, handleTEST);
  server.on("/", HTTP_GET, handleRoot);
  server.onNotFound(handleNotFound);
  
}


void loop() {
  server.handleClient();
  delay(100);
}

Thank you!

Try adding this line into in between & <META http-equiv=\"refresh\" content=\"15;URL=/\">That should tell the browser to load the root after 15 seconds

@noiasca: Thank you for the suggestions.

  • Using GET did not help.
  • I used a combination of delays up and down the code but it did not work
    I have not tried yet the other suggestions as I eventually found an 'easy' solution to this problem (more below).

@Deva_Rishi: Thank you. As I had mentioned in my original post I had tried to refresh using meta tags but it does not work either. I appreciate your suggestion though, this is something I had thought about as well.

So, in case it helps somebody else, the way I fixed this issue was to add these 2 lines of code in the handleTEST function just before the ESP.restart() line:

server.stop();
delay(10);

The small delay is necessary after server.stop() or it does not work otherwise.
So the function now looks like this:

void handleTEST(){
  server.sendHeader("Location","/");
  server.send(303);
  server.stop();
  delay(10);
  ESP.restart();
}

For those who want to play around with the whole program with this change, here it is:

#include <WiFiManager.h>
#include <ESP8266WebServer.h>       
#include <ESP8266WiFi.h>

const char s0[] PROGMEM = "<!DOCTYPE html><head></head><html><body> \n";
const char s5[] PROGMEM = "<label><font color=\"black\" size=\"5px\"><u><b>TEST</b></u></font></label>

\n\n";
const char s10[] PROGMEM = "<form action=\"/TEST\" method=\"POST\">\n";
const char s15[] PROGMEM = "<label>Press button.</label>

\n";
const char s20[] PROGMEM = "<input type=\"submit\" style=\"color:black;font-weight:bold;\" value=\"Restart now\" onsubmit=\"return false\" > </form>

\n";
const char s25[] PROGMEM = "<label>It will re-start Now.</label>\n\n";
const char s30[] PROGMEM = "</body></html>";
const char* const s_tbl[] PROGMEM = { s0, s5, s10, s15, s20, s25, s30};

int html_count = 0;
int html_lines() {
  html_count = sizeof(s_tbl) / sizeof(s_tbl[0]); return html_count;
}

const int htmlbufsiz = 7 * 160;
char htmlbuf[htmlbufsiz];
void hmpg() {
  strcpy(htmlbuf,"");
  for (int c = 0; c < html_lines(); c++) {
    strcat_P(htmlbuf, (char*)pgm_read_dword(&(s_tbl[c])));
  }
}

ESP8266WebServer server(80);
void handleRoot() {
  server.send(200,"text/html", htmlbuf);
}
void handleNotFound(){
  server.send(404, "text/plain", "404: Not found");
}
void handleTEST(){
  server.sendHeader("Location","/");
  server.send(303);
  server.stop();
  delay(10);
  ESP.restart();
}


void setup() {
  Serial.begin(74880);
  
  WiFiManager wifiManager;
  wifiManager.autoConnect("AccessPoint");
  server.begin();
  Serial.println("HTTP server started");
  
  hmpg();
  server.sendHeader("Location","/");
  server.send(303);
  
  server.on("/TEST", HTTP_POST, handleTEST);
  server.on("/", HTTP_GET, handleRoot);
  server.onNotFound(handleNotFound);  
}


void loop() {
  server.handleClient();
  delay(100);
}

It now works like a charm and never get stuck in the /TEST page/URI. I have been the recipient of much appreciated help on this forum, so I hope this, in turn, can be helpful to somebody else as well.

73,
Arnaud

@Deva_Rishi: Thank you. As I had mentioned in my original post I had tried to refresh using meta tags but it does not work either. I appreciate your suggestion though, this is something I had thought about as well.

I have been having issues with it as well, but the way it is used within ESP8266HTTPUpdateServer.h actually does. I am not sure if managed to correctly transfer that snippet, and to be honest i still have to implement it in some of my code. (i have done it partly, but have been busy with a few other things, so it was a bit of an ehhhhh ?! )