Wifi Extender does not work if I stange the SSID in the code

Hey falks,

many of you guys (@UKHeliBob) know, that I am looking for an Wifi Extender for weeks now. It has to be an ESP8266. I now found this code here:

#if LWIP_FEATURES && !LWIP_IPV6
#define HAVE_NETDUMP 0

#ifndef STASSID
#define STASSID "Netgear"
#define STAPSK "11111111"
#endif
#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>
#define NAPT 1000
#define NAPT_PORT 10
#if HAVE_NETDUMP
#include <NetDump.h>

void dump(int netif_idx, const char* data, size_t len, int out, int success) {
  (void)success;
  Serial.print(out ? F("out ") : F(" in "));
  Serial.printf("%d ", netif_idx);

  // optional filter example: if (netDump_is_ARP(data))
  {
    netDump(Serial, data, len);
    // netDumpHex(Serial, data, len);
  }
}
#endif

void setup() {
  Serial.begin(115200);
  Serial.printf("\n\nNAPT Range extender\n");
  Serial.printf("Heap on start: %d\n", ESP.getFreeHeap());

#if HAVE_NETDUMP
  phy_capture = dump;
#endif

  // first, connect to STA so we can get a proper local DNS server
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());

  // By default, DNS option will point to the interface IP
  // Instead, point it to the real DNS server.
  // Notice that:
  // - DhcpServer class only supports IPv4
  // - Only a single IP can be set
  auto& server = WiFi.softAPDhcpServer();
  server.setDns(WiFi.dnsIP(0));

  WiFi.softAPConfig(  // enable AP, with android-compatible google domain
    IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));
  WiFi.softAP(STASSID "extender", STAPSK);
  Serial.printf("AP: %s\n", WiFi.softAPIP().toString().c_str());

  Serial.printf("Heap before: %d\n", ESP.getFreeHeap());
  err_t ret = ip_napt_init(NAPT, NAPT_PORT);
  Serial.printf("ip_napt_init(%d,%d): ret=%d (OK=%d)\n", NAPT, NAPT_PORT, (int)ret, (int)ERR_OK);
  if (ret == ERR_OK) {
    ret = ip_napt_enable_no(SOFTAP_IF, 1);
    Serial.printf("ip_napt_enable_no(SOFTAP_IF): ret=%d (OK=%d)\n", (int)ret, (int)ERR_OK);
    if (ret == ERR_OK) { Serial.printf("WiFi Network '%s' with same password is now NATed behind '%s'\n", STASSID "extender", STASSID); }
  }
  Serial.printf("Heap after napt init: %d\n", ESP.getFreeHeap());
  if (ret != ERR_OK) { Serial.printf("NAPT initialization failed\n"); }
}

#else

void setup() {
  Serial.begin(115200);
  Serial.printf("\n\nNAPT not supported in this configuration\n");
}

#endif

void loop() {}

It works very well. But, when I change the code on the top to this here:

//#define STASSID "Netgear"
String STASSID ="Netgear";
#define STAPSK "11111111"

than it don't work properly. The ESP connects to my router, yes. And my other devices connects to the ESP-Extender, yes. But there is no actual internet connection.

Is this because I used the Wifi.begin() method with a String instead of the defintion ?

To be clear, It has to be a String, because the wifi credentials will be delivered via Serial. This code above is only a test.

You are defining a variable of type String with name STAPSK
then you define a macro with the exact same name STAPSK
and assign it a different value.

Don't you think the variable should be named

String STASSID ="Netgear";

instead of STAPSK
the name for the ssid should be
STASSID
?

1 Like

Sorry, that was just wrong written here in the forum. I've just corrected it into:

//#define STASSID "Netgear"
String STASSID ="Netgear";
#define STAPSK "11111111"

So this still does not etablish an actual internet connection.

try

 WiFi.begin(STASSID.c_str(), STAPSK.c_str());

not sure if this works but often such functions expect a so called c_string.

1 Like

I guess I tried this out too... but I will try now, wait.

It still don't work. The ESP connects to my router, and my other devices connects to ma ESP as well... the connected devices has no internet access. Wait, I will post the code at this moment:


// NAPT example released to public domain

#if LWIP_FEATURES && !LWIP_IPV6

#define HAVE_NETDUMP 0



