Skip to content

Commit c7ddab9

Browse files
committed
Added two basic Arduino examples
1 parent f69fffc commit c7ddab9

File tree

4 files changed

+299
-25
lines changed

4 files changed

+299
-25
lines changed
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 HTTP Server on port 80 and an HTTPS server
12+
* on port 443 of your ESP32 with the following functionalities:
13+
* - Show simple page on web server root
14+
* - 404 for everything else
15+
*
16+
* The comments in this script focus on making both protocols available,
17+
* for setting up the server itself, see Static-Page.
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+
// Include certificate data (see note above)
25+
#include "cert.h"
26+
#include "private_key.h"
27+
28+
// We will use wifi
29+
#include <WiFi.h>
30+
31+
// Includes for the server
32+
// Note: We include HTTPServer and HTTPSServer
33+
#include <HTTPSServer.hpp>
34+
#include <HTTPServer.hpp>
35+
#include <SSLCert.hpp>
36+
#include <HTTPRequest.hpp>
37+
#include <HTTPResponse.hpp>
38+
39+
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
40+
using namespace httpsserver;
41+
42+
// Create an SSL certificate object from the files included above
43+
SSLCert cert = SSLCert(
44+
example_crt_DER, example_crt_DER_len,
45+
example_key_DER, example_key_DER_len
46+
);
47+
48+
// First, we create the HTTPSServer with the certificate created above
49+
HTTPSServer secureServer = HTTPSServer(&cert);
50+
51+
// Additionally, we create an HTTPServer for unencrypted traffic
52+
HTTPServer insecureServer = HTTPServer();
53+
54+
// Declare some handler functions for the various URLs on the server
55+
void handleRoot(HTTPRequest * req, HTTPResponse * res);
56+
void handle404(HTTPRequest * req, HTTPResponse * res);
57+
58+
void setup() {
59+
// For logging
60+
Serial.begin(115200);
61+
62+
// Connect to WiFi
63+
Serial.println("Setting up WiFi");
64+
WiFi.begin(WIFI_SSID, WIFI_PSK);
65+
while (WiFi.status() != WL_CONNECTED) {
66+
Serial.print(".");
67+
delay(500);
68+
}
69+
Serial.print("Connected. IP=");
70+
Serial.println(WiFi.localIP());
71+
72+
// For every resource available on the server, we need to create a ResourceNode
73+
// The ResourceNode links URL and HTTP method to a handler function
74+
ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot);
75+
ResourceNode * node404 = new ResourceNode("", "GET", &handle404);
76+
77+
// Add the root node to the servers. We can use the same ResourceNode on multiple
78+
// servers (you could also run multiple HTTPS servers)
79+
secureServer.registerNode(nodeRoot);
80+
insecureServer.registerNode(nodeRoot);
81+
82+
// We do the same for the default Node
83+
secureServer.setDefaultNode(node404);
84+
insecureServer.setDefaultNode(node404);
85+
86+
Serial.println("Starting HTTPS server...");
87+
secureServer.start();
88+
Serial.println("Starting HTTP server...");
89+
insecureServer.start();
90+
if (secureServer.isRunning() && insecureServer.isRunning()) {
91+
Serial.println("Servers ready.");
92+
}
93+
}
94+
95+
void loop() {
96+
// We need to call both loop functions here
97+
secureServer.loop();
98+
insecureServer.loop();
99+
100+
// Other code would go here...
101+
delay(1);
102+
}
103+
104+
// The hanlder functions are the same as in the Static-Page example.
105+
// The only difference is the check for isSecure in the root handler
106+
107+
void handleRoot(HTTPRequest * req, HTTPResponse * res) {
108+
res->setHeader("Content-Type", "text/html");
109+
110+
res->println("<!DOCTYPE html>");
111+
res->println("<html>");
112+
res->println("<head><title>Hello World!</title></head>");
113+
res->println("<body>");
114+
res->println("<h1>Hello World!</h1>");
115+
116+
res->print("<p>Your server is running for ");
117+
res->print((int)(millis()/1000), DEC);
118+
res->println(" seconds.</p>");
119+
120+
// You can check if you are connected over a secure connection, eg. if you
121+
// want to use authentication and redirect the user to a secure connection
122+
// for that
123+
if (req->isSecure()) {
124+
res->println("<p>You are connected via <strong>HTTPS</strong>.</p>");
125+
} else {
126+
res->println("<p>You are connected via <strong>HTTP</strong>.</p>");
127+
}
128+
129+
res->println("</body>");
130+
res->println("</html>");
131+
}
132+
133+
void handle404(HTTPRequest * req, HTTPResponse * res) {
134+
req->discardRequestBody();
135+
res->setStatusCode(404);
136+
res->setStatusText("Not Found");
137+
res->setHeader("Content-Type", "text/html");
138+
res->println("<!DOCTYPE html>");
139+
res->println("<html>");
140+
res->println("<head><title>Not Found</title></head>");
141+
res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
142+
res->println("</html>");
143+
}

