Reputation: 185
With the help of this tutorial i created a simple WebServer on my ESP32:
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi network with SSID and password
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP); //IP address of my ESP32
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects
Serial.println("New Client"); // print a message out in the serial port
//...
}
}
How to get the ip addess of this connecting client?^^ So thats maybe something there like
String ipaddress = /*...*/;
Upvotes: 0
Views: 3751
Reputation: 3736
Function remoteIP
returns the IP address as IPAddress object.
IPAddress ip = client.remoteIP();
IPAddress implements Printable so it can be used with println.
Serial.println(ip);
If you insist on getting the IP as String, the ESP32 core version of IPAddress has toString method.
String ipaddress = ip.toString();
There is no function to print the IPAddress object to characters array. You could use CStringBuilder class from my StreamLib to print the IPAddress to characters array. CStringBuilder allows to fill characters array with Arduino Stream class functions. The StreamLib library is in Library Manager.
If you don't want to use the StreamLib library here is a (maybe not ideal) function to format the IPAddress to characters array.
void ip2str(const IPAddress& ip, char* s) {
itoa(ip[0], s, 10);
size_t l = strlen(s);
s[l] = '.';
itoa(ip[1], s + l + 1, 10);
l = strlen(s);
s[l] = '.';
itoa(ip[2], s + l + 1, 10);
l = strlen(s);
s[l] = '.';
itoa(ip[3], s + l + 1, 10);
}
Upvotes: 2