Saturday, 24 October 2015

Using the ESP8266 as a WiFi range extender

This blog article refers to my YouTube Video you find here.
It shows how an ESP8266 module (like this one) can be used as a (rather unreliable) WiFi range extender.

Overview of the setup

Beside an ESP8266 module, you need to:

  • download the modified firmeware here and flash it to your module 
  • copy the DNS forwarder script from here to your module
  • add a route to the 192.168.4.0 subnet to your default gateway (i.e. usually  your WiFi dsl/cable router)
  • attach the module to your WiFi network and run it in StationAP mode:
 wifi.setmode(wifi.STATIONAP)  
 wifi.sta.config("YOURSSID","YOURWIFIPASSWD")  
 tmr.alarm(0, 500, 1, function()  
     if wifi.sta.getip()==nil then  
      print("Connecting to AP...")  
     else  
      tmr.stop(1)  
      tmr.stop(0)  
      print("Connected as: " .. wifi.sta.getip())  
     end  
   end)  
 cfg={}  
    cfg.ssid="intarwebs"  
    wifi.ap.config(cfg)  

  • connect another device to the "intarwebs" SSID. If you want to really use that, you should encrypt that side as well. Here is how.
The disconnects from the Wifi Router appear to be related to  big packets and/or heavy traffic. It has been suggested that it might be a power issue, but neither with the scope, nor with the Fluke 87V's min/max function I was able to detected a significant voltage drop.
I have captured the traffic with wireshark, but could not make sense of it yet. My best guess at the moment is that with a big TCP windows size, the module runs into a buffer size problem or that it is busy for too long on the "left" side. The module only has one radio to serve both connections, so unlike your typical router, it can't forward a continuous stream of packets.



