building a large project

Hello,

i'm in the process of rewriting a prety large project.
in this rewriting i wan't to keep the main file small.

i'm trying to create several smaller .h and .ccp files (librarys)
but i get strainge error's like double declarations and or missing variables and more of that kind off errors.

i know what they mean, but don't realy know how to setup this kind off project.

this if for starting the project setup

main.ino

Lib1.h
Lib1.cpp

webpages.h (make use of variables in Lib1.h and need functions from within Lib1.CPP)

in the futher there will be more files to split out the functions.

i hope you understand me and could point me to some good tutorials on how to start code a larger project so it keeps readable.

See Reply #3 Here.

You say you are rewriting a large project but it sounds more as if you are just chopping up existing code.

Do you fully understand what the existing system does?
Why are you rewriting it?

You should analyse the existing system, chop out any unused bits, learn from any mistakes made, keep the good ideas.

@ardly,

i do fully understand what is happening and why somthings are done the way i programmed.

I'm currently in development of a new hardware device, which i want to give extended functionality, for that and for my own learning want to program it the right way.

it is not necessary to rewrite the code, it is because i want to learn coding better.

i'm a bit further, i'm curently running in a bit of trouble.

when trying to compile i get an error on void handle root, because webserver is not declared.
Can't managed to get it done the right way.
if i make

void handleRoot(WebServerClass& webserver)

and acces it with

webserver.on("/", handleRoot(webserver));

i get this strange error:

src/webpages.cpp: In function 'boolean Setup_Wifi(WebServerClass&, AutoConnect&)':
src/webpages.cpp:98:42: error: invalid use of void expression
webserver.on("/", handleRoot(webserver));

It doesn't make sense to me, is there someone who could explain what is wrong and how to fix it.

#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <WifiClient.h>
#include <AutoConnect.h> 
#include "webpages.h"


// Loading from PROGMEM
const char settings[] PROGMEM = R"raw(
{
  "title" : "Dimmer Settings",
  "uri" : "/Settings",
  "menu" : true,
  "element" : [
    {
      "name" : "caption1",
      "type" : "ACText",
      "value" : "<H1>Settings</H1>
"
    },
     {
      "name" : "caption2",
      "type" : "ACText",
      "value" : "<H2>Device settings</h2>"
    },
    { "name" : "devicename",
      "type" : "ACInput",
      "value" : "10",
      "label" : "Device name, also topic name for MQTT
"
    },    
    { "name" : "mqttServer",
      "type" : "ACInput",
      "value" : "127.0.0.1",
      "label" : "IP adres MQTT server
"
    },    
    { "name" : "mqttUsername",
      "type" : "ACInput",
      "value" : "",
      "label" : "MQTT user name
"
    },  
    { "name" : "mqttPasswordhttp",
      "type" : "ACInput",
      "value" : "",
      "label" : "MQTT user password
"
    },  
    {
      "name" : "caption3",
      "type" : "ACText",
      "value" : "<H2>Dimmer settings</h2>"
    },      
    { "name" : "minDim",
      "type" : "ACInput",
      "value" : "10",
      "pattern": "^[0-6]?[0-9]$",    //review
      "label" : "Minimum dimvalue
"
    },    
    { "name" : "maxDim",
      "type" : "ACInput",
      "value" : "10",
      "pattern": "^[0-6]?[0-9]$",    //review
      "label" : "Maximum dimvalue
"
    },
    { "name" : "dimCurve",
      "type" : "ACInput",
      "value" : "0",
      "label" : "Dimcurve
"
    },
    {
      "name": "save",
      "type": "ACSubmit",
      "value": "Save",
      "uri": "/saveDim"
    }
  ]
}
)raw";

//Display root page
void handleRoot() {
  String  content =
    "<html>"
    "<head>"
    "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
    "</head>"
    "<body>"
    "<center><H1>ESP32 WiFi Dimmer " + String(SW_Version) +
    "
AG 2019
"
    "
"
    "</H1>Check github for project and schematic</p>"
    "<a href=\"https://github.com/ageurtse/Wifi-Dimmer\" target=\"_blank\">https://github.com/ageurtse/Wifi-Dimmer</a>
"
    "</center></p>
"
     "</body>"
    "</html>";
  webserver.send(200, "text/html", content);
}

boolean Setup_Wifi(WebServerClass& webserver, AutoConnect& WebPortal) {
  webserver.on("/", handleRoot);
  WebPortal.load(settings);
 // WebPortal.on("/settings",settingsOn, AC_EXIT_AHEAD);
  
  if (WebPortal.begin()) {
    debugln("WiFi started, connect to: " + WiFi.localIP().toString());
  }
  return true;
};

Posting snippets is useless. Post enough code so that we can attempt to compile and get the exact same error that you’re seeing.

As well as posting all your code your should try to produce the smallest code that generates the error. If you snip out enough stuff you may well find the error yourself.

ageurtse:
when trying to compile i get an error on void handle root, because webserver is not declared.
Can't managed to get it done the right way.
if i make

void handleRoot(WebServerClass& webserver)

and access it with

webserver.on("/", handleRoot(webserver));

i get this strange error:

src/webpages.cpp: In function 'boolean Setup_Wifi(WebServerClass&, AutoConnect&)':
src/webpages.cpp:98:42: error: invalid use of void expression
webserver.on("/", handleRoot(webserver));

It doesn't make sense to me, is there someone who could explain what is wrong and how to fix it.

The compiler is complaining because instead of passing the address of the 'handleRoot()' function to 'webserver.on()' you are CALLING the 'handleRoot()' function with the argument 'webserver' and passing the returned 'void' value to 'webserver.on()'. The 'void' value makes no sense in that context. You want:

webserver.on("/", handleRoot);

That passes the address of 'handleRoot()'. If you put parens after the function name then you are calling the function.

@johnwasser, I saw that too. However, those errors are not repeated in the larger code snippet below directly below them in the same post. So, who knows what @ageurtse's code really looks like?

EDIT:
Also, the signature for the function pointer passed to 'webserver.on()' must be:

void theFunction();

i.e. It takes no arguments and returns no value.