Hello,
i am trying to do OTA-update using HTTPS-Sever.
I am using this libary.
i managed to use the W5500-Ethernet-modul successfully.
Here is the code i use:
/**
Example for the ESP32_SC_W5500 HTTP(S) Webserver
IMPORTANT NOTE:
To run this script, your need to
1) Make sure to have certificate data available. You will find a
shell script (create_cert.sh) and instructions to do so in the library folder
under extras/
This script will install an HTTP Server on port 80 and an HTTPS server
on port 443 of your ESP32_SC_W5500 with the following functionalities:
- Show simple page on web server root
- 404 for everything else
The comments in this script focus on making both protocols available,
for setting up the server itself, see Static-Page.
*/
// Include certificate data (see note above)
#include "cert.h"
#include "private_key.h"
#include <SPI.h>
//////////////////////////////////////////////////
// For ESP32_SC_W5500
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
//////////////////////////////////////////////////////////
// For ESP32-S3
// Optional values to override default settings
// Don't change unless you know what you're doing
#define ETH_SPI_HOST SPI3_HOST
#define SPI_CLOCK_MHZ 25 //32
// Must connect INT to GPIOxx or not working
#define INT_GPIO 19
//////////////////////////////////////////////////////////
#include <WebServer_ESP32_SC_W5500.h>
//////////////////////////////////////////////////
#define SCK 36 //12
#define SS 39 //10
#define MOSI 38 //11
#define MISO 37 //13
// Enter a MAC address and IP address for your controller below.
byte mac[] ={0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01};
// Select the IP address according to your local network
IPAddress myIP(192, 19, 225, 150);
IPAddress myGW(225, 225, 225, 1);
IPAddress mySN(255, 255, 0, 0);
// Google DNS Server IP
// IPAddress myDNS(8, 8, 8, 8);
//////////////////////////////////////////////////
#include <HTTPS_Server_Generic.h>
#include <StreamString.h>
#include "Update.h"
#include <LittleFS.h>
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
using namespace httpsserver;
// Header and body style for the update webpage
static const char pageheader[] PROGMEM = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
static const char htmlhead[] PROGMEM = "<html><head><title>HttpUpdater</title><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ></head>";
static const char bodystyle[] PROGMEM = "<body style=\"color: dimgray; background-color: palegoldenrod; font-size: 12pt; font-family: sans-serif;\">";
static const char htmlclose[] PROGMEM = "</body></html>";
// Create an SSL certificate object from the files included above
SSLCert cert = SSLCert(
example_crt_DER, example_crt_DER_len,
example_key_DER, example_key_DER_len
);
// First, we create the HTTPSServer with the certificate created above
HTTPSServer secureServer = HTTPSServer(&cert);
String certStr, keyStr;
void loadCerts(String &certStr, String &keyStr) {
File certFile = LittleFS.open("/cert.pem", "r");
File keyFile = LittleFS.open("/key.pem", "r");
certStr = certFile.readString();
keyStr = keyFile.readString();
certFile.close();
keyFile.close();
}
void handleRoot(HTTPRequest * req, HTTPResponse * res)
{
res->setHeader("Content-Type", "text/html");
res->println("<!DOCTYPE html>");
res->println("<html>");
res->println("<head><title>Hello World!</title></head>");
res->println("<body>");
res->println("<h1>Hello World!</h1>");
res->print("<p>Your server is running for ");
res->print((int)(millis() / 1000), DEC);
res->println(" seconds.</p>");
// You can check if you are connected over a secure connection, eg. if you
// want to use authentication and redirect the user to a secure connection
// for that
if (req->isSecure())
{
res->println("<p>You are connected via <strong>HTTPS</strong>.</p>");
}
else
{
res->println("<p>You are connected via <strong>HTTP</strong>.</p>");
}
res->println("</body>");
res->println("</html>");
}
void handle404(HTTPRequest * req, HTTPResponse * res)
{
req->discardRequestBody();
res->setStatusCode(404);
res->setStatusText("Not Found");
res->setHeader("Content-Type", "text/html");
res->println("<!DOCTYPE html>");
res->println("<html>");
res->println("<head><title>Not Found</title></head>");
res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
res->println("</html>");
}
//----------------------------------------- Firmware Update Pages -----------------------------------------------------
// HTML content for the upload webpage
const char uploadPageHTML[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>OTA Update</title>
</head>
<body>
<h1>Firmware Update</h1>
<form method="POST" action="/update" enctype="multipart/form-data">
<input type="file" name="firmware">
<button type="submit">Upload</button>
</form>
</body>
</html>
)rawliteral";
// Handle the webpage serving
void handleUploadPage(HTTPRequest *req, HTTPResponse *res) {
res->setHeader("Content-Type", "text/html");
res->print(uploadPageHTML); // Serve the HTML page
}
void handleFirmwareUpdate(HTTPRequest * req, HTTPResponse * res) {
if (req->getMethod() != "POST") {
res->setStatusCode(405);
res->println("Method Not Allowed");
return;
}
// Get content length
size_t contentLength = req->getContentLength();
if (contentLength == 0) {
res->setStatusCode(400);
res->println("Empty content. No firmware file received.");
return;
}
// Begin OTA process
if (!Update.begin(contentLength)) {
res->setStatusCode(500);
res->println("Failed to start update process.");
return;
}
uint8_t buffer[4095];
size_t bytesRead = 0;
// Read data from the request body
while ((bytesRead = req->readBytes(buffer, sizeof(buffer))) > 0) {
if (Update.write(buffer, bytesRead) != bytesRead) {
Update.abort();
res->setStatusCode(500);
res->println("Error while writing firmware data.");
return;
}
}
// Finalize the update
if (Update.end(true)) { // true to set reboot flag
res->setStatusCode(200);
res->println("Firmware update successful. Device will reboot.");
ESP.restart();
} else {
res->setStatusCode(500);
res->println("Firmware update failed.");
}
}
void setup()
{
// For logging
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(500);
///////////////////////////////////////////////
Serial.print("\nStarting HTTPS-and-HTTP on " + String(ARDUINO_BOARD));
Serial.println(" with " + String(SHIELD_TYPE));
Serial.println(WEBSERVER_ESP32_SC_W5500_VERSION);
Serial.println(HTTPS_SERVER_GENERIC_VERSION);
///////////////////////////////////
ESP32_W5500_onEvent();
ETH.begin(MISO, MOSI, SCK, SS, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac);
ETH.config(myIP, myGW, mySN);
// give the Ethernet shield a second to initialize:
delay(2000);
Serial.print("Link speed: ");
Serial.println(ETH.linkSpeed());
ESP32_W5500_waitForConnect();
///////////////////////////////////
Serial.print(F("HTTPS EthernetWebServer is @ IP : "));
Serial.println(ETH.localIP());
Serial.print(F("To access, use https://"));
Serial.println(ETH.localIP());
///////////////////////////////////////////////
ResourceNode * updatepageNode = new ResourceNode("/update","GET", &handleUploadPage);
ResourceNode * otaNode = new ResourceNode("/update","POST", &handleFirmwareUpdate);
secureServer.registerNode(updatepageNode);
secureServer.registerNode(otaNode);
Serial.println("Starting HTTPS server...");
secureServer.start();
if (secureServer.isRunning() && insecureServer.isRunning())
{
Serial.println("Servers ready.");
}
}
void loop()
{
// We need to call both loop functions here
secureServer.loop();
insecureServer.loop();
// Other code would go here...
delay(1);
}
It opens the update page and i can select the .bin file to uploade it, but then i get:
The website is not available
Connection has been reset.
Any one can help me please.
Thanks in advance.