Post topics, source code that relate to the Arduino Platform

User avatar
By awwende
#28527 Oh that's interesting. Only error I found was in handleSave(), I assume "String = "Settings Saved ...\r\n"" should be String str? But after that adjustment and adding the headers for ESP8266WiFi and ESP8266WebServer, I was able to get it to compile.

I changed the handleRoot function to:
Code: Select allvoid handleRoot() {
  String htmlDoc = {   "<!DOCTYPE html>"
                       "<html>"
                       "<head>"
                             "<title>Clock Settings</title>"
                       "</head>"
                       "<body>"
                             "<form method=\"post\">"
                                   "<input type=\"submit\" name=\"clk_action\" value=\"RESET\">"
                             "</form>"
                       "</body>"
                       "</html>"};
  webserver.send(200, "text/plain", htmlDoc);
}


I figured I would try to create a simple page, but it didn't actually create the page, it just posted the text. What am I doing wrong?

And I'm not quite clear, how do I read back the data in the form? Is it automatically placed in webserver.args()?
User avatar
By martinayotte
#28530 Yes, the missing str was a typo. For the HTML form, you need to change the "text/plain" to "text/html".
For reading back the form data, yes, the data is automagically parsed and placed in webserver.argName(i) and webserver.arg(i), where the first is the fieldID and the second its value entered by the user, while webserver.args() is the number of fields received. There are some other helpers such webserver.hasArg("fieldname") to verify if this field exist and can be retrieve by it name using webserver.arg("fieldname")
User avatar
By awwende
#28531 I'm that much closer to getting this working. However, when I navigate to /save, it still tells me that webserver.args() = 0. Here's the code that I'm using, it seems like I'm still having the same issue as before, where post http request gets sent and the content length > 0 but it never actually gives the content of the form . It's interesting though that without calling softAP, it still stores the name and password for the AP.

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer webserver(80);

void handleRoot() {
  String htmlDoc = "<!DOCTYPE html>";
  htmlDoc += "<html>";
  htmlDoc += "<head>";
  htmlDoc += "<title>Clock Settings</title>";
  htmlDoc += "</head>";
  htmlDoc += "<body>";
  htmlDoc += "<form method=\"post\">";
  htmlDoc += "<input type=\"submit\" name=\"clk_action\" value=\"RESET\">";
  htmlDoc += "</form>";
  htmlDoc += "</body>";
  htmlDoc += "</html>";
  webserver.send(200, "text/html", htmlDoc);
}

void handleNotFound() {
  webserver.send(404, "text/plain", "Page not found ...");
}

void handleSave() {
  String str = "Settings Saved ...\r\n";
  Serial.println(webserver.args());
  if (webserver.args() > 0 ) {
    for ( uint8_t i = 0; i < webserver.args(); i++ ) {
      str += webserver.argName(i) + " = " + webserver.arg(i) + "\r\n";
      Serial.println("Arg "+ String(i)+"="+ webserver.arg(i));
    }
  }
  webserver.send(200, "text/plain", str.c_str());
}

void setup() {
  Serial.begin(9600);
  delay(10);
  webserver.on("/", handleRoot);
  webserver.on("/save", handleSave);
  webserver.onNotFound(handleNotFound);
  webserver.begin();
  Serial.println("Web server has started");
}

void loop() {
  webserver.handleClient();
}
User avatar
By martinayotte
#28532 Your form is actually an empty form, only RESET button is present, so it is normal that handlerSave don't have any thing to print. Also, you need to provide action with proper path to the post.

Change the form with some thing like :
Code: Select all  htmlDoc += "<form method=\"post\" action=\"/save\" >";
  htmlDoc += "NTP Server :<br><input name=\"ntpserver\" type=\"text\" size=\"12\" value=\"\" /><br>\r\n";
  htmlDoc += "<input type=\"submit\" name=\"clk_action\" value=\"RESET\">";
  htmlDoc += "</form>";


Then, while saving, you should see :

Code: Select allntpserver = value_entered_in_the_field


About STA remembering the previous connection, it is always like that, even if connection was done with another firmware in the past, except if you upload a blank.bin at 0x7E000 to erase them.