The plan is to create a bigger project and to split the ESP8266 webserver-code over several modules (.h .c files). The main problem is that the ESP8266WebServer server is not declared in the index.h and index.c files.
What I've been trying to do:
Content main.c
#include <ESP8266WiFi.h>
#include "ESP8266WebServer.h"
#include "index.h"
ESP8266WebServer server(80);
const char* ssid = "test";
const char* password = "12345678";
void setup()
{
WiFi.softAP(ssid, password);
server.on("/", handleRoot(server)); // <-- use of deleted function 'esp8266webserver::ESP8266WebServerTemplate<WiFiServer>::ESP8266WebServerTemplate(const esp8266webserver::ESP8266WebServerTemplate<WiFiServer>&)'
server.begin();
}
void loop()
{
server1.handleClient();
}
Content index.h:
void handleRoot(ESP8266WebServer server);
const char htmlIndex[] PROGMEM = R"=====(
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
)=====";
Content index.c:
#include "webpages/index.h"
void handleRoot(ESP8266WebServer server)
{
Serial.println("GET /");
server.send(200, "text/html", htmlIndex);
}
How can I use the ESP8266WebServer server created in the main.c in index.c?
1 Answer 1
Move the webserver to index.c and include the webserver type:
ESP8266WebServer server(80);
Add an external reference in index.h and include it:
#include "ESP8266WebServer.h"
extern ESP8266WebServer server;
Index.h is included by both main.c and index.c so it will know about server being an ESP8266WebServer type, but does not allocate memory for it; that is done in index.c
This is called a forward reference.
-
I shifted
#include "ESP8266WebServer.h" extern ESP8266WebServer server;
to main.h and included it in index.c but in the end it is your idea. thx.kimliv– kimliv2019年12月14日 00:13:09 +00:00Commented Dec 14, 2019 at 0:13 -
Yes, in principle it does not matter where you place the external variable, as long as it is not in a header file (or at least not one that is included twice). Good luck with your project.Michel Keijzers– Michel Keijzers2019年12月14日 00:22:53 +00:00Commented Dec 14, 2019 at 0:22
Explore related questions
See similar questions with these tags.