-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncWebServerTLS.ino
79 lines (63 loc) · 1.76 KB
/
AsyncWebServerTLS.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
AsyncWebServer server(443);
const char* ssid = "your-ssid";
const char* password = "your-password";
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
int fileRequest(void * arg, const char *filename, uint8_t **buf) {
Serial.printf("Reading file: %s\n", filename);
if(SPIFFS.begin()) {
Serial.println("SPIFFS has started");
}
File file = SPIFFS.open(filename, "r");
if(file){
Serial.printf("File found: %s\n", filename);
size_t size = file.size();
uint8_t * nbuf = (uint8_t*)malloc(size);
if(nbuf){
size = file.read(nbuf, size);
file.close();
*buf = nbuf;
return size;
}
file.close();
} else {
Serial.println("File not found!");
*buf = 0;
return 0;
}
}
void handleTest(AsyncWebServerRequest *request) {
String message = "";
if(request->hasParam("message")) {
message = request->getParam("message")->value();
}
request->send(200, "text/plain", "Param: " + message);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", "Hello, world");
});
server.on("/test", HTTP_GET, handleTest);
server.onNotFound(notFound);
server.onSslFileRequest(fileRequest, NULL);
Serial.println( "Certs validation" );
server.beginSecure("/Cert.pem", "/Key.pem", NULL);
Serial.println( "HTTPs server has started" );
}
void loop() {
}