Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 804d932

Browse files
authored
v1.2.0 to add ESP32 + LwIP Ethernet
#### Releases v1.2.0 1. Initial coding to port [esp32_https_server](https://github.com/fhessel/esp32_https_server) and [ESP32_HTTPS_Server](https://github.com/khoih-prog/ESP32_HTTPS_Server) to ESP32 boards, WT32_ETH01, (ESP32 + LwIP W5500) and (ESP32 + LwIP ENC28J60) Ethernet 2. Use `allman astyle` and restyle library.
1 parent ea4e34f commit 804d932

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+8182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
*/
19+
20+
// TODO: Configure your WiFi here
21+
#define WIFI_SSID "<your ssid goes here>"
22+
#define WIFI_PSK "<your pre-shared key goes here>"
23+
24+
/** Check if we have multiple cores */
25+
#if CONFIG_FREERTOS_UNICORE
26+
#define ARDUINO_RUNNING_CORE 0
27+
#else
28+
#define ARDUINO_RUNNING_CORE 1
29+
#endif
30+
31+
// Include certificate data (see note above)
32+
#include "cert.h"
33+
#include "private_key.h"
34+
35+
// We will use wifi
36+
#include <WiFi.h>
37+
38+
#include <HTTPS_Server_Generic.h>
39+
40+
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
41+
using namespace httpsserver;
42+
43+
// Create an SSL certificate object from the files included above
44+
SSLCert cert = SSLCert(example_crt_DER, example_crt_DER_len,
45+
example_key_DER, example_key_DER_len);
46+
47+
// Create an SSL-enabled server that uses the certificate
48+
HTTPSServer secureServer = HTTPSServer(&cert);
49+
50+
// Declare some handler functions for the various URLs on the server
51+
void handleRoot(HTTPRequest * req, HTTPResponse * res);
52+
void handle404(HTTPRequest * req, HTTPResponse * res);
53+
54+
// We declare a function that will be the entry-point for the task that is going to be
55+
// created.
56+
void serverTask(void *params);
57+
58+
void setup()
59+
{
60+
// For logging
61+
Serial.begin(115200);
62+
63+
while (!Serial && millis() < 5000);
64+
65+
///////////////////////////////////////////////
66+
67+
Serial.print("\nStarting Async_Server on ");
68+
Serial.println(ARDUINO_BOARD);
69+
Serial.println(HTTPS_SERVER_GENERIC_VERSION);
70+
71+
///////////////////////////////////
72+
73+
// Connect to WiFi
74+
Serial.println("Setting up WiFi");
75+
WiFi.begin(WIFI_SSID, WIFI_PSK);
76+
77+
while (WiFi.status() != WL_CONNECTED)
78+
{
79+
Serial.print(".");
80+
delay(500);
81+
}
82+
83+
///////////////////////////////////
84+
85+
Serial.print(F("HTTPS WiFiWebServer is @ IP : "));
86+
Serial.println(WiFi.localIP());
87+
88+
Serial.print(F("To access, use https://"));
89+
Serial.println(WiFi.localIP());
90+
91+
///////////////////////////////////////////////
92+
93+
// Setup the server as a separate task.
94+
Serial.println("Creating server task... ");
95+
// We pass:
96+
// serverTask - the function that should be run as separate task
97+
// "https443" - a name for the task (mainly used for logging)
98+
// 6144 - stack size in byte. If you want up to four clients, you should
99+
// not go below 6kB. If your stack is too small, you will encounter
100+
// Panic and stack canary exceptions, usually during the call to
101+
// SSL_accept.
102+
xTaskCreatePinnedToCore(serverTask, "https443", 6144, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
103+
}
104+
105+
void loop()
106+
{
107+
Serial.println("loop()");
108+
delay(5000);
109+
}
110+
111+
void serverTask(void *params)
112+
{
113+
// In the separate task we first do everything that we would have done in the
114+
// setup() function, if we would run the server synchronously.
115+
116+
// Note: The second task has its own stack, so you need to think about where
117+
// you create the server's resources and how to make sure that the server
118+
// can access everything it needs to access. Also make sure that concurrent
119+
// access is no problem in your sketch or implement countermeasures like locks
120+
// or mutexes.
121+
122+
// Create nodes
123+
ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot);
124+
ResourceNode * node404 = new ResourceNode("", "GET", &handle404);
125+
126+
// Add nodes to the server
127+
secureServer.registerNode(nodeRoot);
128+
secureServer.setDefaultNode(node404);
129+
130+
Serial.println("Starting server...");
131+
secureServer.start();
132+
133+
if (secureServer.isRunning())
134+
{
135+
Serial.println("Server ready.");
136+
137+
// "loop()" function of the separate task
138+
while (true)
139+
{
140+
// This call will let the server do its work
141+
secureServer.loop();
142+
143+
// Other code would go here...
144+
delay(1);
145+
}
146+
}
147+
}
148+
149+
void handleRoot(HTTPRequest * req, HTTPResponse * res)
150+
{
151+
// Status code is 200 OK by default.
152+
// We want to deliver a simple HTML page, so we send a corresponding content type:
153+
res->setHeader("Content-Type", "text/html");
154+
155+
// The response implements the Print interface, so you can use it just like
156+
// you would write to Serial etc.
157+
res->println("<!DOCTYPE html>");
158+
res->println("<html>");
159+
res->println("<head><title>Hello World!</title></head>");
160+
res->println("<body>");
161+
res->println("<h1>Hello World!</h1>");
162+
res->print("<p>Your server is running for ");
163+
// A bit of dynamic data: Show the uptime
164+
res->print((int)(millis() / 1000), DEC);
165+
res->println(" seconds.</p>");
166+
res->println("</body>");
167+
res->println("</html>");
168+
}
169+
170+
void handle404(HTTPRequest * req, HTTPResponse * res)
171+
{
172+
// Discard request body, if we received any
173+
// We do this, as this is the default node and may also server POST/PUT requests
174+
req->discardRequestBody();
175+
176+
// Set the response status
177+
res->setStatusCode(404);
178+
res->setStatusText("Not Found");
179+
180+
// Set content type of the response
181+
res->setHeader("Content-Type", "text/html");
182+
183+
// Write a tiny HTTP page
184+
res->println("<!DOCTYPE html>");
185+
res->println("<html>");
186+
res->println("<head><title>Not Found</title></head>");
187+
res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
188+
res->println("</html>");
189+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef CERT_H_
2+
#define CERT_H_
3+
#error You have to run the srcipt extras/create_cert.sh to recreate these files
4+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef PRIVATE_KEY_H_
2+
#define PRIVATE_KEY_H_
3+
#error You have to run the srcipt extras/create_cert.sh to recreate these files
4+
#endif

0 commit comments

Comments
 (0)