· 3 months ago · Jun 30, 2025, 05:35 PM
1#include <ESP8266WiFi.h>
2#include <ESP8266WebServerSecure.h>
3#include "certs.h"
4
5const char* ssid = "YOUR_WIFI_SSID";
6const char* password = "YOUR_WIFI_PASSWORD";
7
8const char* authUser = "admin";
9const char* authPass = "yourpassword";
10
11BearSSL::ESP8266WebServerSecure server(10000);
12
13void 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
45void loop() {
46 server.handleClient();
47}