|
| 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 | +} |
0 commit comments