Language: EN

como-servir-contenido-dinamico-desde-el-esp8266

How to serve dynamic content from ESP8266 or ESP32

We continue with the tutorials of the ESP8266 and ESP32 seeing a server that returns information that changes dynamically with each request.

We will refer to the ESP8266, but the same code is compatible for the ESP32, adjusting the names of the libraries. At the end you have the code for both the ESP8266 and the ESP32.

Previously we saw how to set up a basic server with an ESP8266, and in the previous post we saw how to distinguish between types of calls and receive arguments.

So far, we were returning “more or less” static content, that is, always the same. But on many occasions we will want to return content that varies, such as the state of a variable, or an analog or digital input, the measurement of a sensor.

In a real project we will normally serve certain static content (typically html) and this will be updated through some mechanism (Ajax, Websocket) making requests to certain Endpoints. We will see all this in due course.

In this post we are going to learn to serve content that varies dynamically. To do this, we start from the server program of the previous example

Here we have only added a ‘ledStatus’ variable that represents any variable that we would like to return

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

bool ledStatus = false;  // Example variable

#include "config.h"  // Replace with your network data
#include "Server.hpp"
#include "ESP8266_Utils.hpp"

void setup(void) 
{
   Serial.begin(115200);

   ConnectWiFi_STA();
   
   InitServer();
}
 
void loop()
{
   server.handleClient();
}

On the other hand, in the ‘Server.hpp’ file we have defined a single route from the root ’/‘. In the response we generate a String that contains the text we want. In this example, we only return ‘LED: ON’ or ‘LED: OFF’, depending on the state of the variable.

ESP8266WebServer server(80);

void handleRoot() {
   String response = "LED: ";
   response.concat(ledStatus ? "OFF" : "ON");
  
   server.send(200, "text/plain", response);
}

void handleNotFound() {
  server.send(404, "text/plain", "Not found");
}

void InitServer()
{
   server.on("/", handleRoot);
   server.onNotFound(handleNotFound);
   server.begin();
   Serial.println("HTTP server started");
}

Result

If we run the code we will see that, indeed, we are returning the dynamic content correctly. That easy!

esp8266-contenido-dinamico

Of course, this is a very simple example. But it is worth learning it very well because, together with the previous post, it forms the basis for setting up our own API.

The main problem with this approach is that it represents a load for the processor because it has to generate the information on each request, and that the treatment of the response Strings can take up a lot of memory if it is long.

In the next post we will see how to improve this by serving the content from Flash memory, and in the next post we will see how to use the available file systems (SPIFFS and LittleFS) to serve static content. See you soon!

Download the code

All the code in this post is available for download on Github.

github-full

Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples

Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples