HTTP_GET not declared in scope

Hello everyone,

im trying to do an async webserver with access point using an ESP32.

Its a little bit of a mix between this tutorial for AP and async and this tutorial since its also with an async webserver and it uses inputs which i desperatly need.

The part i dont understand is i included all the necessary libraries and i still got this error. If i copy paste the code in one block everything is fine. So whats wrong?

These are my errors:

In file included from C:\..\Arduino\libraries\ESPAsyncWebServer-master\src/ESPAsyncWebServer.h:467:0,
                 from C:\..\AppData\Local\Temp\arduino_build_497972\sketch\webserver.h:5,
                 from C:\..\Async Webserver ESP32\main\main.ino:3:
C:\..\Arduino\libraries\ESPAsyncWebServer-master\src/WebHandlerImpl.h: In constructor 'AsyncCallbackWebHandler::AsyncCallbackWebHandler()':
C:\..\Arduino\libraries\ESPAsyncWebServer-master\src/WebHandlerImpl.h:76:49: error: 'HTTP_ANY' was not declared in this scope
     AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
                                                 ^
In file included from C:\..\Arduino\libraries\ESPAsyncWebServer-master\src/ESPAsyncWebServer.h:467:0,
                 from C:\..\AppData\Local\Temp\arduino_build_497972\sketch\webserver.h:5,
                 from C:\..\AppData\Local\Temp\arduino_build_497972\sketch\webserver.cpp:1:
C:\..\Arduino\libraries\ESPAsyncWebServer-master\src/WebHandlerImpl.h: In constructor 'AsyncCallbackWebHandler::AsyncCallbackWebHandler()':
C:\..\Arduino\libraries\ESPAsyncWebServer-master\src/WebHandlerImpl.h:76:49: error: 'HTTP_ANY' was not declared in this scope
     AsyncCallbackWebHandler() : _uri(), _method(HTTP_ANY), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
                                                 ^
C:\Users\Valentin\AppData\Local\Temp\arduino_build_497972\sketch\webserver.cpp: In function 'void setup_server(AsyncWebServer)':
webserver.cpp:6:18: error: 'HTTP_GET' was not declared in this scope
   server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
                  ^
Multiple libraries were found for "WiFi.h"
 Used: C:\..\Arduino15\packages\firebeetle32\hardware\esp32\0.1.1\libraries\WiFi
 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
Using library ESPAsyncWebServer-master at version 1.2.3 in folder: C:\..\Arduino\libraries\ESPAsyncWebServer-master 
Using library FS at version 1.0 in folder: C:\..\Arduino15\packages\firebeetle32\hardware\esp32\0.1.1\libraries\FS 
Using library WiFi at version 1.0 in folder: C:\..\Arduino15\packages\firebeetle32\hardware\esp32\0.1.1\libraries\WiFi 
Using library AsyncTCP-master at version 1.1.1 in folder: C:\..\Arduino\libraries\AsyncTCP-master 
exit status 1
'HTTP_GET' was not declared in this scope

I already tried to use the standard arduino WiFi library which resulted just in more errors so i switched it back.
I split up the code into three pieces for better visibility.
main.ino

#include <Arduino.h>

#include "webserver.h"

// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "blabla";
const char* password = "blabla";

void setup() {
  Serial.begin(115200);
  WiFi.softAP(ssid, password);
  IP = WiFi.softAPIP();

  Serial.println();
  Serial.print("IP Address: ");
  Serial.println(IP);
  
  setup_server(server);
}
void loop() {
}

webserver.cpp

#include "webserver.h"

void setup_server(AsyncWebServer server) {
  // Send web page with input fields to client
  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send_P(200, "text/html", index_html);
  });

  // Send a GET request to <ESP_IP>/get?input1=<inputMessage>
  server.on("/get", HTTP_GET, [] (AsyncWebServerRequest * request) {

    // GET input1 value on <ESP_IP>/get?input1=<inputMessage>
    if (request->hasParam(PARAM_INPUT_1)) {
      inputMessage = request->getParam(PARAM_INPUT_1)->value();
      inputParam = PARAM_INPUT_1;
    }
    // GET input2 value on <ESP_IP>/get?input2=<inputMessage>
    else if (request->hasParam(PARAM_INPUT_2)) {
      inputMessage = request->getParam(PARAM_INPUT_2)->value();
      inputParam = PARAM_INPUT_2;
    }
    // GET input3 value on <ESP_IP>/get?input3=<inputMessage>
    else if (request->hasParam(PARAM_INPUT_3)) {
      inputMessage = request->getParam(PARAM_INPUT_3)->value();
      inputParam = PARAM_INPUT_3;
    }
    else {
      inputMessage = "No message sent";
      inputParam = "none";
    }
    Serial.println(inputMessage);
    request->send(200, "text/html", "HTTP GET request sent to your ESP on input field ("
                  + inputParam + ") with value: " + inputMessage +
                  "
<a href=\"/\">Return to Home Page</a>");
  });
  server.onNotFound(notFound);
  server.begin();
}

void notFound(AsyncWebServerRequest *request) {
  request->send(404, "text/plain", "Not found");
}

webserver.h

#ifndef WEBSERVER_H
#define WEBSERVER_H

#include "Arduino.h"
#include "ESPAsyncWebServer.h"
#include "AsyncTCP.h"
#include <WiFi.h>

const char* PARAM_INPUT_1 = "input1";
const char* PARAM_INPUT_2 = "input2";
const char* PARAM_INPUT_3 = "input3";

AsyncWebServer server(80);

String music_state;
IPAddress IP;

String inputMessage;
String inputParam;

// HTML web page to handle 3 input fields (input1, input2, input3)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
  <title>ESP Input Form</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  </head><body>
  <form action="/get">
    input1: <input type="text" name="input1">
    <input type="submit" value="Submit">
  </form>

  <form action="/get">
    input2: <input type="text" name="input2">
    <input type="submit" value="Submit">
  </form>

  <form action="/get">
    input3: <input type="text" name="input3">
    <input type="submit" value="Submit">
  </form>
</body></html>)rawliteral";

void setup_server(AsyncWebServer server);

void notFound(AsyncWebServerRequest *request);

#endif

Thanks for the input, i will check back tomorrow and answer all questions and test every suggestion

The code compiles if all three files are copied into one. This is caused by the way the Arduino IDE identifies the libraries to be included and compiled.

You can split it up but your separation is quite bad as you're using globals defined in the .h file.

I copied the code of webserver.cpp and webserver.h into a webserver.ino file and inserted the following lines into main.ino:

#include "ESPAsyncWebServer.h"
#include "AsyncTCP.h"
#include <WiFi.h>

extern IPAddress IP;
extern AsyncWebServer server;

That way it compiles too.

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