ESP32 Arduino web server: Get free heap and reset device remotely

In this ESP32 tutorial we will check how to remotely obtain the device’s free heap and how to reset it. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

Introduction

In this ESP32 tutorial we will check how to remotely obtain the device’s free heap and how to reset it. This will be done on the Arduino core and using the async HTTP web server libraries, which were introduced in this tutorial.

For a detailed explanation on how to obtain the free heap of the ESP32, please check here. For a tutorial on how to perform a software reset on the ESP32, please consult this post.

The objective of this guide is to show how we can leverage the HTTP server libraries to create an API that can be used, for example, to a technical user to remotely diagnose problems in the ESP32. Naturally, this is a very simple example that can be further enhanced for that purpose.

If you prefer a video tutorial, please check below my YouTube channel.

The code

As mentioned in the introductory section, this tutorial is built on top of previous examples and thus the code will be very similar.

In the beginning of the code, we include all the libraries needed for setting up the server. We don’t need any additional includes for getting the free heap or resetting the device since these functionalities are available by default on the Arduino core.

As usual we will also need the WiFi credentials for connecting the ESP32 to the network, so it can be reached by the client.

To finish this section of the code, we need an object of class AsyncWebServer, which is used to setup the server. Remember that its constructor receives as input the port where, the server will be listening for requests. We will pass to it the value 80, which is the default HTTP port.

#include "WiFi.h"
#include "ESPAsyncWebServer.h"

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

AsyncWebServer server(80);

Next we will write the Arduino setup function, which is where we will configure the server and specify its routes.

But first we will open a serial connection, to output some information from our program.

After this we will connect the ESP to the wireless network and in the end print its local IP. This IP will later be used to allow the client to connect to the server.

Serial.begin(115200);

WiFi.begin(ssid, password);

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

Serial.println(WiFi.localIP());

Next we need to specify the routes for our server. The first one, which we will call “/heap”, will listen to HTTP GET requests and return the output value as plain text.

Remember from the previous post that we can obtain the free heap by calling the getFreeHeap method on the ESP object, which is available by default without the need for extra includes.

We will call the mentioned method on the route handling function and return the output to the client.

server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", String(ESP.getFreeHeap()));
});

Next we need to specify the route that resets the device, which we will call “/reset”. Since this is a command rather that a query, we will be listening for POST requests in this route. You can check here how to control the HTTP methods allowed on a given route.

As explained in the previous article, we can perform a software reset on the ESP32 with a call to the restart method on the same ESP object.

Note however that to avoid leaving our client hanging, we first call the send method of the request object to return the response to the request and only after we reset the device.

server.on("/reset", HTTP_POST, [](AsyncWebServerRequest *request){
    request->send(200,"text/plain","ok");
    delay(2000);
    ESP.restart();
});

To finalize the code, we call the begin method on the server object, so it becomes ready for receiving the request. The final source code can be seen below.

#include "WiFi.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("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", String(ESP.getFreeHeap()) );
  });

  server.on("/reset", HTTP_POST, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain","ok");
    delay(2000);
    ESP.restart();
  });

  server.begin();
}

void loop(){}

Testing the code

To test the previous code, first compile and upload it using the Arduino IDE. Next, open the serial monitor and copy the IP that gets printed once the connection to the WiFi network is established. We will use that IP to contact the server from a web browser, which will act as client.

So, open a web browser at your choice and paste the following, changing #yourDeviceIp# by the IP you just copied.

http://#yourDeviceIp#/heap

You should get, an output similar to figure 1, which illustrates the result of our command. As expected, it returned the free heap of the ESP32 as plain text (value in bytes).

ESP32 Arduino web server get heap remotly.png

Figure 1 – Getting the free heap.

Now, to test the reset functionality, we need to remember that the server is listening for HTTP POST methods. Thus, we will use Postman to make the request on the following route:

http://#yourDeviceIp#/reset

Note that although this is a POST method, we don’t need to specify any body for the request, since no parameters are needed to execute the command.

So, in Postman, select POST from the methods dropdown, fill the URL field with the server endpoint and click the send button. You should get an output similar to figure 2, which shows the “ok” message being returned.

ESP32 Arduino reset remote.png

Figure 2 – Remotely resetting the ESP32.

Note that after this request, if you go back to the serial monitor, the device should have restarted, which can be seen by the initial messages that the ESP32 outputs upon starting and the IP that we are printing in our code.

 

6 thoughts on “ESP32 Arduino web server: Get free heap and reset device remotely”

  1. Pingback: ESP32 Arduino web server: getting client IP | techtutorialsx

  2. Pingback: ESP32 Arduino web server: getting client IP | techtutorialsx

  3. Pingback: ESP32 Arduino: Temperature, humidity and CO2 concentration web server | techtutorialsx

  4. Pingback: ESP32 Arduino: Temperature, humidity and CO2 concentration web server | techtutorialsx

  5. Pingback: ESP32 Arduino web server: Add header to response | techtutorialsx

  6. Pingback: ESP32 Arduino web server: Add header to response | techtutorialsx

  7. Pingback: ESP32 Async HTTP web server: websockets introduction | techtutorialsx

  8. Pingback: ESP32 Async HTTP web server: websockets introduction | techtutorialsx

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading