Skip to content

Instantly share code, notes, and snippets.

@gresan-gits
Last active March 3, 2020 04:55
Show Gist options
  • Save gresan-gits/758dd041aab8088c0ffbef90deb8bbcf to your computer and use it in GitHub Desktop.
Save gresan-gits/758dd041aab8088c0ffbef90deb8bbcf to your computer and use it in GitHub Desktop.
AsyncWebserverRespnse
AsyncResponseStream *response = request->beginResponseStream("text/html");
response->addHeader("Server","ESP Async Web Server");
response->printf("<!DOCTYPE html><html><head><title>Webpage at %s</title></head><body>", request->url().c_str());
response->print("<h2>Hello ");
response->print(request->client()->remoteIP());
response->print("</h2>");
response->print("<h3>General</h3>");
response->print("<ul>");
response->printf("<li>Version: HTTP/1.%u</li>", request->version());
response->printf("<li>Method: %s</li>", request->methodToString());
response->printf("<li>URL: %s</li>", request->url().c_str());
response->printf("<li>Host: %s</li>", request->host().c_str());
response->printf("<li>ContentType: %s</li>", request->contentType().c_str());
response->printf("<li>ContentLength: %u</li>", request->contentLength());
response->printf("<li>Multipart: %s</li>", request->multipart()?"true":"false");
response->print("</ul>");
response->print("<h3>Headers</h3>");
response->print("<ul>");
int headers = request->headers();
for(int i=0;i<headers;i++){
AsyncWebHeader* h = request->getHeader(i);
response->printf("<li>%s: %s</li>", h->name().c_str(), h->value().c_str());
}
response->print("</ul>");
response->print("<h3>Parameters</h3>");
response->print("<ul>");
int params = request->params();
for(int i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isFile()){
response->printf("<li>FILE[%s]: %s, size: %u</li>", p->name().c_str(), p->value().c_str(), p->size());
} else if(p->isPost()){
response->printf("<li>POST[%s]: %s</li>", p->name().c_str(), p->value().c_str());
} else {
response->printf("<li>GET[%s]: %s</li>", p->name().c_str(), p->value().c_str());
}
}
response->print("</ul>");
response->print("</body></html>");
//send the response last
request->send(response);
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
const char* ssid = "Xuong May Tran Bam";
const char* password = "123456789";
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
int paramsNr = request->params();
Serial.println(paramsNr);
for (int i = 0; i < paramsNr; i++) {
AsyncWebParameter* p = request->getParam(i);
Serial.print("Param name: ");
Serial.println(p->name());
Serial.print("Param value: ");
Serial.println(p->value());
Serial.println("------");
}
request->send(200, "text/plain", "message received");
});
server.on("/scan", HTTP_GET, [](AsyncWebServerRequest * request) {
String json = "[";
int n = WiFi.scanComplete();
if (n == -2) {
WiFi.scanNetworks(true);
} else if (n) {
for (int i = 0; i < n; ++i) {
if (i) json += ",";
json += "{";
json += "\"rssi\":" + String(WiFi.RSSI(i));
json += ",\"ssid\":\"" + WiFi.SSID(i) + "\"";
json += ",\"bssid\":\"" + WiFi.BSSIDstr(i) + "\"";
json += ",\"channel\":" + String(WiFi.channel(i));
json += ",\"secure\":" + String(WiFi.encryptionType(i));
json += "}";
}
WiFi.scanDelete();
if (WiFi.scanComplete() == -2) {
WiFi.scanNetworks(true);
}
}
json += "]";
request->send(200, "application/json", json);
json = String();
});
server.begin();
}
void loop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment