Hello all,
the foloowing code works perfectlly to update the ESP32 over the Ethernet.
The problem is: when i try to put the code section related to the update functionality in a separate header file, i get compilation error.
I have tried some solutions but had no success with it.
Any suggestions how to manage that?
Thank you in advance.
The code :
#include <WebServer.h>
#include <StreamString.h>
#include <SPI.h>
#include <EthernetWebServer.h>
#include "Ethernet_Generic.h"
//#include <EthernetUdp.h> // not needed yet
#define W5500_CS 5
#define SPI_FRQ 32000000 // better than the 1 or 4MHz default
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>";
// Use DHCP dynamic IP
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01};
// Use Static IP
IPAddress ip(192, 168, 2, 222);
EthernetWebServer ethernetServer(80);
void handleEthernetRoot() {
ethernetServer.send(200, "text/plain", "Hello from ESP32 Ethernet!");
//ethernetServer.send(200, "text/plain", "Hello from ESP32 Ethernet!, The upload worked !!");
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.print("Connecting to ");
Serial.println(ssid);
//OTAUpdateSetup();
SPI.setFrequency(SPI_FRQ);
Ethernet.init (W5500_CS);
// start the ethernet connection and the server:
//uint16_t index = millis() % NUMBER_OF_MAC;
Ethernet.begin(mac, ip);
//Ethernet.begin(mac);
Serial.println("Currently Used SPI pinout:");
Serial.print("MOSI:");
Serial.println(MOSI);
Serial.print("MISO:");
Serial.println(MISO);
Serial.print("SCK:");
Serial.println(SCK);
Serial.print("CS/SS:");
Serial.println(W5500_CS);
ethernetServer.on("/", handleEthernetRoot);
ethernetServer.on(update_path, HTTP_GET, handleUpdate);
ethernetServer.on(update_path, HTTP_POST, handlePostUpdate, handleFileUpload);
ethernetServer.begin();
Serial.print("HTTP EthernetWebServer is @ IP : ");
Serial.println(Ethernet.localIP());
}
void loop() {
//ArduinoOTA.handle();
ethernetServer.handleClient();
}
//----------------------------------------- Firmware Update Pages -----------------------------------------------------
void handleUpdate() {
if (!ethernetServer.authenticate("admin", "password"))
return ethernetServer.requestAuthentication();
String s = "";
s += FPSTR(pageheader);
s += FPSTR(htmlhead);
s += FPSTR(bodystyle);
s += "<h1>OTA Firmware Update</h1>";
s += "<pre><form method='post' action='' enctype='multipart/form-data'>";
s += "<input type='file' name='update'>";
s += " <input type='submit' value=' Update '></form></pre>";
s += FPSTR(htmlclose);
ethernetServer.send(200, "text/html", s);
}
void handlePostUpdate() {
if (!ethernetServer.authenticate("admin", "password"))
return ethernetServer.requestAuthentication();
if (Update.hasError()) {
StreamString str;
Update.printError(str);
str;
String s = "";
s += FPSTR(pageheader);
s += FPSTR(htmlhead);
s += FPSTR(bodystyle);
s += "<h1>Update Error </h1>";
s += str;
s += FPSTR(htmlclose);
ethernetServer.send(200, "text / html", s);
}
else {
String s = "";
s += FPSTR(pageheader);
s += FPSTR(htmlhead);
s += FPSTR(bodystyle);
s += "<META http-equiv='refresh' content='30;URL=/'>Update Success ! Rebooting...\n";
s += (htmlclose);
//ethernetServer.client().setNoDelay(true);
ethernetServer.send(200, "text / html", s);
delay(1000);
ethernetServer.client().stop();
ESP.restart();
}
}
void handleFileUpload() {
if (!ethernetServer.authenticate("admin", "password"))
return ethernetServer.requestAuthentication();
ethernetHTTPUpload& upload = ethernetServer.upload();
String updateerror = "";
if (upload.status == UPLOAD_FILE_START) {
//EthernetUDP::stop();
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
StreamString str;
Update.printError(str);
updateerror = str;
}
}
else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
StreamString str;
Update.printError(str);
updateerror = str;
}
}
else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
StreamString str;
Update.printError(str);
updateerror = str;
}
else if (upload.status == UPLOAD_FILE_ABORTED) {
Update.end();
}
yield();
}
}