In this tutorial, we will check how to get the IP of a socket client that connects to a socket server hosted on the ESP32, using the Arduino core.

You can check a detailed tutorial on how to set a socket server on the ESP32 on this previous tutorial. If you want to learn how to develop a socket client on the ESP32, then please consult this tutorial.

In this tutorial, since the ESP32 will be working as a server, we will need to develop a socket client to contact it. We will do it in Python, since it is very simple to set a socket client on this language.

If you don’t feel comfortable with Python development, then you can skip that section and use a web browser as socket client instead. Details on how to use the browser to reach the ESP32 socket server are explained at the end of the “Testing the Code” section.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The Python code

First of all, we need to import Python’s socket module, which we will use to create a socket client and connect to the ESP32 server.

import socket

Next we need to create an object of class socket. This object will expose the methods we need to connect to the server and send data to it.

sock = socket.socket()

Now we will assign the IP address and port of the ESP32 server to two variables, since we are going to need this information to contact the server. If you don’t know the IP of your your ESP32 in your local network, we will print it later on the Arduino code. You should copy it and use it here.

Note that below I’m using the IP of my ESP32 on my local network. Yours will most likely be different.

We will use port 80 for the server and this value is set by us on the Arduino code, so it doesn’t depend on the network.

host = ""
port = 80

To establish the connection to the server, we call the connect method on the socket object, passing as argument a tuple with the host and port we have just declared.

sock.connect((host, port)) 

To send some data, we need to call the send method on the socket object and pass as input the content to be sent to the server. We will send a simple string.


Finally, we call the close method on the socket object to close the connection and free the resources.


The final Python code can be seen below.

import socket               

sock = socket.socket()

host = "" #ESP32 IP in local network
port = 80             #ESP32 Server Port    

sock.connect((host, port))

The Arduino code

The Arduino code will be very simple. We are going to focus on getting the remote client IP, so we are not going to establish any exchange of data with it.

As usual, since we need to make our ESP32 reachable by clients, we will need to connect it to a WiFi network. This means we need to include the WiFi.h library and declare two variables to hold the network credentials (password and network name).

We will also need an object of class WiFiServer, which we will use to set the server and receive incoming socket client connections.

The constructor of this class receives as input the port where the server will be listening. As we have seen while writing the Python code, we will use port 80. Nonetheless, you can test with other ports, as long as you update the Python code accordingly.

#include "WiFi.h"

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

WiFiServer wifiServer(80);

Moving on to the Arduino setup, we start by opening a serial connection to output the results of our program and then we connect the ESP32 to the WiFi network using the previously declared credentials.

After the connection is established, we need to start the socket server, so it receives incoming requests. This is done by calling the begin method. This method receives no arguments...

Read more »