48 comments:

  1. Great work! But what is "modified firmware" actually? Yeah, we can just install it to the nodemcu but I want to know more. Can you share changes or source code?

    ReplyDelete
    Replies
    1. The modifications are scattered around my blog, sorry about that. The relevant changes to make this work are:
      In /app/include/lwipopts.h add a #define IP_FORWARD 1
      and
      In /app/include/lwip/app/dhcpserver.h add #define USE_DNS somewhere (around line 54)
      These two should be sufficient to make it work.

      Delete
    2. Could you the content of this post without modifying the firmware?

      Delete
    3. Michael, I've thought about that for a while: If we need to stick to the original firmware, we'd need a http-proxy implementation on the ESP8266. While this is certainly possible, I haven't seen an implementation yet.
      We can only run one TCP server on the module, so support for automatic proxy detection is an additional (if optional) challenge.
      So at the moment the firmware approach seems like the easiest solution.

      Delete
  2. Please, fix URL for "DNS forwarder script"
    You've missed "h".

    ReplyDelete
  3. Thanks for this post. One question have you tried to implement this with the Arduino IDE for the ESP8266?

    ReplyDelete
  4. No, only with NodeMCU. If you try it with the Arduino IDE, I'd be most interested in the results. There have been hints in the NodeMCU issues that the disconnect-problem might be fixed in the lastest -dev version. I'll try that some time.

    ReplyDelete
  5. Hi, can you share compiled liblwip.a with IP_FORWARD enabled?
    Thank you

    ReplyDelete
    Replies
    1. You find the compiled NodeMCU formware here: https://www.dropbox.com/s/fyblkervtz02q97/NodeMCU095-ARe-redu-fw-dns.bin?dl=0
      The changes are just:
      In /app/include/lwipopts.h add a #define IP_FORWARD 1
      and
      In /app/include/lwip/app/dhcpserver.h add #define USE_DNS somewhere (around line 54)

      Delete
  6. Hi ! I dont know what you wanted to achieve, but i did a similar repeater focused on one task : when the repeater (running your classic dns-liar dns server, using my speed improvements) is asked for a specific kind of urls (starting with folderxyy/ ) then it tries to hit the same url on the AP he is connected to.

    A first ESP (located too far from my wifi AP to connect directly with it) can signal that the mailman opened my mailbox by hitting a link on the repeater, which hits the same link on the hotspot. The repeater ESP is put into a recycled phone charger and permanently powered. The ESP in my mailbox is only powered when the mailbox is opened.

    This is a very minimalist repeater, but probably enough for many uses. I'll post sources later on...

    ReplyDelete
  7. Andreas, here is a working repeater - very limited compared to your version, but good enough if you just want to transmit a simple information (like hittinh a PHP page)

    http://iotests.blogspot.fr/2015/11/extend-range-of-esp8266-as-wide-as-you.html

    ReplyDelete
    Replies
    1. Good job! I've also thought about a similar solution, but could not get it done because of the "only one TCP server" limitation of the firmware. I'll see if I can work my way around that somehow to make a proper HTTP-proxy chain.

      Delete
  8. Thank you so much for the "IP_FORWARD = 1" idea!

    By the way, you don't need a DNS forwarder. Just edit app/lwip/app/dhcpserver.c to modify this:

    148 #ifdef USE_DNS
    149 *optptr++ = DHCP_OPTION_DNS_SERVER;
    150 *optptr++ = 4;
    151 *optptr++ = ip4_addr1( &ipadd);
    152 *optptr++ = ip4_addr2( &ipadd);
    153 *optptr++ = ip4_addr3( &ipadd);
    154 *optptr++ = ip4_addr4( &ipadd);
    155 #endif

    to look like this:

    148 #ifdef USE_DNS
    149 *optptr++ = DHCP_OPTION_DNS_SERVER;
    150 *optptr++ = 4;
    151 *optptr++ = 8;
    152 *optptr++ = 8;
    153 *optptr++ = 8;
    154 *optptr++ = 8;
    155 #endif

    (or instead of 8.8.8.8 put IP address of your fritzbox).

    That's it!

    ReplyDelete
    Replies
    1. Yes, you're quite right. That makes things even simpler. - Thanks!

      Delete
  9. Hi
    Suppose I setup a wifi hotspot on my windows pc and I provide the hotspot credentials to the ESP. How do I add the routing step?
    "add a route to the 192.168.4.0 subnet to your default gateway (i.e. usually your WiFi dsl/cable router)"

    Can you please list the commands?will this be helpful http://www.howtogeek.com/howto/windows/adding-a-tcpip-route-to-the-windows-routing-table/
    Thanks

    ReplyDelete
    Replies
    1. First you need to get the local lan address of the ESP. You'll see that on the ESP's serial console. I'll call that w.x.y.z
      If your stationary PC is a windows machine, you need to open a commend window (run cmd.exe). In that command window, type:
      "route add 192.168.4.0 mask 255.255.255.0 w.x.y.z"
      you should then be able to send pings between the device connected to the ESP and the stationary PC.
      The more universal option is to set the route on the router. But that depends on the make/model of your particular router.
      Perhaps the Video will make that clear: https://youtu.be/NH1rjk3Q_MU

      Delete
  10. how do you add a route to the 192.168.4.0 subnet to your default gateway (i.e. usually your WiFi dsl/cable router)?

    ReplyDelete
    Replies
    1. That depends on your router. I'd download the manual as a pdf and search it for "static route". If all else fails, you might have to insert an OpenWRT router. But I think practically all DSL/CABLE routers should have an option for that. (Probably hidden somewhere in the "advanced settings" menue)

      Delete
  11. Which esp8266 module u have used? Is it ESP8266 esp-1??

    ReplyDelete
    Replies
    1. I used this development board:
      http://www.banggood.com/V3-NodeMcu-Lua-WIFI-Development-Board-p-992733.html?p=NT1005763073201409DA
      It has an ESP-12e on it. But that is pretty much irrelevant. An ESP-01 will work ok, too. Flash RAM size is not really an issue here.

      Delete
  12. any thoughts on paired back to back ESP's as range extender / repeater

    one configured as an AP the other as gateway / router station

    a serial channel connection between them is maxed out at ~900 Kbaud

    is it possible to use more than one wired channel connect to match WiFi bandwidth?

    ReplyDelete
    Replies
    1. this is the exact idea I have while reading the 8266 specs - putting 2 devices back to back each on their own wifi channel. Did you managed to do some real testing yet?

      Delete
    2. Martin has greatly improved the code and added some great features, including a capture function.
      https://github.com/martin-ger/esp_wifi_repeater
      I recommend his solution over my approach.

      Delete
    3. Thanks for another nice project link.

      This one does not work with 2 devices too. It's just using the same wifi channel for both network, literally disabling the network usage for stations located between the base access point and this range "extender".

      Delete
  13. Leider hängt sich mein esp nach dem zugriff auf websiten über einen client auf. der esp ist dann auch nicht mehr an ping bar, nach einem reset geht dann wieder genau ein aufruf

    ReplyDelete
  14. mein esp hängt sich auf nach dem ein client versucht eine website aufzurufen. ein ping auf den esp ist ab dem moment nicht mehr möglich

    ReplyDelete
    Replies
    1. Wie im Text beschrieben, kam das bei mit auch immer wieder vor. Andererseits schaffen es andere Leute ganze Videos darüber abzuspielen. Über die Ursachen kann ich nur spekulieren. Es ist aber einen Versuch wert, die Firmware mit einen neueren NodeMCU Version nochmals zu bauen. Die hier verwendete Version hat ja schon bald ein Jahr auf dem Buckel.

      Delete
  15. Hi,

    thanks for the post. I've just tried it on a Wemos D1 mini with the latest NodeMCU firmware (modified according to your blog and Aex Aey's comment - thanks).
    And it just works without noticable drops.
    Anyway this was my first try with ESP8266 and it looks cool :)

    ReplyDelete
    Replies
    1. Great to hear it works for you. People will probably be interested in your firmware, as I haven't updated my build environment for quite a while.

      Delete
  16. Im new to this area, and im studying lua and dont have great knowledge on lua. when i run code gives the error: "ap.lua:11: wrong arg type"
    line 11 is: "wifi.ap.setmac("AC-1B-1C-D1-0B-22")"
    it seems like firmware 0.9 doesn't support set mac function.
    can u please give changed firmware 1.0 which supports set mac function or give a solution for this

    ReplyDelete
    Replies
    1. I think you're quite right: My firmware version is too old and some commends have changed or have been added since 0.95. Have a look at the comment above from "klaca", he has made a new firmware build. Perhaps he is willing to share it.

      Delete
  17. Ok, anyone can try it with the newer firmware:

    http://tiny.cc/modemcu_wifi_extender_fw

    ReplyDelete
  18. Hello, i try with newer firmware and all work ok, in my router put ip destination 192.168.4.0 ( esp8266 mode ap ), gateway put ip station esp8266 192.168.1.33, and submask 255.255.255.240, but if want to connect many esp8266 to my router...i have to put the gateway different for each esp8266, anybody know can put only a gateway for many esp8266? more simple, thanks

    ReplyDelete
    Replies
    1. You have to use differnt subnets behind each of the ESPs and route each network to the respective ESP. In newer firmwares, you can set the SoftAP IP range: https://nodemcu.readthedocs.io/en/master/en/modules/wifi/#wifiapsetip

      Delete
  19. do you have some new stable version?

    ReplyDelete
    Replies
    1. Try [klaca]'s firmware build. He posted a link here in the comments back in September.

      Delete
  20. Hello again, thanks for your reply Andreas Reischle, routing in my router all work good, but now i have other problem. Esp8266 only can connect in AP 4 stations, but i need about 10 stations per AP, i will need to build custom firmware but i dont know if it will work this modify. And only see https://nodemcu-build.com/ and i cant modify any lines, it is automatic, thanks.

    ReplyDelete
  21. can you explain how you made setup at router means connection.

    ReplyDelete
  22. what i am doing wrong that I can't ping www.google.com ...

    ReplyDelete
  23. Does "nslookup www.google.com" work?

    ReplyDelete
  24. Great tutorial.
    I am a university student and live in hostel, having wifi but its range is not reach in my room. I figured out that the wifi had 192.168.16.1 and 192.168.12.1 as default gateways. I am unable to access the router page, so can I still have this repeater working. How can I change the 192.168.4.0 thing? and to what ip?

    ReplyDelete
    Replies
    1. User Martin-ger on Gitgub ( https://github.com/martin-ger/esp_wifi_repeater ) has done a much better job than me. His solution includes NAT which should make things easy for you.

      Delete
    2. This comment has been removed by the author.

      Delete
    3. His binary doesn't work for me either.

      Delete
  25. The version 2.0 failed for my nodemcu 3 device. Now I use firmware 1.5.4 and it works now

    ReplyDelete