Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 566acfb

Browse files
r89mme-no-dev
authored andcommitted
Add SimpleServer example to show how to GET and POST and use parameters (me-no-dev#341)
1 parent b6b43d3 commit 566acfb

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// A simple server implementation showing how to:
3+
// * serve static messages
4+
// * read GET and POST parameters
5+
// * handle missing pages / 404s
6+
//
7+
8+
#include <Arduino.h>
9+
#include <ESP8266WiFi.h>
10+
#include <Hash.h>
11+
#include <ESPAsyncTCP.h>
12+
#include <ESPAsyncWebServer.h>
13+
14+
AsyncWebServer server(80);
15+
16+
const char* ssid = "YOUR_SSID";
17+
const char* password = "YOUR_PASSWORD";
18+
19+
const char* PARAM_MESSAGE = "message";
20+
21+
void notFound(AsyncWebServerRequest *request) {
22+
request->send(404, "text/plain", "Not found");
23+
}
24+
25+
void setup() {
26+
27+
Serial.begin(115200);
28+
WiFi.mode(WIFI_STA);
29+
WiFi.begin(ssid, password);
30+
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
31+
Serial.printf("WiFi Failed!\n");
32+
return;
33+
}
34+
35+
Serial.print("IP Address: ");
36+
Serial.println(WiFi.localIP());
37+
Serial.print("Hostname: ");
38+
Serial.println(WiFi.hostname());
39+
40+
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
41+
request->send(200, "text/plain", "Hello, world");
42+
});
43+
44+
// Send a GET request to <IP>/get?message=<message>
45+
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
46+
String message;
47+
if (request->hasParam(PARAM_MESSAGE)) {
48+
message = request->getParam(PARAM_MESSAGE)->value();
49+
} else {
50+
message = "No message sent";
51+
}
52+
request->send(200, "text/plain", "Hello, GET: " + message);
53+
});
54+
55+
// Send a POST request to <IP>/post with a form field message set to <message>
56+
server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request){
57+
String message;
58+
if (request->hasParam(PARAM_MESSAGE, true)) {
59+
message = request->getParam(PARAM_MESSAGE, true)->value();
60+
} else {
61+
message = "No message sent";
62+
}
63+
request->send(200, "text/plain", "Hello, POST: " + message);
64+
});
65+
66+
server.onNotFound(notFound);
67+
68+
server.begin();
69+
}
70+
71+
void loop() {
72+
}

0 commit comments

Comments
 (0)