Search code examples
arduinoesp8266arduino-esp8266

Mobile phone's browser cannot load website hosted by ESP8266 when mobile network is connected


I'm using an ESP8266 web server. I'm creating a Wifi AP and I use a web server to host a website that I want to access from my mobile phone's browser:

void wifi::access_point::begin() {
  WiFi.mode(WIFI_AP_STA);
  WiFi.softAPConfig(ap_ip, ap_gateway, ap_subnet);
  WiFi.softAP(ap_ssid, ap_password);
  server.on ( "/", [this]() {
    handle_root();
  });
  server.on( "/submit", [this]() {
    handle_submit();
  });
  server.begin();
  dns_server.start(dns_port, "my_wifi_config.com", ap_ip);
}

void wifi::access_point::handle_root() {
  char html[1000];
  snprintf (html, 1000,
        "<html>\
        <head>\
        <title>Wifi Configuration</title>\
        <style>\
        body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; font-size: 1.5em; Color: #000000; }\
        h1 { Color: #AA0000; }\
        </style>\
        </head>\
        <body>\
        <center>\
        <h1>Wifi Configuration</h1>\
        <form action='/submit' method='POST'>\
        <p> Wifi SSID: </p>\
        <input type='text' name='ssid'>\
        <p> Wifi password: <\p>\
        <input type='text' name='password'>\
        <br>\
        <input type='submit' name='Submit'>\
        </form>\
        </center>\
        </body>\
        </html>"
       );
  server.send(200, "text/html", html);
}

From my desktop I can connect to the AP and access the ap_ip and everything works fine. In my phone I can also connect to the WiFi AP, but if I'm connected to the mobile network the browser doesn't load the website, it says "web page not found". If it turn it off, the website loads fine.

I would need to be able to load the website hosted by my ESP8266 web server without having to turn off my mobile data. Any idea why this is happening and how could I avoid this problem?


Solution

  • Your ESP creates its own little LAN, and you can access it when you're part of this LAN. But when you're connected to mobile network you are part of global internet which doesn't know anything about your ESP's LAN. So, you need to connect them somehow.

    For example - you have your home internet provider and, let's say, you have a wireless router D-Link. It means that you can make your home LAN visible to the internet via D-Link Dynamic DNS service. So, you can run your ESP as a client of the home LAN (not as AP) and you can map ESP's local IP to some globally available address.

    Yes, you will loose nice domain 'my_wifi_config.com', but you will be able to access ESP's functionality, whatever it is (webpage with thermotemeter values or a button to turn on the lights).