Skip to content

Commit dbbd047

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
Updated HelloServer with mDNS and NotFound catcher with custom page
the not found handler should be used in conjunction with other means of getting data, like SD card
1 parent 81af3a0 commit dbbd047

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
#include <ESP8266WiFi.h>
22
#include <WiFiClient.h>
33
#include <ESP8266WebServer.h>
4+
#include <ESP8266mDNS.h>
45

5-
const char* ssid = "...............";
6-
const char* password = "...............";
7-
6+
const char* ssid = "........";
7+
const char* password = "........";
8+
MDNSResponder mdns;
89

910
ESP8266WebServer server(80);
10-
11-
const int led = 13;
12-
11+
1312
void handle_root() {
14-
digitalWrite(led, 1);
1513
server.send(200, "text/plain", "hello from esp8266!");
16-
delay(100);
17-
digitalWrite(led, 0);
14+
}
15+
16+
bool handle_not_found(){
17+
String message = "URI: ";
18+
message += server.uri();
19+
message += "\nMethod: ";
20+
message += (server.method() == HTTP_GET)?"GET":"POST";
21+
message += "\nArguments: ";
22+
message += server.args();
23+
message += "\n";
24+
for (uint8_t i=0; i<server.args(); i++){
25+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
26+
}
27+
message += "\nNotFound!";
28+
server.send(404, "text/plain", message);
29+
return true;
1830
}
1931

20-
void setup(void)
21-
{
32+
void setup(void){
2233
Serial.begin(115200);
23-
pinMode(led, OUTPUT);
24-
digitalWrite(led, 0);
25-
26-
// Connect to WiFi network
2734
WiFi.begin(ssid, password);
2835
Serial.println("");
2936

@@ -37,18 +44,22 @@ void setup(void)
3744
Serial.println(ssid);
3845
Serial.print("IP address: ");
3946
Serial.println(WiFi.localIP());
40-
47+
48+
if (mdns.begin("esp8266", WiFi.localIP())) Serial.println("MDNS responder started");
49+
4150
server.on("/", handle_root);
4251

4352
server.on("/inline", [](){
4453
server.send(200, "text/plain", "this works as well");
4554
});
55+
56+
server.onNotFound(handle_not_found);
4657

4758
server.begin();
4859
Serial.println("HTTP server started");
4960
}
5061

51-
void loop(void)
52-
{
62+
void loop(void){
63+
mdns.update();
5364
server.handleClient();
5465
}

0 commit comments

Comments
 (0)