/*
* Hello world web server
* circuits4you.com
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "index.h" //Our HTML webpage contents
//SSID and Password of your WiFi router
const char* ssid = "Router";
const char* password = "123456789";
ESP8266WebServer server(80); //Server on port 80
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
//==============================================================
// SETUP
//==============================================================
void setup(void){
Serial.begin(9600);
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
server.on("/", handleRoot); //Which routine to handle at root location
server.begin(); //Start server
Serial.println("HTTP server started");
}
//==============================================================
// LOOP
//==============================================================
void loop(void){
server.handleClient(); //Handle client requests
}
I think there is something going on using the header file as a webpage and was hoping you all had some thoughts. This is the first time I ever ran into this type of issue.
I tried the example code on that site and it causes my wemos 8266 to crash when the header file is called. The /ledOff and /ledOn part works to operate the board LED.
Why? The String looks like a very expensive and unnecessary copy. Just pass MAIN_page to the send function directly.
const char MAIN_page[] PROGMEM = R"=====(
The PROGMEM attribute is not a part of the type of MAIN_page. This could cause the compiler to select the wrong overload of the String constructor (String s = MAIN_page). Reading data from PROGMEM requires different instructions than reading data from RAM, where ordinary strings reside.
However, since Sep 14, 2019, this shouldn't be an issue anymore.
What version of the ESP8266 Core are you using?
mt_keg:
I am attempting to follow a tutorial of how to include webpage code in a separate header file
Generally speaking, that's a bad idea. Neither variable definitions nor executable code belong in header files. Reason being is that if that header file get #include(d) in multiple .ino / .cpp files, you'll get linker errors for duplicate definitions. For guidance on what belongs in header files version source code files, see Reply #3 Here.
Was there a solution to this issue?
Arduino 1.8.10
ESP Exception Decoder output,
Decoding stack results
0x4020671f: String::String(char const*) at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/cores/esp8266/WString.cpp line 34
0x40203f6c: handleRoot() at /home/amf/devel/esp3266/wemos/projects/webserver/webserver.ino line 25
0x40201eb4: esp8266webserver::FunctionRequestHandler ::handle(esp8266webserver::ESP8266WebServerTemplate &, HTTPMethod, String) at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h line 42
0x402092b4: esp8266webserver::FunctionRequestHandler ::canHandle(HTTPMethod, String) at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h line 28
0x40209282: std::_Function_handler ::_M_invoke(std::_Any_data const&) at /home/amf/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-4-b40a506/xtensa-lx106-elf/include/c++/4.8.2/functional line 2073
0x401000e1: std::function ::operator()() const at /home/amf/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-4-b40a506/xtensa-lx106-elf/include/c++/4.8.2/functional line 2465
0x40201ef0: esp8266webserver::FunctionRequestHandler ::handle(esp8266webserver::ESP8266WebServerTemplate &, HTTPMethod, String) at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h line 48
0x40203fe2: esp8266webserver::ESP8266WebServerTemplate ::_handleRequest() at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h line 673
0x402092b4: esp8266webserver::FunctionRequestHandler ::canHandle(HTTPMethod, String) at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h line 28
0x402043b8: esp8266webserver::ESP8266WebServerTemplate ::handleClient() at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h line 335
0x40209278: std::_Function_handler ::_M_invoke(std::_Any_data const&) at /home/amf/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-4-b40a506/xtensa-lx106-elf/include/c++/4.8.2/functional line 2069
0x40100170: ets_post(uint8, ETSSignal, ETSParam) at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/cores/esp8266/core_esp8266_main.cpp line 177
0x4020447c: loop() at /home/amf/devel/esp3266/wemos/projects/webserver/webserver.ino line 73
0x40207700: loop_wrapper() at /home/amf/.arduino15/packages/esp8266/hardware/esp8266/2.7.1/cores/esp8266/core_esp8266_main.cpp line 197
Why "after much googling"? That's effectively the same solution as the one I posted in reply #4.
Like I mentioned in that same reply, converting it to a String before calling the send function is extremely wasteful and useless. Pass MAIN_page to the send function directly.
I will try to test it too. I am just trying to learn to code. I am working with one project and we use one of the seo tools, which may help you. We tried it first to count the number of web pages on the sites, but we use more it's options now. If I am not wrong it may help with some code issues too, check it as this tool is for free.