-->
Page 1 of 1

Webserver: serving different content to AP users & wifi user

PostPosted: Wed Oct 10, 2018 8:59 am
by ErikLem
I have some code on my ESP to connect it to a wifi network as a client. At the same time I also start a softAP:

WiFi.softAP("ESP AP" );

What I would like to do is serve different content with the webserver, depending on if someone gets there through the IP on the wifi network it's connected to, of if they get there while they are connected to the AP.

The softAP gets IP 192.168.4.1 by default, and seems to give 192.168.4.2 to the first connected client.
I could do a check like this:

Code: Select all  String addy = server.client().remoteIP().toString();
 
 if (addy == "192.168.4.2"){
      server.send(200, "text/html", "you're connected to the AP");
  } else {
    server.send(200, "text/html", "You're on the same network as the ESP");
  }


but that seems a bit unreliable, since it's possible (even though the chance is low) that someone has 192.168.4.2 wheil connected to his wifi network. It also will not work when a second client is connected to the AP (although in reality only one client should be connected in my use case)

I was wondering if there's a better way to determine if the user is connected to the AP or the the wifi network that the ESP is on.

Re: Webserver: serving different content to AP users & wifi

PostPosted: Wed Oct 10, 2018 10:09 am
by martinayotte
ErikLem wrote:but that seems a bit unreliable, since it's possible (even though the chance is low) that someone has 192.168.4.2 wheil connected to his wifi network. It also will not work when a second client is connected to the AP


You could simply check the first portion of the string "192.168.4" instead of the entire one.
You can also throw away the .toString() and keep it in IPAddress object which then can be check like :
Code: Select allif (ip(0) == 192 && ip(1) == 168 && ip(2) == 4) { // the code }

Re: Webserver: serving different content to AP users & wifi

PostPosted: Wed Oct 10, 2018 11:07 am
by Pablo2048
You can also use a better way - use ESPAsyncWeb server, and follow https://github.com/me-no-dev/ESPAsyncWe ... in-ap-mode - simple, fast, no ip math mambo-jambo :-)

Re: Webserver: serving different content to AP users & wifi

PostPosted: Mon Oct 15, 2018 7:59 am
by ErikLem
Pablo2048 wrote:You can also use a better way - use ESPAsyncWeb server, and follow https://github.com/me-no-dev/ESPAsyncWe ... in-ap-mode - simple, fast, no ip math mambo-jambo :-)


That sounds like a much better idea, I'm going to have a look at it.
Only thing that might complicate things is that I'm combining it with the wifimanager library (https://github.com/tzapu/WiFiManager) which uses a different webserver library, but I see someone already did a attempt to rebuild it to the asyncwebserver: https://github.com/alanswx/ESPAsyncWiFiManager

going to have a look at that to see it it's any good.