String SSIDD ="Netgear";
String SSID2 ="Range-ext";
String STAPSK ="11111111";
/*
#ifndef STASSID
#define STASSID "Netgear"
#define STAPSK "11111111"
#endif
*/

#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>

#define NAPT 1000
#define NAPT_PORT 10

#if HAVE_NETDUMP

#include <NetDump.h>

void dump(int netif_idx, const char* data, size_t len, int out, int success) {
  (void)success;
  Serial.print(out ? F("out ") : F(" in "));
  Serial.printf("%d ", netif_idx);

  // optional filter example: if (netDump_is_ARP(data))
  {
    netDump(Serial, data, len);
    // netDumpHex(Serial, data, len);
  }
}
#endif



void setup() {
  Serial.begin(115200);
  Serial.printf("\n\nNAPT Range extender\n");
  Serial.printf("Heap on start: %d\n", ESP.getFreeHeap());

#if HAVE_NETDUMP
  phy_capture = dump;
#endif

  // first, connect to STA so we can get a proper local DNS server
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSIDD.c_str(), STAPSK.c_str());
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());

  // By default, DNS option will point to the interface IP
  // Instead, point it to the real DNS server.
  // Notice that:
  // - DhcpServer class only supports IPv4
  // - Only a single IP can be set
  auto& server = WiFi.softAPDhcpServer();
  server.setDns(WiFi.dnsIP(0));

  WiFi.softAPConfig(  // enable AP, with android-compatible google domain
    IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));
  WiFi.softAP(SSID2.c_str(), STAPSK.c_str());
  Serial.printf("AP: %s\n", WiFi.softAPIP().toString().c_str());

  Serial.printf("Heap before: %d\n", ESP.getFreeHeap());
  err_t ret = ip_napt_init(NAPT, NAPT_PORT);

  Serial.printf("Heap after napt init: %d\n", ESP.getFreeHeap());
  if (ret != ERR_OK) { Serial.printf("NAPT initialization failed\n"); }
}

#else

void setup() {
  Serial.begin(115200);
  Serial.printf("\n\nNAPT not supported in this configuration\n");
}

#endif

void loop() {}

Forget to mention the serial monitor output:

23:24:02.343 -> STA: 192.168.43.57 (dns: 192.168.43.1 / (IP unset))
23:24:03.326 -> AP: 172.217.28.254
23:24:03.373 -> Heap before: 48416
23:24:03.373 -> Heap after napt init: 24200

May be it has somethink to do with the (IP unset) ?

[EDIT] I just tried the original code out. There is also an (IP unset). So, this is clearly not the problem.

I don't know.

This is your actual code including serial prints of the SSID/PSK variables right before using them
The string are embraced with "#" to make very clear what the real content is

// NAPT example released to public domain

#if LWIP_FEATURES && !LWIP_IPV6

#define HAVE_NETDUMP 0



String SSIDD ="Netgear";
String SSID2 ="Range-ext";
String STAPSK ="11111111";
/*
#ifndef STASSID
#define STASSID "Netgear"
#define STAPSK "11111111"
#endif
*/

#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>

#define NAPT 1000
#define NAPT_PORT 10

#if HAVE_NETDUMP

#include <NetDump.h>

void dump(int netif_idx, const char* data, size_t len, int out, int success) {
  (void)success;
  Serial.print(out ? F("out ") : F(" in "));
  Serial.printf("%d ", netif_idx);

  // optional filter example: if (netDump_is_ARP(data))
  {
    netDump(Serial, data, len);
    // netDumpHex(Serial, data, len);
  }
}
#endif



void setup() {
  Serial.begin(115200);
  Serial.printf("\n\nNAPT Range extender\n");
  Serial.printf("Heap on start: %d\n", ESP.getFreeHeap());

#if HAVE_NETDUMP
  phy_capture = dump;
#endif

  // first, connect to STA so we can get a proper local DNS server
  WiFi.mode(WIFI_STA);

  Serial.print("SSIDD=#");
  Serial.print(SSIDD);
  Serial.println("#");
  
  Serial.print("STAPSK=#");
  Serial.print(STAPSK);
  Serial.println("#");

  Serial.print("SSID2=#");
  Serial.print(SSID2);
  Serial.println("#");

  WiFi.begin(SSIDD.c_str(), STAPSK.c_str());
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());

  // By default, DNS option will point to the interface IP
  // Instead, point it to the real DNS server.
  // Notice that:
  // - DhcpServer class only supports IPv4
  // - Only a single IP can be set
  auto& server = WiFi.softAPDhcpServer();
  server.setDns(WiFi.dnsIP(0));

  WiFi.softAPConfig(  // enable AP, with android-compatible google domain
    IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));

  Serial.print("STAPSK=#");
  Serial.print(STAPSK);
  Serial.println("#");

  Serial.print("SSID2=#");
  Serial.print(SSID2);
  Serial.println("#");

  WiFi.softAP(SSID2.c_str(), STAPSK.c_str());
  Serial.printf("AP: %s\n", WiFi.softAPIP().toString().c_str());

  Serial.printf("Heap before: %d\n", ESP.getFreeHeap());
  err_t ret = ip_napt_init(NAPT, NAPT_PORT);

  Serial.printf("Heap after napt init: %d\n", ESP.getFreeHeap());
  if (ret != ERR_OK) { Serial.printf("NAPT initialization failed\n"); }
}

#else

void setup() {
  Serial.begin(115200);
  Serial.printf("\n\nNAPT not supported in this configuration\n");
}

#endif

void loop() {}
1 Like

You are setting up softap
don't you think you have to use the combined AP and STA mode?

WiFi.mode(WIFI_AP_STA);

You are right, but the original code which by the way you can find in the examples>Esp8266wifi>RangeExtender-NAPT is also like this:

WiFi.mode(WIFI_STA); <-- and this works

And I have just tried out your edited code with these Serial.prints. It is misterious, that the SSIDD is missing. Here is my Serial Monitor output with your code:

23:36:40.006 -> STA: 192.168.43.57 (dns: 192.168.43.1 / (IP unset))
23:36:40.099 -> STAPSK=#11111111#
23:36:40.099 -> SSID2=#Range-ext#
23:36:40.989 -> AP: 172.217.28.254
23:36:40.989 -> Heap before: 48392
23:36:40.989 -> Heap after napt init: 24176

So there is no SSIDD

add a delay(2000);
after

when I use your code in #8, the serial monitor says this:

23:47:31.900 -> SSIDD=#Netgear#
23:47:33.913 -> STAPSK=#11111111#
23:47:33.913 -> SSID2=#Range-ext#
23:47:33.913 -> .........
23:47:39.107 -> STA: 192.168.43.57 (dns: 192.168.43.1 / (IP unset))
23:47:39.248 -> STAPSK=#11111111#
23:47:39.248 -> SSID2=#Range-ext#
23:47:40.090 -> AP: 172.217.28.254
23:47:40.137 -> Heap before: 48392
23:47:40.137 -> Heap after napt init: 24176

In other words: it recognizes all variables. I think, that there is a problem with eather the Wifi.begin() method, or a problem with the
#include <lwip/napt.h>
#include <lwip/dns.h>

I guess, that it has somethink to do with DNS... look at the comments:

  // first, connect to STA so we can get a proper local DNS server
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSIDD.c_str(), STAPSK.c_str());
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(500);
  }
  Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());
  // By default, DNS option will point to the interface IP
  // Instead, point it to the real DNS server.
  // Notice that:
  // - DhcpServer class only supports IPv4
  // - Only a single IP can be set

@StefanL38
I guess I have found the problem... but first:

I have put the String right on the very top of the code... particularly above the #if defintion.. like this:

String STASSID ="Netgear";
String sSSID ="Netgear-v";
String STAPSK ="11111111"; 
// As you can see, the strings are now above the #if
#if LWIP_FEATURES && !LWIP_IPV6

#define HAVE_NETDUMP 0

#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>

I dont't know whether I shoulg open a new topic @UKHeliBob because now I have a second question:

is it possible, to SPIFFS read file on the very top of the code ? Like this:


#include "FS.h"
File b = SPIFFS.open("/readssid.txt", "r");
String STASSID =b.readStringUntil('\n'); // in other words, the SSID is stored in .txt

String sSSID ="Netgear-v";
String STAPSK ="11111111"; 

#if LWIP_FEATURES && !LWIP_IPV6
#define HAVE_NETDUMP 0
#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>

