@@ -62,106 +62,106 @@ void handle404(HTTPRequest * req, HTTPResponse * res);
6262void serverTask (void *params);
6363
6464void 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);
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);
8888}
8989
9090void loop () {
91- Serial.println (" loop()" );
92- delay (5000 );
91+ Serial.println (" loop()" );
92+ delay (5000 );
9393}
9494
9595void serverTask (void *params) {
96- // In the separate task we first do everything that we would have done in the
97- // setup() function, if we would run the server synchronously.
98-
99- // Note: The second task has its own stack, so you need to think about where
100- // you create the server's resources and how to make sure that the server
101- // can access everything it needs to access. Also make sure that concurrent
102- // access is no problem in your sketch or implement countermeasures like locks
103- // or mutexes.
104-
105- // Create nodes
106- ResourceNode * nodeRoot = new ResourceNode (" /" , " GET" , &handleRoot);
107- ResourceNode * node404 = new ResourceNode (" " , " GET" , &handle404);
108-
109- // Add nodes to the server
110- secureServer.registerNode (nodeRoot);
111- secureServer.setDefaultNode (node404);
112-
113- Serial.println (" Starting server..." );
114- secureServer.start ();
115- if (secureServer.isRunning ()) {
116- Serial.println (" Server ready." );
117-
118- // "loop()" function of the separate task
119- while (true ) {
120- // This call will let the server do its work
121- secureServer.loop ();
122-
123- // Other code would go here...
124- delay (1 );
125- }
126- }
96+ // In the separate task we first do everything that we would have done in the
97+ // setup() function, if we would run the server synchronously.
98+
99+ // Note: The second task has its own stack, so you need to think about where
100+ // you create the server's resources and how to make sure that the server
101+ // can access everything it needs to access. Also make sure that concurrent
102+ // access is no problem in your sketch or implement countermeasures like locks
103+ // or mutexes.
104+
105+ // Create nodes
106+ ResourceNode * nodeRoot = new ResourceNode (" /" , " GET" , &handleRoot);
107+ ResourceNode * node404 = new ResourceNode (" " , " GET" , &handle404);
108+
109+ // Add nodes to the server
110+ secureServer.registerNode (nodeRoot);
111+ secureServer.setDefaultNode (node404);
112+
113+ Serial.println (" Starting server..." );
114+ secureServer.start ();
115+ if (secureServer.isRunning ()) {
116+ Serial.println (" Server ready." );
117+
118+ // "loop()" function of the separate task
119+ while (true ) {
120+ // This call will let the server do its work
121+ secureServer.loop ();
122+
123+ // Other code would go here...
124+ delay (1 );
125+ }
126+ }
127127}
128128
129129void handleRoot (HTTPRequest * req, HTTPResponse * res) {
130- // Status code is 200 OK by default.
131- // We want to deliver a simple HTML page, so we send a corresponding content type:
132- res->setHeader (" Content-Type" , " text/html" );
133-
134- // The response implements the Print interface, so you can use it just like
135- // you would write to Serial etc.
136- res->println (" <!DOCTYPE html>" );
137- res->println (" <html>" );
138- res->println (" <head><title>Hello World!</title></head>" );
139- res->println (" <body>" );
140- res->println (" <h1>Hello World!</h1>" );
141- res->print (" <p>Your server is running for " );
142- // A bit of dynamic data: Show the uptime
143- res->print ((int )(millis ()/1000 ), DEC);
144- res->println (" seconds.</p>" );
145- res->println (" </body>" );
146- res->println (" </html>" );
130+ // Status code is 200 OK by default.
131+ // We want to deliver a simple HTML page, so we send a corresponding content type:
132+ res->setHeader (" Content-Type" , " text/html" );
133+
134+ // The response implements the Print interface, so you can use it just like
135+ // you would write to Serial etc.
136+ res->println (" <!DOCTYPE html>" );
137+ res->println (" <html>" );
138+ res->println (" <head><title>Hello World!</title></head>" );
139+ res->println (" <body>" );
140+ res->println (" <h1>Hello World!</h1>" );
141+ res->print (" <p>Your server is running for " );
142+ // A bit of dynamic data: Show the uptime
143+ res->print ((int )(millis ()/1000 ), DEC);
144+ res->println (" seconds.</p>" );
145+ res->println (" </body>" );
146+ res->println (" </html>" );
147147}
148148
149149void handle404 (HTTPRequest * req, HTTPResponse * res) {
150- // Discard request body, if we received any
151- // We do this, as this is the default node and may also server POST/PUT requests
152- req->discardRequestBody ();
153-
154- // Set the response status
155- res->setStatusCode (404 );
156- res->setStatusText (" Not Found" );
157-
158- // Set content type of the response
159- res->setHeader (" Content-Type" , " text/html" );
160-
161- // Write a tiny HTTP page
162- res->println (" <!DOCTYPE html>" );
163- res->println (" <html>" );
164- res->println (" <head><title>Not Found</title></head>" );
165- res->println (" <body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>" );
166- res->println (" </html>" );
150+ // Discard request body, if we received any
151+ // We do this, as this is the default node and may also server POST/PUT requests
152+ req->discardRequestBody ();
153+
154+ // Set the response status
155+ res->setStatusCode (404 );
156+ res->setStatusText (" Not Found" );
157+
158+ // Set content type of the response
159+ res->setHeader (" Content-Type" , " text/html" );
160+
161+ // Write a tiny HTTP page
162+ res->println (" <!DOCTYPE html>" );
163+ res->println (" <html>" );
164+ res->println (" <head><title>Not Found</title></head>" );
165+ res->println (" <body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>" );
166+ res->println (" </html>" );
167167}
0 commit comments