I am developing a code snippet to connect an ESP32 to an existing WiFi.
The code snippet uses the AP+STA mode to configure the ESP
The idea is to configure the name of the existing WiFi and the password in the ESP32 through a web downloaded through the WiFi created in AP mode.
First, the ESP32 shows the name and the IP created on the screen of the Heltec board.
When a user connects to that IP through that WiFi and to this IP, a website is downloaded where he can enter the name and password of his own WiFi. And, once connected, the ESP would inform the user of the IP to which they can definitely connect, without having to use the ESP32's WiFi.
I want the user to be able to read this IP address (of the user's WiFi) through a website or through the Heltec display. Not through the serial monitor of the programming IDE.
However, I am having trouble getting the local IP address programmatically.
Both the IP address of the AP connection and the IP address of the STA connection are displayed through the serial monitor without problems. Probably because Serial.println(WiFi.softAPIP()) and Serial.println(WiFi.localIP()) use the "Printable" interface to get these addresses.
But, although the IP address of the AP connection doesn't give me any problems, I can't save the IP address of the STA connection in the code.
I have tried several combinations all of them with negative results. The next code:
IPAddress STAIP = WiFi.localIP();
uint8_t staIP_array[4] = { STAIP[0],STAIP[1],STAIP[2],STAIP[3] };
uint32_t staIP_dword = STAIP;
String strIP_STA =
String(staIP_array[0]) + "." +
String(staIP_array[1]) + "." +
String(staIP_array[2]) + "." +
String(staIP_array[3]);
String strIP = WiFi.localIP().toString();
std::string ipadd1 = WiFi.localIP().toString().c_str();
Serial.print("STA IP:");
Serial.println(STAIP);
Serial.print(" - STA IP (array):");
Serial.println(strIP_STA);
Serial.print(" - STA IP (dword):");
Serial.println(staIP_dword);
Serial.print(" - STA IP (strIP):");
Serial.println(strIP);
Shows:
STA IP:0.0.0.0
- STA IP (array):0.0.0.0
- STA IP (dword):0
- STA IP (strIP):0.0.0.0
Even though the same code for the IP address of the AP connection works fine:
IPAddress softAPIP = WiFi.softAPIP();
uint8_t IP_array[4] = { softAPIP[0],softAPIP[1],softAPIP[2],softAPIP[3] };
String strIP =
String(IP_array[0]) + "." +
String(IP_array[1]) + "." +
String(IP_array[2]) + "." +
String(IP_array[3]);
...
Heltec.display->drawString(30, 20, strIP);
How can I get the IP address of the STA connection in AP+STA mode of the ESP32?
Is it possible to take advantage of the Printable interface in some way?