ESP32 Arduino HTTP server: external and internal redirects

The objective of this ESP32 exemple code is to explain how to perform redirects using the async HTTP web server libraries 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 ESP32 exemple code is to explain how to perform redirects using the async HTTP web server libraries on the Arduino core. If you haven’t yet installed them, please check here how to do it.

We will check both how to redirect to an internal and to an external URL.

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

For more tutorials on the HTTP async server, please consult the related posts section.


The code

The first part of the code is similar to what we have been covering in the previous tutorial posts about the async HTTP web server. So, as usual, we started by including the libraries needed to setup everything.

Next, we declare two global variables that will hold the credentials needed to connect to the WiFi network. To finalize the global variables declaration, we will also need an instance of class AsyncWebServer, which we will use to setup the HTTP server.

#include <WiFi.h>
#include <FS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

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

AsyncWebServer server(80);

In the setup function, we will start by opening a serial connection and then connect the ESP32 to the WiFi network. After the connection, we will print the local IP assigned to it.

Serial.begin(115200);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
}

Serial.println(WiFi.localIP());

Now we will configure the routes of our server. First, we will need a simple route to be the destination of the internal redirect. The handling function of this route will return a textual message to the client. We will call this route “/hello“.

server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", "Hello from ESP32 server route");
});

Now that we have a working route, we will configure a second one to perform the internal redirect.

The declaration of the route and the binding to a handling function is performed the same way as in the previous one. Nonetheless, in the implementation of the handling function, we will call a different method of the request object to perform the redirect.

Thus, to perform the redirect, we call the redirect method of the request object, passing as input a string with the route to which we want to redirect the client.

Note that since the redirect will be internal, we simply need to specify the path (“/hello“), we don’t need to specify the whole URL (the http://{serverIp} should not be used).

We will call this route “/redirect/internal“.

server.on("/redirect/internal", HTTP_GET, [](AsyncWebServerRequest *request){
    request->redirect("/hello");
});

The third and last route we will specify will do an external redirect. The configuration is exactly the same but now we will pass the URL of the website to the redirect method. Since now we are going to redirect to an external location, we need to use the whole URL of the website, which includes the domain name (or the IP).

We will call this route “/redirect/external“.

server.on("/redirect/external", HTTP_GET, [](AsyncWebServerRequest *request){
    request->redirect("https://techtutorialsx.com/");
});

Finally, after setting all the routes, we need to call the begin method on the server object, so it starts listening to incoming HTTP requests. You can check the whole 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.println(WiFi.localIP());

  server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", "Hello from ESP32 server route");
  });

  server.on("/redirect/internal", HTTP_GET, [](AsyncWebServerRequest *request){
    request->redirect("/hello");
  });

  server.on("/redirect/external", HTTP_GET, [](AsyncWebServerRequest *request){
    request->redirect("https://techtutorialsx.com/");
  });

  server.begin();
}

void loop(){}


Testing the code

To test the whole code, first compile it and upload it to your ESP32 microcontroller and then open the Arduino IDE serial monitor.

After the connection to the Wireless Network is established, the local IP assigned to the ESP32 should be printed. Copy it, since we are going to use it to reach the server.

Now, open a web browser of your choice, typing the following URL and changing {yourDeviceIp} by the IP you just copied and {type} by the words external or internal.

http://{yourDeviceIp}/redirect/{type}

For the internal server redirect, your browser should be directed the route that returns the hello message, as shown in figure 1. Note that if we open the developers console of the browser (in my case, I’m using Google Chrome), it is possible to check that the “redirect/internal” route will return a 302 status, which corresponds to a redirect.

ESP32 Arduino HTTP server internal redirect.png

Figure 1 – Internal redirect.

For the external redirect, the browser should be directed to the website specified, as shown in figure 2. In this case, we have opened the request information on the developers console in order to confirm that the server is returning a 302 HTTP response, alongside with the destination URL in the response headers.

ESP32 Arduino HTTP server external redirect.png

Figure 2 – External redirect.


Related Posts

Advertisements
This entry was posted in ESP32 and tagged , , , , , , . Bookmark the permalink.

3 Responses to ESP32 Arduino HTTP server: external and internal redirects

  1. yuulye says:

    I will never understand pointer*

    Liked by 1 person

    • antepher says:

      Hi!

      Pointers are indeed a little bit tricky at first, but once you get used to them they will be very useful.

      My recommendation is that you try to understand how they work and start using them to get used to it, since they will give a lot of flexibility to your coding tools.

      There are plenty of online resources about them, my suggestion is that you try to pick a good guide that teaches them from start to finish, since picking up a couple of explanations here and there may be confusing.

      Best regards,
      Nuno Santos

      Like

  2. Pingback: ESP32 Arduino HTTP server: route not found handling | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s