WeMos D1 Mini WiFi Server

WeMos D1 Mini
Now that you've set up your WeMos D1 Mini to be programmable using the Arduino IDE, it's time to build a project. I'm a huge believer in active learning and project-making does just that. For this tutorial, I will guide you on how to build a WeMos D1 Mini WiFi server.

There are two ways for the WeMos D1 Mini to host a server: as a station and as an access point. I've already shown a tutorial on the former using NodeMCU so I will now use an access point server instead. Basically, an access point server doesn't need another WiFi router - the WeMos D1 mini itself is the router. It broadcasts a WiFi connection and the user connects to it through SSID and password.

Required Libraries

You'll need three libraries for this project, all of which are already included in the ESP8266 core for Arduino.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

Define Access Point SSID and Password

Next, we define the SSID and password for the WiFi connection:

const char* ssid = "<YOUR WIFI SSID>";
const char* password = "<YOUR WIFI PASSWORD>";

Initialize Server Object

Then we create the server object:

ESP8266WebServer server(80);

Create Access Point

Inside setup, we initialize the WiFi connection through:

WiFi.softAP(ssid, password);

This should start the access point, making the SSID visible to would-be clients.

Write HTML Page

Now we need to create an interface for the client. For starters, we'll build a web page that allows the user to control the on-board LED on the WeMos D1. For this, we create a page with the following HTML:
<h1>WeMos D1 Mini Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a>&nbsp;<a href=\"LEDOff\"><button>OFF</button></a></p>

This is a very simple HTML and could be improved with CSS and Javascript. For the purpose of this tutorial, we will stick with this one.

We place the HTML inside a string variable index
String index = "<h1>Simple NodeMCU Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a>&nbsp;<a href=\"LEDOff\"><button>OFF</button></a></p>";

And have it shown to the client who visits the home page:

server.on("/", [](){
    server.send(200, "text/html", index);
});

Bind to LED Control

Notice that on the HTML page, we have links to two pages: /LEDOn and /LEDOff.

When the client visits these links, that is when we are supposed to turn on or off the on board LED:
server.on("/LEDOn", [](){
    server.send(200, "text/html", index);
    digitalWrite(13, HIGH);
    delay(1000);
});

server.on("/LEDOff", [](){
    server.send(200, "text/html", index);
    digitalWrite(13, LOW);
    delay(1000);
});

Finally, we start the server by calling the begin function.

server.begin();

The above code needs to be executed only once and hence will be inside setup(). The loop() function will only have one line of code and that is:

server.handleClient();

This makes the WeMos D1 Mini always listening to clients that visit the server.

WeMos D1 Mini WiFi Server Full Sketch

Here's the full code for our WeMos D1 Mini WiFi Server:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>


// Replace with your network credentials
const char* ssid = "<YOUR WIFI SSID>";
const char* password = "<YOUR WIFI PASSWORD>";
ESP8266WebServer server(80);   //instantiate server at port 80 (http port)


void setup(void){

  //the HTML of the web page
  String index = "<h1>Simple NodeMCU Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a>&nbsp;<a href=\"LEDOff\"><button>OFF</button></a></p>";
  //make the LED pin output and initially turned off
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  
  delay(1000);
  Serial.begin(115200);
  WiFi.softAP(ssid, password); //begin WiFi access point
  Serial.println("");
  
  server.on("/", [](){
    server.send(200, "text/html", index);
  });
  server.on("/LEDOn", [](){
    server.send(200, "text/html", index);
    digitalWrite(13, HIGH);
    delay(1000);
  });
  server.on("/LEDOff", [](){
    server.send(200, "text/html", index);
    digitalWrite(13, LOW);
    delay(1000);
  });
  server.begin();
  Serial.println("Web server started!");
}
void loop(void){
  server.handleClient();
}

Upload the code above to your WeMos D1 Mini. If successful, the device should be visible as a WiFi access point with the SSID and password you specified in the code above.

Leave a Reply

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