Advertisement
mat8854

esp8266 websever https

Jun 30th, 2025 (edited)
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServerSecure.h>
  3. #include "certs.h"
  4.  
  5. const char* ssid = "YOUR_WIFI_SSID";
  6. const char* password = "YOUR_WIFI_PASSWORD";
  7.  
  8. const char* authUser = "admin";
  9. const char* authPass = "yourpassword";
  10.  
  11. BearSSL::ESP8266WebServerSecure server(10000);
  12.  
  13. void setup() {
  14.   Serial.begin(115200);
  15.   delay(100);
  16.  
  17.   // Connect to Wi-Fi
  18.   WiFi.begin(ssid, password);
  19.   Serial.print("Connecting to WiFi...");
  20.   while (WiFi.status() != WL_CONNECTED) {
  21.     delay(500);
  22.     Serial.print(".");
  23.   }
  24.   Serial.println("\nWiFi connected. IP address: ");
  25.   Serial.println(WiFi.localIP());
  26.  
  27.   // Load certificate and private key
  28.   server.getServer().setRSACert(
  29.     (const uint8_t*)cert, strlen(cert),
  30.     (const uint8_t*)key, strlen(key)
  31.   );
  32.  
  33.   // Add basic authentication to the root route
  34.   server.on("/", []() {
  35.     if (!server.authenticate(authUser, authPass)) {
  36.       return server.requestAuthentication();  // send 401
  37.     }
  38.     server.send(200, "text/html", "<h1>Hello, secure world!</h1><p>You are authenticated.</p>");
  39.   });
  40.  
  41.   server.begin();
  42.   Serial.println("Secure HTTPS server started on port 10000.");
  43. }
  44.  
  45. void loop() {
  46.   server.handleClient();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement