Compilation error when i try to make header file from main code

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();
  }
}

This is how i made the header and cpp files:
Header:

#ifndef UPDATEFIRMWARE_H
#define UPDATEFIRMWARE_H

#include <StreamString.h>
#include "Update.h"
#include <Ethernet_Generic.h>
#include <EthernetWebServer.h>
#include "defines.h"


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>";

//----------------------------------------- Firmware Update Pages -----------------------------------------------------

void handleUpdate();

void handlePostUpdate();

void handleFileUpload();


#endif

And the cpp:

#include "updatefirmware.h"

EthernetWebServer ethernetServer(80);


//----------------------------------------- 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>OTEthernet 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  '>";
  s += "  <a href='/analog'> Read RGBC color sensor </a><br>";
  s += "                  <input type='reset'></form></pre>";
//  s += "           <input type='submit' value='  Update  '></form></pre>";
//  s += "<a href='/analog'>Read RGBC color sensor</a><br>";
  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='5;URL=/'>Update Success ! Rebooting...\n";  //content= '30;
    s += (htmlclose);
    //ethernetServer.client().setNoDelay(true);
    ethernetServer.send(200, "text / html", s);

    ethernetServer.client().stop();
    ESP.restart();
  }
  
  // noTone(BUZZER_PIN);
  // tone(BUZZER_PIN, 480, 300);   // Play a note for 300ms

  // noTone(BUZZER_PIN);
}


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();
  }
  
}

What is the error that you get (in full) ?

V:\Elektronik\Toolbox_AST12\Toolbox4_Software\main\updatefirmware.cpp: In function 'void handlePostUpdate()':
V:\Elektronik\Toolbox_AST12\Toolbox4_Software\main\updatefirmware.cpp:35:26: error: no matching function for call to 'UpdateClass::printError(StreamString&)'
     Update.printError(str);
In file included from V:\Elektronik\Toolbox_AST12\Toolbox4_Software\main\updatefirmware.h:5,
                 from V:\Elektronik\Toolbox_AST12\Toolbox4_Software\main\updatefirmware.cpp:1:
C:\Users\khalilm\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.11\libraries\Update\src/Update.h:86:10: note: candidate: 'void UpdateClass::printError(Print&)'
     void printError(Print &out);
          ^~~~~~~~~~
C:\Users\khalilm\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.11\libraries\Update\src/Update.h:86:10: note:   no known conversion for argument 1 from 'StreamString' to 'Print&'
V:\Elektronik\Toolbox_AST12\Toolbox4_Software\main\updatefirmware.cpp:42:7: error: no match for 'operator+=' (operand types are 'String' and 'StreamString')
     s += str;

exit status 1

Compilation error: no matching function for call to 'UpdateClass::printError(StreamString&)'

It's a too long error message.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.