Arduino

Super-Tiny Wemos D1 Mini Web Server (Step-by-Step Tutorial)

You may already know that the ubiquitous ESP8266 is designed to connect to WiFi, but did you know it can also act as a web server? In this tutorial we will turn a Wemos D1 Mini into a very tiny web server using just 17 lines of code.

I have read a few very good tutorials that cover more complex implementations of an ESP-based web server. I wanted to offer something a little easier to help you get up and running quicker.

I love the internet

Prerequisite

You will need to have the Arduino IDE installed and running. I would recommend heading over to the website and getting the latest version. You will also need an ESP8266 board, I would recommend the Wemos D1 Mini used in this series of tutorials.

You may also want to first check out my tutorial on how to add ESP devices to the Arduino IDE if you have not yet programmed an ESP device with Arduino.

Writing the code

First we are going to need to open the Arduino IDE and create a new sketch. Go ahead and save it with the filename of your choice.

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Adding the includes

The first lines of code that we need to add to our new project are the includes. We will add the ESP8266WiFi and ESP8266WebServer libraries from the ESP8266 community library.

If you did not add the ESP8266 community library to Arduino, you can see how to do it in this tutorial.

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

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Define WiFi credentials

Next we will define our WiFi credentials. You will need to replace YOUR SSID and YOUR PASSWORD with your own WiFi credentials.

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

// Set WiFi credentials
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Create a new web server

We now need to create a new instances of the ESP8266WebServer. We will call it webserver and assign it to the default port 80.

You can choose any port you like, but web browsers default to 80 if a port is not specified. Therefore it is best to leave this as port 80 unless you have a specific reason to change it.

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

// Set WiFi credentials
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"

// Create a new web server
ESP8266WebServer webserver(80);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Handle the pages

When the web server receives a request it will call a particular function that we can define, depending on the request.

Here we will define a function that is called when a client navigates to the root. We will also define a function to handle a 404 error triggered by an unknown URL being entered.

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

// Set WiFi credentials
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"

// Create a new web server
ESP8266WebServer webserver(80);

// Handle Root
void rootPage() { 
  webserver.send(200, "text/plain", "Check out http://www.siytek.com !"); 
}

// Handle 404
void notfoundPage(){ 
  webserver.send(404, "text/plain", "404: Not found"); 
}

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Begin the serial port

Now we can move on to our setup function. Firstly we will define the serial port in order to receive a message that states the IP address of our web server, once it connects to the network.

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

// Set WiFi credentials
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"

// Create a new web server
ESP8266WebServer webserver(80);

// Handle Root
void rootPage() { 
  webserver.send(200, "text/plain", "Check out http://www.siytek.com !"); 
}

// Handle 404
void notfoundPage(){ 
  webserver.send(404, "text/plain", "404: Not found"); 
}

void setup()
{
  // Setup serial port
  Serial.begin(115200);
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Connect to WiFi

Now we can begin the WiFi connection using the credential definitions specified at the beginning of the program.

After we start the WiFi connection we will put the program into a continuous while loop that iterates until WL_CONNECTED is set. This occurs when the ESP is assigned and IP address.

As soon as the WL_CONNECTED flag is set, the while loop exits and the IP address is printed to the serial console. We can make a note of it so that we can use it to navigate to our hosted web page.

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

// Set WiFi credentials
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"

// Create a new web server
ESP8266WebServer webserver(80);

// Handle Root
void rootPage() { 
  webserver.send(200, "text/plain", "Check out http://www.siytek.com !"); 
}

// Handle 404
void notfoundPage(){ 
  webserver.send(404, "text/plain", "404: Not found"); 
}

void setup()
{
  // Setup serial port
  Serial.begin(115200);
  Serial.println();

  //Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) { delay(100); }

  // WiFi Connected
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

}

void loop() {
  // put your main code here, to run repeatedly:

}

Start the web server

Now we are ready to start our web server! First we will specify the root and 404 error functions that we specified earlier to the webserver instance. Then we can begin the server.

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

// Set WiFi credentials
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"

// Create a new web server
ESP8266WebServer webserver(80);

// Handle Root
void rootPage() { 
  webserver.send(200, "text/plain", "Check out http://www.siytek.com !"); 
}

// Handle 404
void notfoundPage(){ 
  webserver.send(404, "text/plain", "404: Not found"); 
}

void setup()
{
  // Setup serial port
  Serial.begin(115200);
  Serial.println();

  //Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) { delay(100); }

  // WiFi Connected
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

  // Start Web Server
  webserver.on("/", rootPage);
  webserver.onNotFound(notfoundPage);
  webserver.begin();

}

void loop() {
  // put your main code here, to run repeatedly:

}

Listen for HTTP requests

Lastly we need to add a command within the continuous loop function in order to listen for incoming client connections and handle them as necessary.

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

// Set WiFi credentials
#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"

// Create a new web server
ESP8266WebServer webserver(80);

// Handle Root
void rootPage() { 
  webserver.send(200, "text/plain", "Check out http://www.siytek.com !"); 
}

// Handle 404
void notfoundPage(){ 
  webserver.send(404, "text/plain", "404: Not found"); 
}

void setup()
{
  // Setup serial port
  Serial.begin(115200);
  Serial.println();

  //Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) { delay(100); }

  // WiFi Connected
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

  // Start Web Server
  webserver.on("/", rootPage);
  webserver.onNotFound(notfoundPage);
  webserver.begin();

}

// Listen for HTTP requests
void loop(void){ 
  webserver.handleClient(); 
}

Test the web server

We are now ready to test our new super-tiny web server! Go ahead and upload the sketch, then open the serial monitor window. Don’t forget to change the baud rate to 115200 to match what we specified in the program.

Once you have the serial monitor open, press the reset button on the board and wait a few moments. When the ESP establishes a connection to the network, it will print the IP address to the serial window.

Now go ahead and point your browser to http://<your-ip-address> and marvel at your new super-tiny web server!

Conclusion

In this tutorial we have seen that it is possible to build a simple web server in the Arduino IDE using an ESP-based device. The aim of this tutorial was to make the process as simple as possible in order to help you understand the basics.

You should be able to further this example and produce a more complex page using HTML, even including links that control the GPIO pins!

Alternatively if you want to try something a little different, why not check out my tutorial that explains how to use webhooks to send email from an ESP-based device. Also, did you know that you can flash your device over WiFi too? Check out this tutorial to learn how!

Bitmoji Image

Leave a Reply

Your email address will not be published. Required fields are marked *

back to top