|
| 1 | +/** |
| 2 | + Example for the ESP32_ENC HTTP(S) Webserver |
| 3 | +
|
| 4 | + IMPORTANT NOTE: |
| 5 | + To run this script, your need to |
| 6 | + 1) Make sure to have certificate data available. You will find a |
| 7 | + shell script (create_cert.sh) and instructions to do so in the library folder |
| 8 | + under extras/ |
| 9 | +
|
| 10 | + This script will install an HTTPS Server on your ESP32_ENC with the following |
| 11 | + functionalities: |
| 12 | + - Show simple page on web server root |
| 13 | + - 404 for everything else |
| 14 | + The server will be run in a separate task, so that you can do your own stuff |
| 15 | + in the loop() function. |
| 16 | + Everything else is just like the Static-Page example |
| 17 | +*/ |
| 18 | + |
| 19 | +/** Check if we have multiple cores */ |
| 20 | +#if CONFIG_FREERTOS_UNICORE |
| 21 | + #define ARDUINO_RUNNING_CORE 0 |
| 22 | +#else |
| 23 | + #define ARDUINO_RUNNING_CORE 1 |
| 24 | +#endif |
| 25 | + |
| 26 | +// Include certificate data (see note above) |
| 27 | +#include "cert.h" |
| 28 | +#include "private_key.h" |
| 29 | + |
| 30 | +////////////////////////////////////////////////// |
| 31 | + |
| 32 | +// For ESP32_ENC |
| 33 | +#define DEBUG_ETHERNET_WEBSERVER_PORT Serial |
| 34 | + |
| 35 | +// Debug Level from 0 to 4 |
| 36 | +#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3 |
| 37 | + |
| 38 | +////////////////////////////////////////////////////////// |
| 39 | + |
| 40 | +// Optional values to override default settings |
| 41 | +//#define SPI_HOST 1 |
| 42 | +//#define SPI_CLOCK_MHZ 8 |
| 43 | + |
| 44 | +// Must connect INT to GPIOxx or not working |
| 45 | +//#define INT_GPIO 4 |
| 46 | + |
| 47 | +//#define MISO_GPIO 19 |
| 48 | +//#define MOSI_GPIO 23 |
| 49 | +//#define SCK_GPIO 18 |
| 50 | +//#define CS_GPIO 5 |
| 51 | + |
| 52 | +////////////////////////////////////////////////////////// |
| 53 | + |
| 54 | +#include <WebServer_ESP32_ENC.h> |
| 55 | + |
| 56 | +////////////////////////////////////////////////// |
| 57 | + |
| 58 | +// Enter a MAC address and IP address for your controller below. |
| 59 | +#define NUMBER_OF_MAC 20 |
| 60 | + |
| 61 | +byte mac[][NUMBER_OF_MAC] = |
| 62 | +{ |
| 63 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 }, |
| 64 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 }, |
| 65 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 }, |
| 66 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 }, |
| 67 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 }, |
| 68 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 }, |
| 69 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 }, |
| 70 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 }, |
| 71 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 }, |
| 72 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A }, |
| 73 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B }, |
| 74 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C }, |
| 75 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D }, |
| 76 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E }, |
| 77 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F }, |
| 78 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 }, |
| 79 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 }, |
| 80 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 }, |
| 81 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 }, |
| 82 | + { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 }, |
| 83 | +}; |
| 84 | + |
| 85 | +// Select the IP address according to your local network |
| 86 | +IPAddress myIP(192, 168, 2, 232); |
| 87 | +IPAddress myGW(192, 168, 2, 1); |
| 88 | +IPAddress mySN(255, 255, 255, 0); |
| 89 | + |
| 90 | +// Google DNS Server IP |
| 91 | +IPAddress myDNS(8, 8, 8, 8); |
| 92 | + |
| 93 | +////////////////////////////////////////////////// |
| 94 | + |
| 95 | +#include <HTTPS_Server_Generic.h> |
| 96 | + |
| 97 | +// The HTTPS Server comes in a separate namespace. For easier use, include it here. |
| 98 | +using namespace httpsserver; |
| 99 | + |
| 100 | +// Create an SSL certificate object from the files included above |
| 101 | +SSLCert cert = SSLCert( |
| 102 | + example_crt_DER, example_crt_DER_len, |
| 103 | + example_key_DER, example_key_DER_len |
| 104 | + ); |
| 105 | + |
| 106 | +// Create an SSL-enabled server that uses the certificate |
| 107 | +HTTPSServer secureServer = HTTPSServer(&cert); |
| 108 | + |
| 109 | +void serverTask(void *params) |
| 110 | +{ |
| 111 | + // In the separate task we first do everything that we would have done in the |
| 112 | + // setup() function, if we would run the server synchronously. |
| 113 | + |
| 114 | + // Note: The second task has its own stack, so you need to think about where |
| 115 | + // you create the server's resources and how to make sure that the server |
| 116 | + // can access everything it needs to access. Also make sure that concurrent |
| 117 | + // access is no problem in your sketch or implement countermeasures like locks |
| 118 | + // or mutexes. |
| 119 | + |
| 120 | + // Create nodes |
| 121 | + ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot); |
| 122 | + ResourceNode * node404 = new ResourceNode("", "GET", &handle404); |
| 123 | + |
| 124 | + // Add nodes to the server |
| 125 | + secureServer.registerNode(nodeRoot); |
| 126 | + secureServer.setDefaultNode(node404); |
| 127 | + |
| 128 | + Serial.println("Starting server..."); |
| 129 | + secureServer.start(); |
| 130 | + |
| 131 | + if (secureServer.isRunning()) |
| 132 | + { |
| 133 | + Serial.println("Server ready."); |
| 134 | + |
| 135 | + // "loop()" function of the separate task |
| 136 | + while (true) |
| 137 | + { |
| 138 | + // This call will let the server do its work |
| 139 | + secureServer.loop(); |
| 140 | + |
| 141 | + // Other code would go here... |
| 142 | + delay(1); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +void handleRoot(HTTPRequest * req, HTTPResponse * res) |
| 148 | +{ |
| 149 | + // Status code is 200 OK by default. |
| 150 | + // We want to deliver a simple HTML page, so we send a corresponding content type: |
| 151 | + res->setHeader("Content-Type", "text/html"); |
| 152 | + |
| 153 | + // The response implements the Print interface, so you can use it just like |
| 154 | + // you would write to Serial etc. |
| 155 | + res->println("<!DOCTYPE html>"); |
| 156 | + res->println("<html>"); |
| 157 | + res->println("<head><title>Hello World!</title></head>"); |
| 158 | + res->println("<body>"); |
| 159 | + res->println("<h1>Hello World!</h1>"); |
| 160 | + res->print("<p>Your server is running for "); |
| 161 | + // A bit of dynamic data: Show the uptime |
| 162 | + res->print((int)(millis() / 1000), DEC); |
| 163 | + res->println(" seconds.</p>"); |
| 164 | + res->println("</body>"); |
| 165 | + res->println("</html>"); |
| 166 | +} |
| 167 | + |
| 168 | +void handle404(HTTPRequest * req, HTTPResponse * res) |
| 169 | +{ |
| 170 | + // Discard request body, if we received any |
| 171 | + // We do this, as this is the default node and may also server POST/PUT requests |
| 172 | + req->discardRequestBody(); |
| 173 | + |
| 174 | + // Set the response status |
| 175 | + res->setStatusCode(404); |
| 176 | + res->setStatusText("Not Found"); |
| 177 | + |
| 178 | + // Set content type of the response |
| 179 | + res->setHeader("Content-Type", "text/html"); |
| 180 | + |
| 181 | + // Write a tiny HTTP page |
| 182 | + res->println("<!DOCTYPE html>"); |
| 183 | + res->println("<html>"); |
| 184 | + res->println("<head><title>Not Found</title></head>"); |
| 185 | + res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>"); |
| 186 | + res->println("</html>"); |
| 187 | +} |
| 188 | + |
| 189 | +void setup() |
| 190 | +{ |
| 191 | + // For logging |
| 192 | + Serial.begin(115200); |
| 193 | + |
| 194 | + while (!Serial && millis() < 5000); |
| 195 | + |
| 196 | + /////////////////////////////////////////////// |
| 197 | + |
| 198 | + Serial.print("\nStarting Async_Server on " + String(ARDUINO_BOARD)); |
| 199 | + Serial.println(" with " + String(SHIELD_TYPE)); |
| 200 | + Serial.println(WEBSERVER_ESP32_ENC_VERSION); |
| 201 | + Serial.println(HTTPS_SERVER_GENERIC_VERSION); |
| 202 | + |
| 203 | + /////////////////////////////////// |
| 204 | + |
| 205 | + // To be called before ETH.begin() |
| 206 | + ESP32_ENC_onEvent(); |
| 207 | + |
| 208 | + // start the ethernet connection and the server: |
| 209 | + // Use DHCP dynamic IP and random mac |
| 210 | + uint16_t index = millis() % NUMBER_OF_MAC; |
| 211 | + |
| 212 | + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, |
| 213 | + // int SPI_HOST, uint8_t *ENC28J60_Mac = ENC28J60_Default_Mac); |
| 214 | + //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST ); |
| 215 | + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST, mac[index] ); |
| 216 | + |
| 217 | + // Static IP, leave without this line to get IP via DHCP |
| 218 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 219 | + ETH.config(myIP, myGW, mySN, myDNS); |
| 220 | + |
| 221 | + ESP32_ENC_waitForConnect(); |
| 222 | + |
| 223 | + /////////////////////////////////// |
| 224 | + |
| 225 | + Serial.print(F("HTTPS EthernetWebServer is @ IP : ")); |
| 226 | + Serial.println(ETH.localIP()); |
| 227 | + |
| 228 | + Serial.print(F("To access, use https://")); |
| 229 | + Serial.println(ETH.localIP()); |
| 230 | + |
| 231 | + /////////////////////////////////////////////// |
| 232 | + |
| 233 | + // Setup the server as a separate task. |
| 234 | + Serial.println("Creating server task... "); |
| 235 | + // We pass: |
| 236 | + // serverTask - the function that should be run as separate task |
| 237 | + // "https443" - a name for the task (mainly used for logging) |
| 238 | + // 6144 - stack size in byte. If you want up to four clients, you should |
| 239 | + // not go below 6kB. If your stack is too small, you will encounter |
| 240 | + // Panic and stack canary exceptions, usually during the call to |
| 241 | + // SSL_accept. |
| 242 | + xTaskCreatePinnedToCore(serverTask, "https443", 6144, NULL, 1, NULL, ARDUINO_RUNNING_CORE); |
| 243 | +} |
| 244 | + |
| 245 | +void loop() |
| 246 | +{ |
| 247 | + Serial.println("loop()"); |
| 248 | + delay(5000); |
| 249 | +} |
0 commit comments