ThreeBoysTech

HttpsExampleCode

Nov 26th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  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.  
  18. const char* WIFI_SSID = "SecureConnection";
  19. const char* WIFI_PSK = "password";
  20. #define NETWORK_CONNECTION 1
  21. //const char* WIFI_SSID = "TBMiniRouter";
  22. //const char* WIFI_PSK = "password";
  23. //#define NETWORK_CONNECTION 0
  24.  
  25. // Include certificate data (see note above)
  26. #include "cert.h"
  27. #include "private_key.h"
  28.  
  29. // Binary data for the favicon
  30. #include "favicon.h"
  31.  
  32. // We will use wifi
  33. #include <WiFi.h>
  34.  
  35. // Includes for the server
  36. #include <HTTPSServer.hpp>
  37. #include <SSLCert.hpp>
  38. #include <HTTPRequest.hpp>
  39. #include <HTTPResponse.hpp>
  40.  
  41.  
  42. /* Put IP Address details */
  43. IPAddress localIP(10,10,10,100);
  44. IPAddress gatewayIP(10,10,10,1);
  45. IPAddress subnetMask(255,255,255,0);
  46.  
  47. // The HTTPS Server comes in a separate namespace. For easier use, include it here.
  48. using namespace httpsserver;
  49.  
  50. // Create an SSL certificate object from the files included above
  51. SSLCert cert = SSLCert(
  52. example_crt_DER, example_crt_DER_len,
  53. example_key_DER, example_key_DER_len
  54. );
  55.  
  56. // Create an SSL-enabled server that uses the certificate
  57. // The contstructor takes some more parameters, but we go for default values here.
  58. HTTPSServer secureServer = HTTPSServer(&cert);
  59.  
  60. // Declare some handler functions for the various URLs on the server
  61. // The signature is always the same for those functions. They get two parameters,
  62. // which are pointers to the request data (read request body, headers, ...) and
  63. // to the response data (write response, set status code, ...)
  64. void handleRoot(HTTPRequest * req, HTTPResponse * res);
  65. void handleFavicon(HTTPRequest * req, HTTPResponse * res);
  66. void handle404(HTTPRequest * req, HTTPResponse * res);
  67.  
  68.  
  69. void ConfigureWiFi(int Switch)
  70. {
  71. Serial.print("configure WifFi: ");
  72. Serial.println(Switch);
  73. if(Switch)
  74. {
  75. // Connect to Wi-Fi network with SSID and password
  76. WiFi.mode(WIFI_AP);
  77. WiFi.softAPConfig(localIP,gatewayIP,subnetMask);
  78. WiFi.softAP(WIFI_SSID,WIFI_PSK);
  79. Serial.print("creating access point ");
  80. Serial.println(WIFI_SSID);
  81.  
  82. }
  83. else
  84. {
  85. // Connect to WiFi
  86. Serial.println("Setting up WiFi");
  87. WiFi.begin(WIFI_SSID, WIFI_PSK);
  88. while (WiFi.status() != WL_CONNECTED) {
  89. Serial.print(".");
  90. delay(500);
  91. }
  92. Serial.print("Connected. IP=");
  93. Serial.println(WiFi.localIP());
  94.  
  95. }
  96. }
  97. void setup() {
  98. // For logging
  99. Serial.begin(115200);
  100.  
  101. ConfigureWiFi(NETWORK_CONNECTION);
  102.  
  103. // For every resource available on the server, we need to create a ResourceNode
  104. // The ResourceNode links URL and HTTP method to a handler function
  105. ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot);
  106. ResourceNode * nodeFavicon = new ResourceNode("/favicon.ico", "GET", &handleFavicon);
  107. ResourceNode * node404 = new ResourceNode("", "GET", &handle404);
  108.  
  109. // Add the root node to the server
  110. secureServer.registerNode(nodeRoot);
  111.  
  112. // Add the favicon
  113. secureServer.registerNode(nodeFavicon);
  114.  
  115. // Add the 404 not found node to the server.
  116. // The path is ignored for the default node.
  117. secureServer.setDefaultNode(node404);
  118.  
  119. Serial.println("Starting server...");
  120. secureServer.start();
  121. if (secureServer.isRunning()) {
  122. Serial.println("Server ready.");
  123. }
  124. }
  125.  
  126.  
  127. void loop() {
  128. // This call will let the server do its work
  129. secureServer.loop();
  130.  
  131. // Other code would go here...
  132. delay(1);
  133. }
  134.  
  135. void handleRoot(HTTPRequest * req, HTTPResponse * res) {
  136. // Status code is 200 OK by default.
  137. // We want to deliver a simple HTML page, so we send a corresponding content type:
  138. res->setHeader("Content-Type", "text/html");
  139.  
  140. // The response implements the Print interface, so you can use it just like
  141. // you would write to Serial etc.
  142. res->println("<!DOCTYPE html>");
  143. res->println("<html>");
  144. res->println("<head><title>Hello World!</title></head>");
  145. res->println("<body>");
  146. res->println("<h1>Hello World!</h1>");
  147. res->print("<p>Your server is running for ");
  148. // A bit of dynamic data: Show the uptime
  149. res->print((int)(millis()/1000), DEC);
  150. res->println(" seconds.</p>");
  151. res->println("</body>");
  152. res->println("</html>");
  153. }
  154.  
  155. void handleFavicon(HTTPRequest * req, HTTPResponse * res) {
  156. // Set Content-Type
  157. res->setHeader("Content-Type", "image/vnd.microsoft.icon");
  158. // Write data from header file
  159. res->write(FAVICON_DATA, FAVICON_LENGTH);
  160. }
  161.  
  162. void handle404(HTTPRequest * req, HTTPResponse * res) {
  163. // Discard request body, if we received any
  164. // We do this, as this is the default node and may also server POST/PUT requests
  165. req->discardRequestBody();
  166.  
  167. // Set the response status
  168. res->setStatusCode(404);
  169. res->setStatusText("Not Found");
  170.  
  171. // Set content type of the response
  172. res->setHeader("Content-Type", "text/html");
  173.  
  174. // Write a tiny HTTP page
  175. res->println("<!DOCTYPE html>");
  176. res->println("<html>");
  177. res->println("<head><title>Not Found</title></head>");
  178. res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
  179. res->println("</html>");
  180. }
Add Comment
Please, Sign In to add comment