examples/Static-Page/Static-Page.ino

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
*/
16+
17+
// TODO: Configure your WiFi here
18+
#define WIFI_SSID "<your ssid goes here>"
19+
#define WIFI_PSK "<your pre-shared key goes here>"
20+
21+
// Include certificate data (see note above)
22+
#include "cert.h"
23+
#include "private_key.h"
24+
25+
// We will use wifi
26+
#include <WiFi.h>
27+
28+
// Includes for the server
29+
#include <HTTPSServer.hpp>
30+
#include <SSLCert.hpp>
31+
#include <HTTPRequest.hpp>
32+
#include <HTTPResponse.hpp>
33+
34+
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
35+
using namespace httpsserver;
36+
37+
// Create an SSL certificate object from the files included above
38+
SSLCert cert = SSLCert(
39+
example_crt_DER, example_crt_DER_len,
40+
example_key_DER, example_key_DER_len
41+
);
42+
43+
// Create an SSL-enabled server that uses the certificate
44+
// The contstructor takes some more parameters, but we go for default values here.
45+
HTTPSServer secureServer = HTTPSServer(&cert);
46+
47+
// Declare some handler functions for the various URLs on the server
48+
void handleRoot(HTTPRequest * req, HTTPResponse * res);
49+
void handle404(HTTPRequest * req, HTTPResponse * res);
50+
51+
void setup() {
52+
// For logging
53+
Serial.begin(115200);
54+
55+
// Connect to WiFi
56+
Serial.println("Setting up WiFi");
57+
WiFi.begin(WIFI_SSID, WIFI_PSK);
58+
while (WiFi.status() != WL_CONNECTED) {
59+
Serial.print(".");
60+
delay(500);
61+
}
62+
Serial.print("Connected. IP=");
63+
Serial.println(WiFi.localIP());
64+
65+
// For every resource available on the server, we need to create a ResourceNode
66+
// The ResourceNode links URL and HTTP method to a handler function
67+
ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot);
68+
ResourceNode * node404 = new ResourceNode("", "GET", &handle404);
69+
70+
// Add the root node to the server
71+
secureServer.registerNode(nodeRoot);
72+
73+
// Add the 404 not found node to the server.
74+
// The path is ignored for the default node.
75+
secureServer.setDefaultNode(node404);
76+
77+
Serial.println("Starting server...");
78+
secureServer.start();
79+
if (secureServer.isRunning()) {
80+
Serial.println("Server ready.");
81+
}
82+
}
83+
84+
void loop() {
85+
// This call will let the server do its work
86+
secureServer.loop();
87+
88+
// Other code would go here...
89+
delay(1);
90+
}
91+
92+
void handleRoot(HTTPRequest * req, HTTPResponse * res) {
93+
// Status code is 200 OK by default.
94+
// We want to deliver a simple HTML page, so we send a corresponding content type:
95+
res->setHeader("Content-Type", "text/html");
96+
97+
// The response implements the Print interface, so you can use it just like
98+
// you would write to Serial etc.
99+
res->println("<!DOCTYPE html>");
100+
res->println("<html>");
101+
res->println("<head><title>Hello World!</title></head>");
102+
res->println("<body>");
103+
res->println("<h1>Hello World!</h1>");
104+
res->print("<p>Your server is running for ");
105+
// A bit of dynamic data: Show the uptime
106+
res->print((int)(millis()/1000), DEC);
107+
res->println(" seconds.</p>");
108+
res->println("</body>");
109+
res->println("</html>");
110+
}
111+
112+
void handle404(HTTPRequest * req, HTTPResponse * res) {
113+
// Discard request body, if we received any
114+
// We do this, as this is the default node and may also server POST/PUT requests
115+
req->discardRequestBody();
116+
117+
// Set the response status
118+
res->setStatusCode(404);
119+
res->setStatusText("Not Found");
120+
121+
// Set content type of the response
122+
res->setHeader("Content-Type", "text/html");
123+
124+
// Write a tiny HTTP page
125+
res->println("<!DOCTYPE html>");
126+
res->println("<html>");
127+
res->println("<head><title>Not Found</title></head>");
128+
res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
129+
res->println("</html>");
130+
}

examples/wifi/wifi.example.h

-18
This file was deleted.

extras/create_cert.sh

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -e
23
#------------------------------------------------------------------------------
34
# cleanup any previously created files
45
rm -f exampleca.* example.* cert.h private_key.h
@@ -16,8 +17,8 @@ distinguished_name = req_distinguished_name
1617
prompt = no
1718
[ req_distinguished_name ]
1819
C = DE
19-
ST = HE
20-
L = Darmstadt
20+
ST = BE
21+
L = Berlin
2122
O = MyCompany
2223
CN = myca.local
2324
EOF
@@ -37,8 +38,8 @@ distinguished_name = req_distinguished_name
3738
prompt = no
3839
[ req_distinguished_name ]
3940
C = DE
40-
ST = HE
41-
L = Darmstadt
41+
ST = BE
42+
L = Berlin
4243
O = MyCompany
4344
CN = esp32.local
4445
EOF
@@ -55,6 +56,24 @@ openssl rsa -in example.key -outform DER -out example.key.DER
5556
openssl x509 -in example.crt -outform DER -out example.crt.DER
5657

5758
# create header files
58-
mkdir ../examples/cert
59-
xxd -i example.crt.DER > ../examples/cert/cert.h
60-
xxd -i example.key.DER > ../examples/cert/private_key.h
59+
xxd -i example.crt.DER > ./cert.h
60+
xxd -i example.key.DER > ./private_key.h
61+
62+
# Copy files to every example
63+
for D in ../examples/*; do
64+
if [ -d "${D}" ] && [ -f "${D}/$(basename $D).ino" ]; then
65+
echo "Adding certificate to example $(basename $D)"
66+
cp ./cert.h ./private_key.h "${D}/"
67+
fi
68+
done
69+
70+
echo ""
71+
echo "Certificates created!"
72+
echo "---------------------"
73+
echo "
74+
echo " Private key: private_key.h"
75+
echo " Certificate data: cert.h"
76+
echo ""
77+
echo "Make sure to have both files available for inclusion when running the examples."
78+
echo "The files have been copied to all example directories, so if you open an example"
79+
echo " sketch, you should be fine."

0 commit comments

Comments
 (0)