Home / References / ESP32 Library / WiFiClientSecure
Description
The remoteIP
method in the ESP32 WiFiClient Library returns the IP address of the remote server or peer to which the client is currently connected. This method is essential for debugging network connections, verifying server identity, or logging communication details in your projects.
Syntax and Usage
The remoteIP
method is called on a WiFiClient
object to retrieve the remote server’s IP address. Below is the syntax and a code snippet demonstrating its usage:
IPAddress ip = client.remoteIP();
Here’s how this method can be used:
- Without Arguments: Simply call the method on an active
WiFiClient
instance to get the remote IP address as anIPAddress
object.
For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.
Argument(s)
This method does not require any arguments. It operates solely on the current state of the WiFiClient
connection.
Return Value
The remoteIP
method returns an IPAddress
object representing the IP address of the remote server. If the client is not connected, it returns an invalid IP address (typically 0.0.0.0
).
Example Codes
Below is an example demonstrating how to use the remoteIP
method in a practical scenario. This corresponds to the usage outlined in Section 2.
Example: Retrieving and Displaying the Remote IP Address
This example connects an ESP32 to www.httpbin.org
, retrieves the remote server’s IP address using remoteIP
, and prints it to the Serial Monitor for verification.
Explanation: The ESP32 connects to a WiFi network and establishes a TCP connection to www.httpbin.org
. The remoteIP
method is called to retrieve the server’s IP address, which is then printed to the Serial Monitor. This allows makers to confirm the identity of the remote server before sending an HTTP request and receiving a response.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
*
* Description: This example connects an ESP32 to
* www.httpbin.org, retrieves the remote server’s IP
* address using ESP32 WiFiClient Library's remoteIP,
* and prints it to the Serial Monitor for verification.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive
* Guide to the ESP32 Arduino Core Library, accessible on
* AvantMaker.com. For additional code examples and in-depth
* documentation related to the ESP32 Arduino Core Library,
* please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with the resources
* they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
const char* host = "www.httpbin.org";
const int port = 80;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
// Create a WiFiClient instance
WiFiClient client;
// Connect to the server
if (client.connect(host, port)) {
Serial.println("Connected to server");
// Get the remote IP address
IPAddress remote = client.remoteIP();
Serial.print("Remote IP Address: ");
Serial.println(remote);
// Send HTTP GET request
client.println("GET /get HTTP/1.1");
client.println("Host: www.httpbin.org");
client.println("Connection: close");
client.println();
// Wait for server response
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("Disconnected");
} else {
Serial.println("Connection failed");
}
}
void loop() {
// Nothing to do here
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFiClient Library
- Connection
- Send Data
- Receive Data
- Config
- Status
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!