Libraries just don't work

I have a program from the internet, and this has been my experience with everything I try to upload with libraries. The errors are first, then the code.
What am I doing that’s causing so many things that are definitely in the library, things that work fine over in Arduino, why are they all failing? How am I not getting libraries right?

I’m pasting them into the lib folder, linking them in .ini, but the commands aren’t declared.

Compiling .pio\build\esp32doit-devkit-v1\libb21\WiFi\WiFiMulti.cpp.o
src\main.cpp: In function 'void setup()':
src\main.cpp:41:18: error: 'handle_OnConnect' was not declared in this scope
   server.on("/", handle_OnConnect);
                  ^
src\main.cpp:42:21: error: 'handle_NotFound' was not declared in this scope
   server.onNotFound(handle_NotFound);
                     ^
src\main.cpp: In function 'void handle_OnConnect()':
src\main.cpp:57:80: error: 'SendHTML' was not declared in this scope
   server.send(200, "text/html", SendHTML(temperature,humidity,pressure,altitude)); 
                                                                                ^
*** [.pio\build\esp32doit-devkit-v1\src\main.cpp.o] Error 1
========================================================================

Code:

#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;

float temperature, humidity, pressure, altitude;

/*Put your SSID & Password*/
const char* ssid = "YourNetworkName";  // Enter SSID here
const char* password = "YourPassword";  //Enter Password here

WebServer server(80);

void setup() {
  Serial.begin(115200);
  delay(100);

  bme.begin(0x76);

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());

  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

}
void loop() {
  server.handleClient();
}

void handle_OnConnect() {
  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0F;
  altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
  server.send(200, "text/html", SendHTML(temperature,humidity,pressure,altitude));
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float temperature,float humidity,float pressure,float altitude){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>ESP32 Weather Station</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<div id=\"webpage\">\n";
  ptr +="<h1>ESP32 Weather Station</h1>\n";
  ptr +="<p>Temperature: ";
  ptr +=temperature;
  ptr +="&deg;C</p>";
  ptr +="<p>Humidity: ";
  ptr +=humidity;
  ptr +="%</p>";
  ptr +="<p>Pressure: ";
  ptr +=pressure;
  ptr +="hPa</p>";
  ptr +="<p>Altitude: ";
  ptr +=altitude;
  ptr +="m</p>";
  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}

Hi @monsterthews,
remember that PlatformIO IDE is just not Arduino IDE.
PlatformIO on top of vscode works like a “normal” text editor, and upon compilation it does not pre-scan file contents for class or function headers as the Arduino IDE does.

You don’t have a problem with the libs, it’s your code…
Problem is that your functions are declared “after” you try to use them:
void handle_OnConnect() is the perfect example, you’re using it in setup() but declaring it further down.
Solution: Define functions ON TOP of object creation before setup() like so:

// Function prototypes
void handle_OnConnect(void);
void handle_NotFound(void);
etc.

so that the compiler can find them.
I propose you read PlatformIO onboarding and browse some tutorials before doing stuff, it will save you quite some frustration.

1 Like

Next time, please use markdown formatting to mark it as code. Put ``` before and after your code, or the forum will mangle it.

The compile errors …

src\main.cpp: In function 'void setup()':
src\main.cpp:44:18: error: 'handle_OnConnect' was not declared in this scope
   server.on("/", handle_OnConnect);
                  ^
src\main.cpp:46:21: error: 'handle_NotFound' was not declared in this scope
   server.onNotFound(handle_NotFound);
                     ^
src\main.cpp: In function 'void handle_OnConnect()':
src\main.cpp:65:85: error: 'SendHTML' was not declared in this scope
   server.send(200, "text / html", SendHTML(temperature, humidity, pressure, altitude));

… are due to the fact that we’re not in Arduino land any more, and there is no fancy pre-processing happening. Which @schallbert just explained :slight_smile: