Build a simple HTTP Webserver on your ESP8266

In this post, we’ll create a minimal web server using the ESP8266WiFi and ESP8266WebServer libraries. These libraries make it easy to create a simple web server that can be accessed over the network.

The code

Here’s the code for a basic web server:

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

const char* ssid = "your_SSID_here";
const char* password = "your_PASSWORD_here";

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/plain", "Hello World!");
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

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

Let’s break it down

The code is relatively simple. We start by including the required libraries and defining the SSID and password for our Wi-Fi network:

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

We define the WebServer instance specifying the HTTP port he will listen on:

ESP8266WebServer server(80);

In the setup function, we initialize the serial communication, connect to the Wi-Fi network, and set up the server to handle the root URL. The handleRoot() function is responsible to manage the incoming GET request by sending an HTTP response with “Hello World!” as text and 200 (HTTP OK) status code.

Finally, we start the server and print a message to the serial monitor:

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

To test the web server, upload the code to the ESP8266 development board and open a web browser. Enter the IP address of the board (which can be found in the serial monitor) and you should see the “Hello World!” message.

Handling POST requests

In the following code, we’ll handle POST requests as well:

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

const char* ssid = "your_SSID_here";
const char* password = "your_PASSWORD_here";

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/plain", "Hello World!");
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  server.on("/", HTTP_GET, handleRoot);
  server.on("/", HTTP_POST, []() {
      String message = server.arg("parameter");
      server.send(200, "text/plain", message);
  });
  server.begin();
  Serial.println("HTTP server started");
}

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

We’ve added a bit of code which handles POST requests to the root:

server.on("/", HTTP_POST, []() {
  String message = server.arg("message");
  server.send(200, "text/plain", message);
});

As you may notice, we’re not defining a new callback function as before, but rather we leverage C++ lambdas to handle the request in-place. We extract the message parameter from the body, then echo it back to the client.

Hope it was useful, cheers!


Made with ❤️ by Mikepicker