1
1
/* *
2
- * Example for the ESP32 HTTP(S) Webserver
3
- *
4
- * IMPORTANT NOTE:
5
- * To run this script, your need to
6
- * 1) Enter your WiFi SSID and PSK below this comment
7
- * 2) Make sure to have certificate data available. You will find a
8
- * shell script and instructions to do so in the library folder
9
- * under extras/
10
- *
11
- * This script will install an HTTPS Server on your ESP32 with the following
12
- * functionalities:
13
- * - Show simple page on web server root
14
- * - 404 for everything else
15
- * The server will be run in a separate task, so that you can do your own stuff
16
- * in the loop() function.
17
- * Everything else is just like the Static-Page example
18
- */
2
+ Example for the ESP32 HTTP(S) Webserver
3
+
4
+ IMPORTANT NOTE:
5
+ To run this script, your need to
6
+ 1) Enter your WiFi SSID and PSK below this comment
7
+ 2) Make sure to have certificate data available. You will find a
8
+ shell script and instructions to do so in the library folder
9
+ under extras/
10
+
11
+ This script will install an HTTPS Server on your ESP32 with the following
12
+ functionalities:
13
+ - Show simple page on web server root
14
+ - 404 for everything else
15
+ The server will be run in a separate task, so that you can do your own stuff
16
+ in the loop() function.
17
+ Everything else is just like the Static-Page example
18
+ */
19
19
20
20
// TODO: Configure your WiFi here
21
- #define WIFI_SSID " <your ssid goes here> "
22
- #define WIFI_PSK " <your pre-shared key goes here> "
21
+ #define WIFI_SSID " your_ssid "
22
+ #define WIFI_PSK " 12345678 "
23
23
24
24
/* * Check if we have multiple cores */
25
25
#if CONFIG_FREERTOS_UNICORE
26
- #define ARDUINO_RUNNING_CORE 0
26
+ #define ARDUINO_RUNNING_CORE 0
27
27
#else
28
- #define ARDUINO_RUNNING_CORE 1
28
+ #define ARDUINO_RUNNING_CORE 1
29
29
#endif
30
30
31
31
// Include certificate data (see note above)
@@ -46,53 +46,15 @@ using namespace httpsserver;
46
46
47
47
// Create an SSL certificate object from the files included above
48
48
SSLCert cert = SSLCert(
49
- example_crt_DER, example_crt_DER_len,
50
- example_key_DER, example_key_DER_len
51
- );
49
+ example_crt_DER, example_crt_DER_len,
50
+ example_key_DER, example_key_DER_len
51
+ );
52
52
53
53
// Create an SSL-enabled server that uses the certificate
54
54
HTTPSServer secureServer = HTTPSServer(&cert);
55
55
56
- // Declare some handler functions for the various URLs on the server
57
- void handleRoot (HTTPRequest * req, HTTPResponse * res);
58
- void handle404 (HTTPRequest * req, HTTPResponse * res);
59
-
60
- // We declare a function that will be the entry-point for the task that is going to be
61
- // created.
62
- void serverTask (void *params);
63
-
64
- void setup () {
65
- // For logging
66
- Serial.begin (115200 );
67
-
68
- // Connect to WiFi
69
- Serial.println (" Setting up WiFi" );
70
- WiFi.begin (WIFI_SSID, WIFI_PSK);
71
- while (WiFi.status () != WL_CONNECTED) {
72
- Serial.print (" ." );
73
- delay (500 );
74
- }
75
- Serial.print (" Connected. IP=" );
76
- Serial.println (WiFi.localIP ());
77
-
78
- // Setup the server as a separate task.
79
- Serial.println (" Creating server task... " );
80
- // We pass:
81
- // serverTask - the function that should be run as separate task
82
- // "https443" - a name for the task (mainly used for logging)
83
- // 6144 - stack size in byte. If you want up to four clients, you should
84
- // not go below 6kB. If your stack is too small, you will encounter
85
- // Panic and stack canary exceptions, usually during the call to
86
- // SSL_accept.
87
- xTaskCreatePinnedToCore (serverTask, " https443" , 6144 , NULL , 1 , NULL , ARDUINO_RUNNING_CORE);
88
- }
89
-
90
- void loop () {
91
- Serial.println (" loop()" );
92
- delay (5000 );
93
- }
94
-
95
- void serverTask (void *params) {
56
+ void serverTask (void *params)
57
+ {
96
58
// In the separate task we first do everything that we would have done in the
97
59
// setup() function, if we would run the server synchronously.
98
60
@@ -112,11 +74,14 @@ void serverTask(void *params) {
112
74
113
75
Serial.println (" Starting server..." );
114
76
secureServer.start ();
115
- if (secureServer.isRunning ()) {
77
+
78
+ if (secureServer.isRunning ())
79
+ {
116
80
Serial.println (" Server ready." );
117
81
118
82
// "loop()" function of the separate task
119
- while (true ) {
83
+ while (true )
84
+ {
120
85
// This call will let the server do its work
121
86
secureServer.loop ();
122
87
@@ -126,7 +91,8 @@ void serverTask(void *params) {
126
91
}
127
92
}
128
93
129
- void handleRoot (HTTPRequest * req, HTTPResponse * res) {
94
+ void handleRoot (HTTPRequest * req, HTTPResponse * res)
95
+ {
130
96
// Status code is 200 OK by default.
131
97
// We want to deliver a simple HTML page, so we send a corresponding content type:
132
98
res->setHeader (" Content-Type" , " text/html" );
@@ -140,13 +106,14 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) {
140
106
res->println (" <h1>Hello World!</h1>" );
141
107
res->print (" <p>Your server is running for " );
142
108
// A bit of dynamic data: Show the uptime
143
- res->print ((int )(millis ()/ 1000 ), DEC);
109
+ res->print ((int )(millis () / 1000 ), DEC);
144
110
res->println (" seconds.</p>" );
145
111
res->println (" </body>" );
146
112
res->println (" </html>" );
147
113
}
148
114
149
- void handle404 (HTTPRequest * req, HTTPResponse * res) {
115
+ void handle404 (HTTPRequest * req, HTTPResponse * res)
116
+ {
150
117
// Discard request body, if we received any
151
118
// We do this, as this is the default node and may also server POST/PUT requests
152
119
req->discardRequestBody ();
@@ -165,3 +132,44 @@ void handle404(HTTPRequest * req, HTTPResponse * res) {
165
132
res->println (" <body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>" );
166
133
res->println (" </html>" );
167
134
}
135
+
136
+ void setup ()
137
+ {
138
+ // For logging
139
+ Serial.begin (115200 );
140
+ while (!Serial && millis () < 5000 );
141
+
142
+ // /////////////////////////////////////////////
143
+
144
+ Serial.print (" \n Starting Async_Server on " ); Serial.println (ARDUINO_BOARD);
145
+
146
+ // Connect to WiFi
147
+ Serial.println (" Setting up WiFi" );
148
+ WiFi.begin (WIFI_SSID, WIFI_PSK);
149
+
150
+ while (WiFi.status () != WL_CONNECTED)
151
+ {
152
+ Serial.print (" ." );
153
+ delay (500 );
154
+ }
155
+
156
+ Serial.print (" Connected. IP=" );
157
+ Serial.println (WiFi.localIP ());
158
+
159
+ // Setup the server as a separate task.
160
+ Serial.println (" Creating server task... " );
161
+ // We pass:
162
+ // serverTask - the function that should be run as separate task
163
+ // "https443" - a name for the task (mainly used for logging)
164
+ // 6144 - stack size in byte. If you want up to four clients, you should
165
+ // not go below 6kB. If your stack is too small, you will encounter
166
+ // Panic and stack canary exceptions, usually during the call to
167
+ // SSL_accept.
168
+ xTaskCreatePinnedToCore (serverTask, " https443" , 6144 , NULL , 1 , NULL , ARDUINO_RUNNING_CORE);
169
+ }
170
+
171
+ void loop ()
172
+ {
173
+ Serial.println (" loop()" );
174
+ delay (5000 );
175
+ }
0 commit comments