@UKHeliBob should I open a new topic ?

No it is not possible because you are using a function-call.

The earliest to make a function-call is function setup.

And as you want to use the function readStringUntil
all functions that have to do with the serial interface can only work
if you have initialised the serial interface with

Serial.begin(115200);

No I don't think so. You can go on in this thread
But do the following:
change the title to something like:

ESP8266 as WiFi-range-extender I can connect to the WiFi-SoftAP but no internet-connection

This titles does say all the important details:

  • type of microcontroller
  • functionality (WiFi-range-extender)
  • problem (no internet-connection)

add to your first post a text that describes in short what you have tried with

  • what has worked
  • what has not worked
  • what you want the code to do

and then write something like
"the actual code that I used for testing is in post #

best regards Stefan

1 Like

Hi @tolga121

You marked the thread as solved.
would you mind posting your working code?

best regards Stefan

1 Like

Absolutely I will post the code :smiley: In fact, I was posting the code... you were just to quick in demanding the code :smiley: wait a minute.

Here is the solution...

The Problem was: the connections were etablished, but the clients which were connected to the Wifi-Repeater, had no actual internet connection despite being connected to the Repeater.

The Solution:
My self-created String should be before the #if directive. So this here is wrong:

#define HAVE_NETDUMP 0
#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>
#define NAPT 1000
#define NAPT_PORT 10

#if HAVE_NETDUMP
#include <NetDump.h>
String STASSID ="Netgear";
String STAPSK ="11111111";
String sSsid ="Repeater";
String sPass ="11111111";

and this is correct, because I placed the strings before the #if directive begin:

String STASSID ="Netgear";
String STAPSK ="11111111";
String sSsid ="Repeater";
String sPass ="11111111";

#define HAVE_NETDUMP 0
#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>
#define NAPT 1000
#define NAPT_PORT 10

#if HAVE_NETDUMP
#include <NetDump.h>

I don't know the problem in detail, but it works very well. I solved it by "testing by doing".

And here is the full code which turnes your ESP as an Wifi-Range-Extender with Strings. It had to be Strings, because I could work better with Strings. For example: now the Wifi-Credentials should be provided by Serial.Read from other ESPs... I haven't done this yet, but in the coming days, I will change the code. My aim is simply get the Wifi-Credentials by connecting one ESP to antoher ESP where the credentials are stored. Like "plug&play" method. Plug it onto another ESP to let it read out the credentials :smiley: Sorry for my English... So here is the code that works with normals Strings:

String STASSID ="Netgear"; // your router
String STAPSK ="11111111"; // your router
String sSsid ="Repeater"; // repeater credentials
String sPass ="11111111"; // repeater credentials


#define HAVE_NETDUMP 0
#include <ESP8266WiFi.h>
#include <lwip/napt.h>
#include <lwip/dns.h>
#define NAPT 1000
#define NAPT_PORT 10

#if HAVE_NETDUMP
#include <NetDump.h>

void dump(int netif_idx, const char* data, size_t len, int out, int success) {
  (void)success;
  Serial.print(out ? F("out ") : F(" in "));
  Serial.printf("%d ", netif_idx);
  // optional filter example: if (netDump_is_ARP(data))
  {
    netDump(Serial, data, len);
    // netDumpHex(Serial, data, len);
  }
}
#endif

void setup() {
  Serial.begin(115200);

  #if HAVE_NETDUMP
  phy_capture = dump;
  #endif

  // first, connect to STA so we can get a proper local DNS server
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID.c_str(), STAPSK.c_str());
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // By default, DNS option will point to the interface IP
  // Instead, point it to the real DNS server.
  // Notice that:
  // - DhcpServer class only supports IPv4
  // - Only a single IP can be set
  auto& server = WiFi.softAPDhcpServer();
  server.setDns(WiFi.dnsIP(0));
  WiFi.softAPConfig(  // enable AP, with android-compatible google domain
    IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));
  WiFi.softAP(sSsid.c_str(), sPass.c_str());

  err_t ret = ip_napt_init(NAPT, NAPT_PORT);
  if (ret == ERR_OK) {
    ret = ip_napt_enable_no(SOFTAP_IF, 1);
  }
  if (ret != ERR_OK) { }
}


void loop() {
}

@StefanL38 Greetings from Germany

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.