
* ********************************** PA3BYB ******************************************
*
* Simple Webserver with the (LoLin)WeMos D1 ESP8266 based board
* Test of the Builtin-led with the command BUILTIN_LED
*
* Date : 2021 11 20
* Version : 0.1
*
* Connect GND>GND on ESP; RTS>3.3 v on ESP; RxD> any digital pin on ESP (bv 5)
******************************************************************************************
* The board has a inbuilt LED using digital pin-5/13. The LED glows when the pin becomes high.
* The LED on 13 (5) can be controlled on two diffent ways. This means by:
* command – pinMode(BUILTIN_LED, OUTPUT); This makes pin to output
* command – digitalWrite(BUILTIN_LED, LOW); or HIGH
* Or
* Constant – const int led = 13 (or 5)
* Command – pinMode(led, OUTPUT);
* Command – digitalWrite(led, 0);
* Command – digitalWrite(led, 1);
*/
The sketch
//================ Including Bibliotheken =============================
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//================ Define Variable ==================================
const char* ssid = ""; // Own SIDD
const char* password = ""; // Own Password
//================ Initialisation Server ===============================
ESP8266WebServer server(80); // Web on port 80
//=============== Start SetUp ======================================
void setup(void){
pinMode(BUILTIN_LED, OUTPUT); // Pin 5/13 Built in led output)
digitalWrite(BUILTIN_LED, LOW); // Led On
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Open serial Monitor for control
Serial.println("");
Serial.print(" Connected to ");
Serial.println(ssid);
Serial.print(" IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("****** MDNS responder started *******");
}
server.on("/", handleRoot); // Jump to subroutine handleRoot
server.on("/inline", [](){
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound); // Jump to subroutine
server.begin();
Serial.println("******* HTTP server started ********");
} // End SetUp
//============== Program Loop =====================================
void loop(void){
server.handleClient();
}
//============== Subroutines =======================================
void handleRoot() {
digitalWrite(BUILTIN_LED, HIGH);
server.send(200, "text/plain", "Simple Webserver testprogram with the WeMos D1");
digitalWrite(BUILTIN_LED, LOW);
}
void handleNotFound(){
digitalWrite(BUILTIN_LED, LOW);
String message = "****** File Not Foundnn ********";
message += "URI: ";
message += server.uri();
message += "nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "nArguments: ";
message += server.args();
message += "n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "n";
}
server.send(404, "text/plain", message);
digitalWrite(BUILTIN_LED, HIGH); // Led no light
}
