ESP32, SPIFFS, SoftAP....How to determine which webpage is being viewed?

Okay...hoping someone can come up with a programming approach here for me. Been spinning my wheels a bit searching the internet for a solution with no luck.

Running an ESP32 on SoftAp wifi. It is running as a server. In the ESP32 SPIFFS, I have a few web pages stored.

The ESP32 is externally powered, no serial connection. Only Wifi.

The way the system works is someone gets on a browser, and logs into the ESP32 server. This opens a webpage, call it main.html.

The sketch then runs and populates data on the page. And this just loops. No issues here, this is all done and working fine.

But...what I want to do is have multiple html pages; say main.html, main2.html, etc. And I want to run only the portion of the sketch needed to populate the ACTIVE page.

Currently, the entire sketch runs, meaning some of the information being passed is not used, ultimely wasting capacity and bandwidth.

If i can figure out which webpage is being used, then I could run the sketch only for the information required. Ultimately allowing me to use more pages, as I would not overload the system with unused information.

Hopefully that makes sense. Again, I need a way to figure out which webpage is actually in use.

I have tried httpcode. And that works fine...but it really just tells me something is connected and working. Not which page.

I also pulled a http.get() for each page. But since the pages are on the server (ESP32), it literally pulls back the html code for the page, in use or not.

As I have kicked this around, since the ESP32 is the server, the page being used will come from the client(user) to the server. It seems like a just need to be able to see what page was last requested at the server? Maybe?

Anyone have any other thoughts on this. I appreciate it!

I'm slightly lost TBH...

Are you wanting to know what pages that active users are viewing?
Maybe something like a login / session, etc?
Maybe using JS to report back active viewing?

Or, just a web page which uses JS to populate the page?

Yeah, I figured. :smirk:

I need to know WHICH page is active.

When they get on the server, they could be on a few different pages. I just want to know which of the pages they are on so I populate the correct data to the page.

Login...hmmm? The login wouldn't work here...but...maybe just clicking an "Activate" button or something on the page would work? A little inelegant, but it would work if its the only option.

If they are on a page then they must have already requested it!

Is this a multi user environment?

Login, not necessarily, maybe just pass a session id around... (either hard coded into links or as a js var and pass it)

Nope just one user at a time.

Appreciate you responding. Thinking it through I think the answer was in front of my face the whole time. Duh! I will work up some code tonight and see if i can get it going. I'll follow-up...

Then the page the one and only user is viewing is the most recent one that your code served to him. Just keep track of that.

Okay still fighting this a bit. I think it is so basic I can't find any good examples. Reminder: ESP32, SoftAP

I figured out what I need to do. Really simple...

When someone clicks on a hyperlink for a different page, e.g. "main2.html". I need the client to send a message to the server/arduino.

And then I just need the arduino to recieve the client message...and make the necessary changes.

Simple. But I cannot find a good example of this. Anyone know of one? Thanks.

I got this figured out. Posting for someone in the future. It was super simple.

When a client request comes in I just added a variable to track which page was requested, using "pageload" in the example...

void setup(){

server.on("/main.html", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/main.html", "text/html");
  pageload = 1;
  });


server.on("/main2.html", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/main2.html", "text/html");
  pageload = 2;
  });
}

I then used "pageload" in the loop to call the appropriate function...

void loop(){


if (pageload == 1){
  main();
}
else if (pageload == 2){
  main2();
}
else{
  delay (5000);
}

}

Post #6

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.