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.
Contents
Required Libraries
You’ll need three libraries for this project, all of which are already included in the ESP8266 core for Arduino.
1 2 3 |
#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:
1 2 |
const char* ssid = "<YOUR WIFI SSID>"; const char* password = "<YOUR WIFI PASSWORD>"; |
Initialize Server Object
Then we create the server object:
1 |
ESP8266WebServer server(80); |
Create Access Point
Inside setup, we initialize the WiFi connection through:
1 |
WiFi.softAP(ssid, password); |
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:
1 |
<h1>WeMos D1 Mini Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a> <a href=\"LEDOff\"><button>OFF</button></a></p> |
We place the HTML inside a string variable index
1 |
String index = "<h1>Simple NodeMCU Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a> <a href=\"LEDOff\"><button>OFF</button></a></p>"; |
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
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); }); |
1 |
server.begin(); |
1 |
server.handleClient(); |
WeMos D1 Mini WiFi Server Full Sketch
Here’s the full code for our WeMos D1 Mini WiFi Server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#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> <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(); } |
Hi,
Thanks for the article. I just want to make sure that, there has to be an existing internet connection already, correct? This router wouldn’t work, for example on a ski slope, correct?
Hi Samuel,
Yeah, a WiFi router would be necessary but it can still work without an Internet connection.