11/* *
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+ */
1919
2020// 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 "
2323
2424/* * Check if we have multiple cores */
2525#if CONFIG_FREERTOS_UNICORE
26- #define ARDUINO_RUNNING_CORE 0
26+ #define ARDUINO_RUNNING_CORE 0
2727#else
28- #define ARDUINO_RUNNING_CORE 1
28+ #define ARDUINO_RUNNING_CORE 1
2929#endif
3030
3131// Include certificate data (see note above)
@@ -46,53 +46,15 @@ using namespace httpsserver;
4646
4747// Create an SSL certificate object from the files included above
4848SSLCert 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+ );
5252
5353// Create an SSL-enabled server that uses the certificate
5454HTTPSServer secureServer = HTTPSServer(&cert);
5555
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+ {
9658 // In the separate task we first do everything that we would have done in the
9759 // setup() function, if we would run the server synchronously.
9860
@@ -112,11 +74,14 @@ void serverTask(void *params) {
11274
11375 Serial.println (" Starting server..." );
11476 secureServer.start ();
115- if (secureServer.isRunning ()) {
77+
78+ if (secureServer.isRunning ())
79+ {
11680 Serial.println (" Server ready." );
11781
11882 // "loop()" function of the separate task
119- while (true ) {
83+ while (true )
84+ {
12085 // This call will let the server do its work
12186 secureServer.loop ();
12287
@@ -126,7 +91,8 @@ void serverTask(void *params) {
12691 }
12792}
12893
129- void handleRoot (HTTPRequest * req, HTTPResponse * res) {
94+ void handleRoot (HTTPRequest * req, HTTPResponse * res)
95+ {
13096 // Status code is 200 OK by default.
13197 // We want to deliver a simple HTML page, so we send a corresponding content type:
13298 res->setHeader (" Content-Type" , " text/html" );
@@ -140,13 +106,14 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) {
140106 res->println (" <h1>Hello World!</h1>" );
141107 res->print (" <p>Your server is running for " );
142108 // A bit of dynamic data: Show the uptime
143- res->print ((int )(millis ()/ 1000 ), DEC);
109+ res->print ((int )(millis () / 1000 ), DEC);
144110 res->println (" seconds.</p>" );
145111 res->println (" </body>" );
146112 res->println (" </html>" );
147113}
148114
149- void handle404 (HTTPRequest * req, HTTPResponse * res) {
115+ void handle404 (HTTPRequest * req, HTTPResponse * res)
116+ {
150117 // Discard request body, if we received any
151118 // We do this, as this is the default node and may also server POST/PUT requests
152119 req->discardRequestBody ();
@@ -165,3 +132,44 @@ void handle404(HTTPRequest * req, HTTPResponse * res) {
165132 res->println (" <body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>" );
166133 res->println (" </html>" );
167134}
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