Changing the WiFi name in DNSServer example

Hi,
I'm working with an ESP12F and the DNSServer example of 'CaptivePortalAdvanced'.
Instead of hardcoding the WiFi name (which appears on the Windows WiFi portal), I want the program to read the chip ID of the ESP module and show the WiFi name as ESP12F_<chip_id> on the windows wifi portal. I altered the code in the following way to achieve the above, but it does not work for me. I'd really appreciate if someone could point out to what I've done wrong or advice me on it.

Here's the part I altered:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>

#ifndef APPSSID
#define APPSSID 
//String ESPSSID=  "";
#define APPSK  "password_123"
#endif

 String ESPSSID2= "ESP12F_";
 unsigned int ESPSSID1 = ESP.getChipId();
 ESPSSID2.concat(ESPSSID1);


String softAP_ssid = ESPSSID2;
const char *softAP_password = APPSK;
const char *hostName = "esp8266";
char ssid[32] = "";
char password[32] = "";
const byte DNS_PORT = 53;
DNSServer dnsServer;

Thank you!!

What exactly do you mean by that ?

 String ESPSSID2= "ESP12F_";
 unsigned int ESPSSID1 = ESP.getChipId();
 ESPSSID2.concat(ESPSSID1);

I do not use Strings, but can you concatenate an int with a String as you are doing ? In addition, are you sure that the ID is returned as an int ?

I get an error saying "ESPSSID2 does not name a type".

The ID is returned as a hex value

I tried it within the setup() function, and it worked (I tried printing the result)

Please post your complete sketch, or a smaller but complete sketch that illustrates the problem, and the full error message copied from the IDE using the convenient button. Use code tags when posting in both cases

Here's the code:

#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>

#ifndef APPSSID
#define APPSSID 
//String ESPSSID=  "";
#define APPSK  "password_123"
#endif

 String ESPSSID2= "ESP12F_";
 unsigned int ESPSSID1 = ESP.getChipId();
 ESPSSID2.concat(ESPSSID1);


String softAP_ssid = ESPSSID2;
const char *softAP_password = APPSK;
const char *hostName = "esp8266";

char ssid[32] = "";
char password[32] = "";
const byte DNS_PORT = 53;
DNSServer dnsServer;

// Web server
ESP8266WebServer server(80);

/* Soft AP network parameters */
IPAddress apIP(192, 168, 4, 1);
IPAddress netMsk(255, 255, 255, 0);


/** Should I connect to WLAN asap? */
boolean connect;

/** Last time I tried to connect to WLAN */
unsigned long lastConnectTry = 0;

/** Current WLAN status */
unsigned int status = WL_IDLE_STATUS;

void setup() {
  delay(1000);
  Serial.begin(9600);
  Serial.println();
  Serial.println("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAPConfig(apIP, apIP, netMsk);
  WiFi.softAP(softAP_ssid, softAP_password);
  delay(500); // Without delay I've seen the IP address blank
  Serial.print("AP IP address: ");
  Serial.println(WiFi.softAPIP());

  /* Setup the DNS server redirecting all the domains to the apIP */
  dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  dnsServer.start(DNS_PORT, "*", apIP);

  /* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */
  server.on("/", handleRoot);
  server.on("/wifi", handleWifi);
  server.on("/wifisave", handleWifiSave);
  server.on("/generate_204", handleRoot);  //Android captive portal. Maybe not needed. Might be handled by notFound handler.
  server.on("/fwlink", handleRoot);  //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
  server.onNotFound(handleNotFound);
  server.begin(); // Web server start
  Serial.println("HTTP server started");
  loadCredentials(); // Load WLAN credentials from network
  connect = strlen(ssid) > 0; // Request WLAN connect if there is a SSID
}

void connectWifi() {
  Serial.println("Connecting as wifi client...");
  WiFi.disconnect();
  WiFi.begin(ssid, password);
  int connRes = WiFi.waitForConnectResult();
  Serial.print("connRes: ");
  Serial.println(connRes);
}

void loop() {
  if (connect) {
    Serial.println("Connect requested");
    connect = false;
    connectWifi();
    lastConnectTry = millis();
  }
  {
    unsigned int s = WiFi.status();
    if (s == 0 && millis() > (lastConnectTry + 60000)) {
      /* If WLAN disconnected and idle try to connect */
      /* Don't set retry time too low as retry interfere the softAP operation */
      connect = true;
    }
    if (status != s) { // WLAN status change
      Serial.print("Status: ");
      Serial.println(s);
      status = s;
      if (s == WL_CONNECTED) {
        /* Just connected to WLAN */
        Serial.println("");
        Serial.print("Connected to ");
        Serial.println(ssid);
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());

        // Setup MDNS responder
        if (!MDNS.begin(myHostname)) {
          Serial.println("Error setting up MDNS responder!");
        } else {
          Serial.println("mDNS responder started");
          // Add service to MDNS-SD
          MDNS.addService("http", "tcp", 80);
        }
      } else if (s == WL_NO_SSID_AVAIL) {
        WiFi.disconnect();
      }
    }
    if (s == WL_CONNECTED) {
      MDNS.update();
    }
  }
  // Do work:
  //DNS
  dnsServer.processNextRequest();
  //HTTP
  server.handleClient();
}

The error:

exit status 1

'ESPSSID2' does not name a type

You are trying to use the concat() function outside of a function. Try using it in setup() instead

There also appear to be some functions such as handleRoot(), handleWifi(), handleWifiSave() and handleNotFound() missing from the sketch

It's this code that I'm using:

Have you got all 4 of the .ino files in the same folder ?

What happens when you fix the problem noted in reply #6 ?

Yes I have

It gives highlights the following line and gives the following error.

Highlighted line:

 WiFi.softAP(softAP_ssid, softAP_password);

The error:

exit status 1
invalid conversion from 'char' to 'const char*' [-fpermissive]

Please post your updated sketch. We can't guess what your "fix" was. Also, when you post error messages, don't cut and paste part of it. Post the entire listing.

unsigned int ESPSSID1 = ESP.getChipId();

I am still suspicious of the line. You say

Do you mean that when you Serial.print() the ID with no format specifier you see a Hex string ? If so then it is not an int

Yes, I had used the following code:

Serial.println(ESPSSID2):

Then the value returned is not an int but either a String or a string. Try declaring the ID as a String rather than an int

Then it gives me the following error

exit status 1
conversion from 'uint32_t {aka unsigned int}' to 'String' is ambiguous

As a small test try this


void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println(ESP.getChipId());
  String base = "BASE_";
  String ID = String(ESP.getChipId());
  base.concat(ID);
  Serial.println(base);
}

void loop()
{
}

If it works (it does for me) then apply the same principles to your sketch

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