0

Now there are many ways to reply with a webpage content to a user who is accessing the web server, But Keeping in mind to use as less space as possible, I was trying something new, Since i didnt got compilation error, I am a little confused as to what might be the error,

in void data() I am storing the html page and saving it in ROM,

void data()
{
  if(SPIFFS.exists("/login.html"))
  {
    Serial.println();
  }
  else{
    SPIFFS.begin();
    File f=SPIFFS.open("/login.html","w");
    String s="<html><head><title>AP</title><h1>Welcome to AP page</h1>\n";
    s+="</head><body><form name=\"/myform\" action=\"/login\" method=\"POST\">\n";
    s+="<input type=\"text\" name=\"username\" placeholder=\"username\"><br/>\n";
    s+="<input type=\"password\" name=\"password\" placeholder=\"password\"><br/>\n";
    s+="<input type=\"submit\" value=\"login\"></form></body></html>";
    f.println(s);
    f.close();
  }

}

Please correct the above lines if I wrote it wrong, After that, I wrote some lines in void setup()

WiFi.mode(WIFI_AP_STA);//Only Access point
  WiFi.softAP(ssid, password);  //Start HOTspot removing password will disable security

  IPAddress myIP = WiFi.softAPIP(); //Get IP address
  Serial.print("HotSpt IP:");
  Serial.println(myIP);

  server.on("/", HTTP_GET, handleRoot);      //Which routine to handle at root location
  server.on("/",HTTP_POST,handlelogin);
  server.onNotFound(handleerror);

  server.begin();   

Now I am calling void handleRoot() I want to send the page that I wrote and saved in the /login.html file.

so for that,

void handleRoot()
  {
    SPIFFS.begin();
    File f=SPIFFS.open("/login.html","r");
    server.send(200, "text/html",f.readStringUntil('EOF'));
  }

Now once I do this and execute and load the program in ESP01, it shows me nothing, I have also tried sending (String)f, anything else I write is giving me an error, This is compiling fine, but when I connect to the AP and open 192.168.4.1 in the browser i am not seeing anything, But when I wrote server.send(200,"text/html",(String)f) I was getting 0, Any idea on what mistake I am making.

  • What do you hope to achieve by having one bit of your program save a file of static data to SPIFFS and then another bit read that file and send it? – Majenko Jul 18 '18 at 14:45
  • Unless I format the device, I can remove the bit where I add those lines to save the HTML file. Like remove the block of that code later. – Wan Street Jul 18 '18 at 14:55
  • Why not use the IDE's SPIFFS tools to create the SPIFFS image with the file already on it? – Majenko Jul 18 '18 at 14:55
  • 1
    I suspect it's the 'EOF' that you're reading until. That's looking for the letter E not the end of the file. – Majenko Jul 18 '18 at 14:57
  • 1
    You should use server.streamFile("text/html", f); – Majenko Jul 18 '18 at 14:58
0

Small update, I used the same code for ESP8266 NodeMCU.

Sorry If I am posting this with wrong device number. But I tried that same code and uploaded it to ESP8266, it worked, and I am getting the AP webpage, that I am storing and sending the file. I will post it as an answer as I need to post an image.

The is the page that I get when using the HTML that I wrote:

Webpage

So, this happened. I am confused as to why it didn't work with ESP01, plus I have to load the code every time I plug in the ESP01.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.