I want to create a https server in ESP32 wrover module. I have googled and created the below code.
`
#include <WiFi.h>
#include <ESPmDNS.h>
#include <HTTPSServer.hpp>
#include <SSLCert.hpp>
#include <FS.h>
#include <LittleFS.h>
#include <SPIFFS.h>
// Replace with your network credentials
const char* ssid = "Buffalo-G-0238";
const char* password = "xvpi4w8km4bhr";
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
using namespace httpsserver;
#define FORMAT_LITTLEFS_IF_FAILED true
// Create an SSL certificate object
SSLCert cert;
void setup() {
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Print the IP address
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
ESP.restart();
}
LittleFS.begin("spiffs");
File certFile = LittleFS.open("/data/server.pem", "r");
if (!certFile) {
Serial.println("Failed to open cert file");
return;
}
File privateKeyFile = LittleFS.open("/data/server.key", "r");
if (!privateKeyFile) {
Serial.println("Failed to open private key file");
return;
}
// Create the SSL certificate
const uint16_t certSize = certFile.size();
unsigned char certData\[certSize\];
certFile.read(certData, certSize);
cert.setCert(certData, certSize);
const uint16_t prikeySize = privateKeyFile.size();
unsigned char prikeyData\[prikeySize\];
certFile.read(prikeyData, prikeySize);
cert.setPK(prikeyData, prikeySize);
// Create an SSL-enabled server that uses the certificate
HTTPSServer secureServer = HTTPSServer(&cert);
// Add a GET endpoint for the root URL ("/")
ResourceNode\* nodeRoot = new ResourceNode("/", "GET", \[\](HTTPRequest\* req, HTTPResponse\* res) {
if (req && res) { // Check for null pointers
res-\>setHeader("Content-Type", "text/html");
res-\>println("\<!DOCTYPE html\>\<html\>\<body\><h1>Hello, world!</h1>\</body\>\</html\>");
}
});
secureServer.registerNode(nodeRoot);
// Start the server
secureServer.start();
if (secureServer.isRunning()) {
Serial.println("Server ready.");
}
}
void loop() { // Nothing }
Program download is success. But I am getting below error after reset. `
E (1559) SPIFFS: mount failed, -10025
E (16717) esp_littlefs: ./components/esp_littlefs/src/littlefs/lfs.c:1347:error: Corrupted dir pair at {0x0, 0x1}
E (16718) esp_littlefs: mount failed, (-84)
E (16721) esp_littlefs: Failed to initialize LittleFS
I dont know what causing the problem. I am having server.key and server.pem files in the sketch/data folder.
Please help me with this problem.
I am stucking in this more than a week.
Thanks in advance.
I have tried in googling and modified here and there but still i am not getting the results.