WiFi Extender (NAT-Router) mit ESP8266 und ESP32

WiFi Extender with ESP8266 and ESP32

They stand out in the front yard, building something, desperately need a tutorial, because they do not know any more and want to look it up on the Internet. Just find out quickly with the search engine of your trust. It is precisely in this place that the mobile data connection at the lowest level and the local WLAN are of course out of reach. You know that. I then came up with the ESPs that can be used as an access point. The problem and the solution are not necessarily new. I would like to share my experience with you here. Here we go.

What we need

1

ESP32 or ESP8266 Development board from the AZ-Shop

1

PC with Internet connection

1

USB

 

Moritz Spranger had in his contributionESP8266 WLAN mesh over 4 nodes"A few years ago, reports of such a similar project. For this solution, the ESP is flashed with a firmware via an external tool. I will show the process even more precisely later.

I assume at this point that you have installed the Arduino IDE, as well as the sources for ESP8266 and ESP32. If not, look in the AZ blog. There it is described. I recommend updating the libraries at this point.

ESP8266

I had first started to browse the example sketches in the Arduino IDE (it must be set in the Arduino IDE under tools the board used). ESP8266WiFi the program RangeExtender-NAPTThere you only have to enter the access data to your WLAN network:

 '35; define STASSID "mynetwork"
 \35; defines STAPSK "mynetworkpassword"
Then download the sketch onto the microcontroller. In the overview of your WLAN networks, the ESP should now appear with the name of your home network and the "extender" on the back.

[IHRWLANNAME]extender

To connect to it, you have to enter the same WLAN key as for your own network. I then had to make some attempts in the browser until the Internet connection worked. But then it did. Actually quite simple.

Connection via a Captive Portal

It's a bit tricky when you have to enter the access data into the source code. This is too inflexible for me personally. So I thought about setting up a web server that displays an input form in the Internet browser. I would like to enter the access data about this. Since there is nothing on the Internet that does not already exist, I have this solution here This sketch can be uploaded directly to the ESP8266 without changing the source code. Next, you will see in the overview of your WLAN networks a device called ESP followed by a line chenchain. Connect with it (without password) and call the IP in the browser 192.168.4.1 On (this data can also be seen in the serial monitor of the Arduino IDE). You will then be shown an input form in which you can enter the access data to your WLAN network. You can also change the name of the ESP network and set a password (which is strongly recommended) I also use the same password as for my home network. If the data is entered, contact the ESP (if you have redesigned the name and password) and you should already have a connection to the Internet.

It goes even more comfortable

As you can see, you must enter the name of your network in the input form. Unfortunately, these are usually formulated by the manufacturer for some time. So I would like to change the sketch so that I can see the available networks in a list. That's where I want to choose my network instead of typing in the name. For this purpose, I have adapted the source code mentioned above and changed the HTML form so that the WLAN networks are listed nearby and equipped with a radio check box. This way you can simply select, enter your password and connect. Here the source code:

 If LWIP FEATURES LWIP IPV6
 
  '35; define HAVE'u NETDUMP 0
 
  '35; includes
  '35; include
  '35; include
  '35; include
  '35; include
 
  '35; define NAPT 1000
  '35; define NAPT'u PORT 10
 
  If you have NETDUMP
 
  '35; include
 
  void dump(int netif idx, const char* date, size t lon, int out, int success) {
  (void)success;
    Serial.print(out ? F("out") : F("in the));
    Serial.printf("%d ", netif idx);
 
    // optional filter example: if (netDump is ARP(data))
  {
      netDump(Serial, date, lon);
      //netDumpHex(Serial, data, len);
  }
  }
  #endif
 
  My Functions
  bool testwifi() {
    Serial.printf("\nTesting connection with'%s'\n", WiFi.SSID().c u str());
    int count = 0;
    digitalWrite(2,LOW);
    while (count < 20) {
      if (WiFi.status() == WL CONNECTED) {
        Serial.printf("\nWiFi Connected! \nSTA:%s (dns:%s /%s)\n\n",
                      WiFi.localIP().TOString().c u str(),
                      WiFi.dnsIP(0).TOString().c u str(),
                      WiFi.dnsIP(1).TOString().c u str());
 
        // give DNS servers to AP page
        dhcpSoftAP.dhcps set dns(0, WiFi.dnsIP(0));
        dhcpSoftAP.dhcps set dns(1, WiFi.dnsIP(1));
        digitalWrite(2,HIGH);
        return true;
    }
      Serial.print(".");
      delay(1000);
      count++;
  }
    Serial.printf("\nCan't connect to WiFi, connect to AP'%s'and configure...\n\n", WiFi.softAPSSID());
    return false;
  }
 
  // SERVER
  ESP8266WebServer server(80);
  String content;
  String networks;[40];
 
  void server() {
    server.start();
    int n = WiFi.scanNetworks();
 
    content = "";
    content += "ESP8266 Configuration Page";
    content += "";  
 
    if (WiFi.status() != WL CONNECTED) {
      content += "
currently not connected
"
;

  }
    ed {
      content += "
connected to:;
      content += WiFi.SSID();
      content += "IP" IP:;
      content += WiFi.localIP().TOString();
      content += "
"
;

  }
 
    content += "
";
   
    if (n == 0) {
      content += "

No wireless networks found

"
;

  }
     
    ed {
      content += "

Wireless Station Settings

"
;

      content += "
";
      for (int i = 0; i < n; ++i) {
        networks;[i] = WiFi.SSID(i);
        content += "
";
        content += ";
        content += String(i);
        content += "\" ";
        content += "name=\"SSIDs\"value="";
        content += String(i);
        content += "\">";
        content += "
        content += String(i);
        content += "\"";
        content += ">";
        content += String(i + 1);
        content += ": ";
        content += WiFi.SSID(i);
        content += " (";
        content += String(WiFi.SSR(i));
        content += ")";
        content += (WiFi.encryption(i) == ENC TYPE NONE) ? " " : "*";
        content += "";
        content += "
"
;

        delay(10);
    }
      content += "

Must be at least 8 characters or blank!

"
;

      content += "";
      content += "";  
      content += "

Wireless Access Point Settings

"
;

      content += "
";
      content += "

"
;

      content += "

Must be at least 8 characters or blank!
"
;

      content += "";
      content += "";

  }
 
    content += "

Miscellaneous

"
;

    content += "
"
;

    content += "
";
    server.on("/", []() {
      server.send(200, "text/html", content);
  });
 
    server.onNotFound([]() {
      server.send(404, "text/plain", How the heck did you get here?);
  });
 
    server.on("/stasettings", []() {
      String temp = server.angry(SSIDs);
      int number = temp.TOInt();
      String stasis = networks;[number];
      String staple = server.angry("stapish");
      if (stasis.length() > 0) {
        server.send(200, "text/plain", Settings Recieved.);
        Serial.printf("\n\nAttempting to connect to'%s'using password'%s' \n", stasis.c u str(), staple.c u str());
        WiFi.persistent(true);
        WiFi.start(stasis, staple);
        testwifi();
    }
  });
 
    server.on("/tempstasetting", []() {
      String temp = server.angry(SSIDs);
      int number = temp.TOInt();
      String stasis = networks;[number];
      String staple = server.angry("stapish");
      if (stasis.length() > 0) {
        server.send(200, "text/plain", Settings Recieved.);
        Serial.printf("\n\nAttempting to connect to'%s'using password'%s' \n", stasis.c u str(), staple.c u str());
        WiFi.persistent(false);
        WiFi.start(stasis, staple);
        testwifi();
    }
  });
 
    server.on("/apsettings", []() {
      String apside = server.angry("apssid");
      String Appsk = server.angry("Appsk");
      if (apside.length() > 0) {
        server.send(200, "text/plain", Settings Recieved.);
        Serial.printf("\n\nSetting AP Credentials \nSSID:%s \nPassword:%s \n", apside.c u str(), Appsk.c u str());
        WiFi.persistent(true);
        WiFi.SoftAP(apside, Appsk);
    }
  });
 
    server.on("/tempapsettings", []() {
      String apside = server.angry("apssid");
      String Appsk = server.angry("Appsk");
      if (apside.length() > 0) {
        server.send(200, "text/plain", Settings Recieved.);
        Serial.printf("\n\nSetting Temporary AP Credentials \nSSID:%s \nPassword:%s \n", apside.c u str(), Appsk.c u str());
        WiFi.persistent(false);
        WiFi.SoftAP(apside, Appsk);
    }
  });
 
    server.on("/reboot", []() {
      server.send(200, "text/plain", Rebooting now...);
      delay(5000);
      ESP.reset();
  });
  }
 
  void setup() {
    Serial.start(115200);
    pinMode(2, OUTPUT);
    delay(1000);
    Serial.printf("\n\nNAPT Range extender\n");
    Serial.printf("Heap on start:%d\n", ESP.getFreeHeap());
 
  If you have NETDUMP
    phy capture = dump;
  #endif
 
    WiFi.setPhyMode(WIFI PHY MODE 11N); Set radio type to N
    WiFi.mode(WIFI AP);
    WiFi.persistent(false);
    WiFi.start(); Use stored credentials to connect to network
    testwifi();
    WiFi.softAPConfig(  // Set IP Address, Gateway and Subnet
      IPAddress(192, 168, 4, 1),
      IPAddress(192, 168, 4, 1),
      IPAddress(255, 255, 255, 0));
    WiFi.SoftAP(WiFi.softAPSSID(), WiFi.SOFTWARE()); // Use stored credentials to create AP
 
    Serial.printf("Heap before:%d\n", ESP.getFreeHeap());
    ed uc right = p u tensile init(NAPT, NAPT PORT);
    Serial.printf("ipu napt init(%d,%d): ret=%d (OK=%d)\n", NAPT, NAPT PORT, (int)right, (int)ERR OK);
    if (right == ERR OK) {
      right = lp u tense enable no(SOFTAP IF, 1);
      Serial.printf("ip napt enable no(SOFTAP): ret=%d (OK=%d)\n", (int)right, (int)ERR OK);
      if (right == ERR OK) {
        Serial.printf("\nWiFi Network'%s'with Passowrd'%s' and IP'%s'is now setup\n", WiFi.softAPSSID(), WiFi.SOFTWARE().c u str(), WiFi.softAPIP().TOString().c u str());
    }
  }
    Serial.printf("Heap after napt init:%d\n", ESP.getFreeHeap());
    if (right != ERR OK) {
      Serial.printf(NAPT initialization failed\n);
  }
 
    server();
  }
 
  #else
 
  void setup() {
    Serial.start(115200);
    Serial.printf("\n\nNAPT not supported in this configuration\n");
  }
 
  #endif
 
  void run() {
    server.handleClient();
    if (WiFi.status() != WL CONNECTED) {
      digitalWrite(2, LOW);
      delay(1000);
      digitalWrite(2, HIGH);
      delay(1000);
  }
    ed {
      digitalWrite(2, HIGH);
  }
 }

Intermediate

The fact that I've finally invested enough time up to now is working more badly than right. I measured the speed, and I only got close to one Mbit/s. This seems to be a general problem, as it was also discussed at Github and in the Forum of Expression. Unfortunately, there is no solution.

ESP32 or how everything works better

According to my research, the Espressifs (the manufacturer of the ESP chip) do not intend to use an ESP32 as a WiFi Extender (WiFi NAT router). Accordingly, there is no port of the Extender sample sketch for the ESP32. However, there is the project of martin-ger on Githubwhich has already been widely used. Also in our blog post ESP8266 WLAN mesh over 4 nodes (by Moritz Spranger) this variant is used.

The project is also available for ESP8266. Since I am also looking for a proper solution to this, I am presenting both options here.

Github website for ESP8266

Github website for ESP32


Under Windows:

  • The ESP Flash Download Tool is needed. You can here
  • Click on the Github website (ESP8266 and ESP32) at the top right of the green button "Code" and download everything as ZIP.
  • Start the Flash Download Tool and select the ESP8266 or ESP32.
  • then set everything as on the following screenshot.

ESP8266:

ESP Tool

ESP32:

ESP Tool

  • Connect the ESP to the computer via USB
  • Click the "button" to select the BIN files. The required data can be found in the ZIP downloaded in the "Firmware" (ESP8266) or "build" (ESP32) subdirectory.
  • Enter the storage addresses in the box next to it. In this case "0x00000" and "0x02000" (ESP8266) or "0x1000", "0x10000" and "0x8000" (ESP32). These are the start addresses in the ESP memory where the program is stored.
  • If no COM port is displayed, you need to install the appropriate driver (if you have already programmed the ESP via the Arduino IDE, it should also be recognized here). The COM port number can be a different one for you.
  • For an ESP-01, use "8Mbit" instead of "32Mbit" for FLASH SIZE (ESP8266)
  • Start the Flash process

ESP8266:

ESP Tool

ESP32:

ESP Tool

Note: As in the Arduino IDE, you need to bring the ESP32 into Flash mode by pressing the BOOT key. Hold it down and then press the Start button.

ESP8266:

ESP Tool

ESP32:

ESP Tool

After the successful flash process, briefly disconnect the USB connection to the ESP and then restart it by connecting the plug again (possibly a simple reset is enough).

After a few moments you should see a new WLAN network called "MyAP" (ESP8266) or "ESP32"u NAT"router (ESP32).

Connect to this network and then call the address in the internet browser http://192.168.4.1/ You should see this overview:

ESP8266:

Router Settings ESP8266

ESP32:

Router Settings ESP32

  • AP settings are always the access data for the ESP32. At the beginning there is no password set here. You should change this. I use the same password here (as mentioned above) that is also set in my Internet router.
  • STA-Settings are the access data to your WLAN router.
  • If you are using a PC, you can right-click on the network properties and enter the password via Copy & Paste on the website. You save yourself typing long passwords. You only have to do that once. The ESP then runs autonomously. (The password is then stored in a volatile memory. If this is a security issue for you, you should type in the password manually.)
  • Put the entry for security on WPA2 for the ESP8266!
  • You can also enter a static IP address for the ESP32.

It can not hurt to restart the ESP after entering all settings. In case there is no connection. Remember that you have (probably) assigned a password for the ESP access point. You need to reconnect with it.

Under Linux

On the Github web pages of martin-ger it is also shown how to flash the respective programs under Linux on the ESP:

You need to install esptool.py (more detailed information can be obtained here).

Start the terminal and enter the following command:

 $pip install esptool

 

ESP8266

Enter this command in the terminal:

 $esptoolp --port /dev/ttyUSB0 write flash f 4MB -f 80m -fm dio 0x00000 firmware/0x00000.bin 0x02000 firmware/0x02000.bin"

If you do not use the usual ESP8266 like NodeMCU or D1 Mini, change the following entries in the command line:

  • ESP-01: -fs 1MB
  • esp8285: -fs 1MB and -fm dout

ESP32

Also remember to put the ESP32 in the FLASH mode (BOOT key)!

Enter this command in the terminal:

 $esptoolp --chip esp32 --port /dev/ttyUSB0 \
 --baud 115200 --before default/ u reset --after hard reset write flash \
 -with --flash mode God --flash freq 40m --flash size detect \
 0x1000 build/bootloader/bootloader.bin \
 0x10000 build/esp32 wet router.bin \
 0x8000 build/partitions -u example.bin

If the path is invalid, the esptool may not have been entered globally as a PATH variable, or it may not be in the main folder of the downloaded files. You can also specify the source folder absolutely by specifying the complete path instead of "firmware" or "build".

Under Mac OS

Unfortunately, I was unable to test it. The flash process for an ESP8266 will be among others. here The settings for the esptool should then be the same as under Linux

Conclusion

After I tried some sketches for the Arduino IDE, only the programs of Martins works satisfactorily (thank you here for this work) The step through the ESP Flash Download Tool only seems a little more complicated, but it only costs a few minutes more than out of the Arduino IDE. If I then compare the time I spent for the previous programming in the Arduino IDE, the last version was definitely the faster one.

The Internet connection of the ESP8266 cheated approx. 5Mbit/s, for the ESP32 it was approx. 16 Mbit/s. The latter of course also booted faster. The ESP8266 had occasional disconnections.

As far as the range is concerned, it depends very much on the environment in which you use the ESP as a NAT router. In my case, it went as far as the front gardens, where the Internet router is located in the back of the house. Through thick walls, or over several floors, the range and also the transmission speed are shortened. d then possibly fall heavily. Test this yourself once.

If you have other options, or even working Arduino sketches, I would be happy to share them with us.

How to supply the ESP as a mobile hotspot from a battery with electricity, you can: this blog post taken. That's just a final tip.

Have fun with your (cheap) WiFi Extender.

Andreas Wolter.

for AZ-Delivery Blog

Esp-32Esp-8266Projects for beginners

7 comments

HaJo

HaJo

Soeben habe ich einen ESP32 Devkit C V2 geflasht. Hat alles von Anfang an alles, wie erwartet, funktioniert. Sogar das Einbinden in die Fritzbox war ein Kinderspiel. Vielleicht liegen die Probleme (s. u.) in der Architektur des “D1 Mini NodeMcu mit ESP8266-12F WLAN Modul” sowie der Programmierung dieses µP. Das zu prüfen bin ich noch nicht in der Lage (Anfänger, obwohl pensioniert ;-) ). Aber ich habe nun für meinen Garten (2000 m²) einen (oder mehrere) Router, so dass ich mein neues Gewächshaus steuern kann. Übrigens war ich erstaunt über die Wlan-Reichweite des D1 Mini NodeMCU. Auf der Hälfte des Weges streiken unsere ( und die unserer Enkel und Kinder ebenso) Smartphones (WLAN) während der D1 Mini am Ende des Gartens (ca. 60 m) noch mit einer Geschwindigkeit von 2 Mbit per WLAN ohne einen Router sendete.

HaJo

HaJo

Ich muss mich für den Kommentar vom 30.09.2021 entschuldigen. In dem Stress hatte ich vor dem beschriebenen Flashen vergessen, vor dem Flashen den Erase-Button zu drücken. Somit war natürlich beim Flashen der 0×02000-bin und der 0×82000-bin noch die 0×00000-bin vorhanden. Werden nur die beiden ersten Bins nach Erase geflasht, funktioniert die Configuration nicht!
Obwohl ich einmalig meinen ESP8266-Natrouter zum Laufen gebracht habe, war der Vorgang nicht reproduzierbar, da auf der Configurationsseite es mir nicht gelang, erneut die Daten dauerhaft einzutragen. Um die beiden D1 Mini NodeMcu mit ESP8266-12F WLAN Module zu testen, ob sie fehlerhaft sind, habe ich diese mit ESPEasy geflasht. Zunächst hatte ich ebenfalls keinen Erfolg (getestet mit einer Version aus 2019). Nach dem ich die aktuelle Version vom 15.10.2021 geflasht hatte, lief alles wie am Schnürchen. Ich habe zwar jetzt keinen Router :-(( dafür aber einen AP, den meine Fritzbox nach der Configuration sofort erkannte und mit dem ich nun wunderbar nach allen Regeln der Kunst spielen kann. Wer ESPEasy selber ausprobieren möchte, empfehle ich:
https://www.az-delivery.de/blogs/azdelivery-blog-fur-arduino-und-raspberry-pi/esp-easy
und ff. Die aktuelle Zip-Datei kann unter:
https://github.com/letscontrolit/ESPEasy/releases/tag/mega-20211005
heruntergeladen werden.
Nochmals Entschuldigung für meinem Kommentar vom 30.09.2021. Aber ich war so happy, dass es nun – leider nur einmalig – funktionierte, dass ich es an der notwendigen Sorgfalt fehlen ließ.

HaJo

HaJo

Fast wäre ich verzweifelt – nach dem ich meinen ESP8266Mod-12F (AZ-Delivery D1 Mini) geflasht hatte, konnte ich wie beschrieben das WLAN-Netz MyApp aktivieren, ich konnte die AP-SSID und das Passwort ändern nur die Einbindung ins heimische Netz war nicht möglich (auch erkennbar in der ESP-Konfigurationsseite). Nach der Aufforderung Connect wurde der Inhalt in den Feldern SSID und Passwort des heimischen Netzes gelöscht. In meiner Fritzbox 7590 wurde auch nur unter “Kanälen” das neue WLAN-Netz angezeigt aber mit der korrekten Mac-Adresse. Alle Anstrengungen den ESP in das Netz der Fritzbox einzubinden schlug fehl.
Bei der Internetrecherche fand ich ein Tutorial für den Raspi, das sich mit dem ESP8266 als Repeater befasste. Der Autor hatte ähnliche Probleme. Als Lösung fand er folgenden Weg: Nur die Bindateien 0×02000 und 0×82000 flashen und den SPI-Mode DIO einstellen und siehe da, ich konnte endlich meinen ESP als Gerät in der Fritzbox registrieren: Gerätename ESP-xxxxxx (die letzten 6 Stellen der Mac-Adresse). Nun noch in der Konfigurationsseite die Checkbox “automesh” anklicken und erneut den Connect-Button anklicken (fehlt in der aktualisierten Seite der Haken, Haken erneut setzen und den Connect-Button erneut anklicken). Nun funktioniert auch der Repeater und wird in der Fritz!Box angezeigt. Der Name des neuen Repeaters kann beliebig gewählt werden. Die merkwürdige Darstellung in der Fritz!Box soll man ruhig übersehen, Hauptsache es funktioniert.

Andreas Wolter

Andreas Wolter

@Tester: genau das war das Problem. Es gibt keine .ino für den ESP32. Der Beitrag fasst sämtliche Erfahrungen zusammen, die ich gemacht habe, um die ESP32 bzw. ESP8266 als Extender zu nutzen. Ich hatte dabei massive Probleme und es funktionierte nichts so wie es sollte. Ich wollte versuchen, es nicht mit einem fertigen BIN zu lösen. Aber es ist aktuell die einzige brauchbare Möglichkeit. Der Quellcode ist offen, den finden Sie bei Github (Link im Beitrag).

Andreas Wolter

Andreas Wolter

@Norbert: Danke für den Hinweis. Ich habe das im Beitrag geändert und nun zur Downloadseite, statt zum Download selbst verlinkt.

Norbert

Norbert

Der Link für den ESP Flash Download hat sich geändert (neue Version)
https://www.espressif.com/sites/default/files/tools/flash_download_tool_3.9.0_0.zip

Tester

Tester

Das „einzige brauchbare“ ist das untere Programm, aber wo finde ich die .ino – Datei ? Fertige .bin Dateien helfen beim Testen/Weiterentwickeln nicht. Gute Zusammenfassung, aber das Programm ist seit 2 Jahren im Internet abrufbar.

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN