AP mode, webserver redirect?

Posted on
  • Hi,

    I'm just getting started on espurino and the esp8266 and have a few questions.

    I've successfully got espurino set and running on an esp8266. I would like to have it set as an access point so that when someone connects to it, they are automatically redirected to page that is served by the esp8266 using the http server.

    is this possible? I can connect to it, but can't figure out how to then use the webserver functionality along with it.

    Any help would be greatly received.


  • I'm not sure if it can be both an access point and a Web server at the same time... If it can, just create a Web server as many examples show: a Web server on a connection connected to a Wifi network with the SSID (and password) for which you just created the access point. About the ip addresses or url to ip address resolution with that setup, I have no experience (yet).

  • I've got it set up in ap+station mode for now.

    I can set up the webserver so that when I browse to the ip at the correct port I get 'hello world'.

    but I connect to the ap and the then go to http://localhost:80 or http://:80 I get nothing.

    I really want it so that whatever address i put in once connected gets redirected to my 'hello world' page

    does that make sense

  • With what do you

    connect to the ap and then go to localhost:80 or http://:80


    localhost (or is (with defaults) always the host on which the http request is made on.

    If you do it with a browser, localhost is the machine where your browser runs on. If it is a different ESP8266 then your access point, this different_ESP8266_then_your_access_point­ is the local host.

    If it is the ESP8266 that is your access point on which you try to create a connection to localhost, I don't know what that yields... if any thing at all, because you have now three things going on: the access point that maintains the Wifi network with given SSID (and DHCP service), a Web server, AND a client of the Web server - the client that wants to connect to the Wifi, and send an http(s) request and expects a response.

    I assume, only access point and Web server run on the ESP8266, and the page request is made from a device other than the access point device, such as from a mobile or desktop browser.

    In this case, your Web server gets (from DHCP service) - or sets - an IP address, and you have to connect to that IP address in the http request... You can give the Web server (connection) a fixed IP address (when not using DHCP) or a host name and use either one of those in the http request.

  • @Ruprect99, your issue is the host/IP addressing, when ESP8266 is operating as Access Point and you are connected to it, the IP you need is

  • Ok. I can see localhost not working.

    I simply want to connect to the esp8266 as an access point using my phone. I then want to be able to go to a page or site hosted on the esp8266 by typing the correct address into the address bar. What should this address be?

    Ideally i would like to be able to type any address and have the esp8266 redirect that reqest to same basic page....like when you log into a secure wifi hotspot and it redirects to the login page. So a user connects to the esp8266, types http://www.google.com, but gets redirected to 'hello world'. I saw on hackaday some did a wifi throwy project that did this, but this was using nodemcu and lua. I wondered if it were possible on espurino.

    Thanks for the help so far.

  • @Ollie, you replied as i was typing!

    Ok, that rings a bell about connecting as that ip address, now how about the other request. It redirecting all other address requests to the same page hosted on the device?

    Thanks again guys.

  • I can't help with that. You'd need some sort of local DNS or hosts file equivalent to make that happen, actually residing on the ESP8266. I don't "think" we have anything like this.

  • Hi, i feared that may be the case.

    This the article and it does mention a dns http://hackaday.com/2015/05/03/esp8266-w­ifi-throwies/

    So is there no dns capabilities in espurino at the moment?

  • I wonder if any of the node.js dns server packages would work? Is there an easy way to tell if they would run on the esp8266?

  • Don't know TBH. I use an entry in hosts to avoid keying the IP, and I'm also aware that we have an ability to set a hostname (http://www.espruino.com/Reference#l_Wifi­_setDHCPHostname) for the board when it is connected as a station to another Wifi network, but I think what you want there is a bridge too far. Two people that would know are @tve and @Gordon - maybe they can jump in.

  • What you want is ignoring host name in an http request, in other words, catch any host names (and even ip addresse) to control in- and out-flow... and that happens on the networking level.

    Any http request with a host name goes first through a name resolution requst-response phase which returns an ip address to the requestor. The name resolution request is made towards naming server address and includes only the hostname as payload. Upon return of valid ip address as successful result of hostname resolution, the requestor (your phone) makes the actual request with the application payloads. If you lookup the host name's ip address upfront and use it in the request, then no hostname resolution is required and the request with payload is made right away to the given ip address.

    Intercepting any request and control its 'routing' is what you are loking for. I'm pretty sure that is natively implemented in the network software stack that comes with ESP8266 SDK libraries and is just bound into the Espruino build.

    The Espruino build exposes some value setting and getting and some functions as methotrexate of the Wifi object, but I have seen nothing that would allow to plug or chain in (your own) routing and session modification functions. May be the Lua implementation you have seen may give you some hints where to apply the crowbar to make it happen.

  • Hi, i'll take a look at the lua build as you suggest to see whats going on....if i can figure it out that is!

    Thanks for the help so far

  • Hi,

    I did some googling last night. What I'm looking for is called a 'captive portal' it turns out.

    I found this post about it http://www.esp8266.com/viewtopic.php?p=2­3857 but its using native code which I don't understand (yet);

    And I found this https://github.com/espruino/Espruino/iss­ues/590 which mentions dns redirect as being supported. So I'm wondering if I use the code in this post but use the * wildcard for the host lookup it might work?


  • This looks interesting


    It looks like its written in C so it might be possible to wrap it up into a module for use in espurino??

  • This fires up a web server and attempts to listen to DNS requests on port 53:

    var counter = 1;
    var wifi=require("wifi");
    function getPage(req,res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<html><head><meta http-equiv="refresh" content="5"></head><body><img src="favicon.ico"/>'+(counter++)+'</body­></html>');
    var net=require('net');
    function callbackdata(data) {
  • You can see web requests, connecting to the SSID ESP and with a browser on

    if you then 'telnet 53', we can attempt to trigger the callback, however it's not called.

    If the port is changed, to say 55 - the callback works.

    This means that in AP mode the ESP is already listening on port 53, so it would require changes to the firmware to allow the captive type mode.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

AP mode, webserver redirect?

Posted by Avatar for Ruprect99 @Ruprect99