0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino web server: getting client IP

DFRobot Mar 15 2018 1893

The objective of this post is to explain how to obtain the IP of a client that contacts a HTTP web server running on the ESP32, on the Arduino core. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

Introduction

The objective of this post is to explain how to obtain the IP of a client that contacts a HTTP web server running on the ESP32, on the Arduino core.

We will use the HTTP async web server libraries we have been covering in the past tutorials. So, much of the code will be similar to what we have been covering and thus we will focus on the route handling function where we will analyze how to get the IP of the clients that contact the server.

If you have not been following the previous tutorials, you can check the “Related Posts” section at the end of this article, which lists all the previous tutorials.

The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

The code

As usual, we start by doing the library includes and global variables declaration that we will need to connect to the WiFi network and to configure the HTTP async web server.

#include <WiFi.h>
#include <FS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
 
const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";
 
AsyncWebServer server(80);

Next, on the Arduino setup function, we connect the ESP32 to the WiFi network and print its local IP after the connection is established, so we know the address to use for contacting it.

We are printing a couple of newlines after so we separate the local IP of the ESP32 from the IP of the clients that will contact it, that we will also print next.

Serial.begin(115200);
 
WiFi.begin(ssid, password);
 
while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
} 
 
Serial.print("Server IP: ");
Serial.println(WiFi.localIP());
Serial.println("\n\n");

Now that we have handled the WiFi connection, we need to configure the routes of the server. As we have been doing in the past tutorials, we simply need to call the on method on the server object, passing as input the route, the HTTP method(s) allowed on that route and the handling function.

For this example, we will call our route “/getIp” and listen to HTTP GET requests. Note however that we should be able to print the IP of the client using this approach independently of the HTTP method that our route is listening to.

server.on("/printIp", HTTP_GET, [](AsyncWebServerRequest *request){
// Route handling function code
});

Now we are going to focus on the AsyncWebServerRequest object pointer that is accessible inside the handling function.

As we have been seen through these tutorials, our web server works asynchronously. We also know that HTTP works on top of TCP sockets, so under the hood the async HTTP web server library is using this mechanism for the lower level communication.

Thus, the web server API allows us to access the lower level client object which is used to exchange the data with the client.

This is an object of class AsyncClient (header file here), which is implemented in the AsyncTCP library. As we have seen in the HTTP web server introductory tutorial, this ESP32 async TCP library is one of the dependencies for the HTTP async web server library. Also, this is one of the libraries that we include on the beginning of our programs.

Thus, in order to obtain the mentioned object, we can call the client method on our request object pointer.

This method takes no arguments and returns a pointer to an object of class AsyncClient. We can then use this new object pointer to call the remoteIP method, which also takes no arguments and returns the client IP.

You can check below the mentioned method calls. Note that we first return the answer of the HTTP request to the client and only then take care of getting the IP and printing it to the serial console. That way, we won’t leave the client hanging, waiting for an answer.

server.on("/printIp", HTTP_GET, [](AsyncWebServerRequest *request){ 
 
    request->send(200, "text/plain", "ok");
 
    Serial.print("Received request from client with IP: ");
    Serial.println(request->client()->remoteIP());
});

To finalize our code we still need to call the begin method on the server object so it begins listening to incoming HTTP requests. You can check the full source code below.

#include <WiFi.h>
#include <FS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
 
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPassword";
 
AsyncWebServer server(80);
 
void setup(){
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.print("Server IP: ");
  Serial.println(WiFi.localIP());
  Serial.println("\n\n");
 
  server.on("/printIp", HTTP_GET, [](AsyncWebServerRequest *request){ 
 
    request->send(200, "text/plain", "ok");
 
    Serial.print("Received request from client with IP: ");
    Serial.println(request->client()->remoteIP());
  });
 
  server.begin();
}
 
void loop(){}

Testing the code

To test the code we need to follow the usual procedure. First, we compile and upload the code to the ESP32 using the Arduino IDE.

When the procedure finishes, open the serial monitor and copy the IP that gets printed. Then, open a web browser of your choice, and type the following in the address bar, changing #yourDeviceIp# by the IP you have just copied.

http://#yourDeviceIp#/printIp

In your web browser, you should get the “ok” answer we defined in the code. If you go back to the serial monitor, then a message similar to the one shown in figure 1 should get printed, containing the local IP of the computer that you have used to contact the ESP32.

Figure 1 – IP of the clients getting printed.
In my case, I’ve tested making the request from two different computers and, as shown in the figure, I got two different IPs. I’ve also included in the figure a snippet of the local IP of the first PC, which matches the one printed in the console.
If you are on Windows, you can get your computer IP by opening a command line window and using the ipconfig command. On Linux, you need to use the ifconfig command.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.