55 lines
1.1 KiB
Arduino
55 lines
1.1 KiB
Arduino
// https://techtutorialsx.com/2016/10/03/esp8266-setting-a-simple-http-webserver/
|
|
|
|
#include "ESP8266WiFi.h"
|
|
#include "ESP8266WebServer.h"
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
connectWifi();
|
|
|
|
server.on("/other", []() { //Define the handling function for the path
|
|
|
|
server.send(200, "text / plain", "Other URL");
|
|
|
|
});
|
|
|
|
server.on("/", handleRootPath); // Associate the handler function to the path
|
|
server.begin();
|
|
Serial.println("Server listening");
|
|
|
|
}
|
|
|
|
void loop() {
|
|
if ((WiFi.status() == WL_CONNECTED)) {
|
|
server.handleClient();
|
|
}
|
|
else {
|
|
Serial.println("WiFi not connected!");
|
|
connectWifi();
|
|
}
|
|
|
|
}
|
|
|
|
void handleRootPath() { //Handler for the rooth path
|
|
|
|
server.send(200, "text/plain", "Hello world");
|
|
|
|
}
|
|
|
|
void connectWifi() {
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("WiFi connected.");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
} |