0

I'm trying to do an async webserver with access point using an ESP32.

It's a little bit of a mix between This tutorial for AP and async and this tutorial since it's also with an async webserver and it uses inputs which I desperately need.

The part I don't 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. It was never necessary to declare HTTP_GET since it should be a variable from a library.

So what's 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 +
      "<br><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><br>
  <form action="/get">
    input2: <input type="text" name="input2">
    <input type="submit" value="Submit">
  </form><br>
  <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.

0

1 Answer 1

2

Macro Conflict

You have two basic problems here. The more significant one is that your use of SERVER_H in your include guard:

#ifndef WEBSERVER_H
#define WEBSERVER_H

conflicts with a usage in ESPAsyncWebServer.h:

#ifndef WEBSERVER_H
typedef enum {
  HTTP_GET     = 0b00000001,
  HTTP_POST    = 0b00000010,
  HTTP_DELETE  = 0b00000100,
  HTTP_PUT     = 0b00001000,
  HTTP_PATCH   = 0b00010000,
  HTTP_HEAD    = 0b00100000,
  HTTP_OPTIONS = 0b01000000,
  HTTP_ANY     = 0b01111111,
} WebRequestMethod;
#endif

and that explains why your HTTP_GET etc are not found. They're not being defined because of your include guard macro.

Changing your code to:

#ifndef XENOSHELL_WEBSERVER_H
#define XENOSHELL_WEBSERVER_H

or something similar will get you past that problem.

Multiply Defined Variables

You also have some non-static variables defined in your webserver.h that will also need to be changed somehow. A simple change just being to make them static:

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

IPAddress IP; is only used in main.ino, and should be moved there as a local const IPAddress IP = WiFi.softAPIP(); If you leave it in the header you'll get multiple definitions because of that.

String inputMessage; and String inputParam; Same problem, only these belong inside your lambda expression in server.on("/get", HTTP_GET, [] (AsyncWebServerRequest * request) {

I don't know why your server variable is in the header either. The simplest thing to do is put it in webserver.cpp instead and don't pass it in to setup_server, in other word remove the parameter and use the variable defined in the same file as the function.


Having done that your code compiles fine. Whether or not it works is another question. I am unable to test that.

4
  • hello @timemage thanks for your very detailed answer. I got the code to compile too, but when i tried to connect to the wifi from the ESP the IP adress is not reachable. The output i get from serial monitor is " dhcps: send_offer>>udp_sendto result 0 ". Do you maybe know what could be the problem?
    – Xenoshell
    Jan 24, 2021 at 22:59
  • 1
    As of yet, I don't have an ESP32 board, so I have no way of troubleshooting this. What you're asking about seems like an entirely new question. I would say cut down your program to the minimal thing necessary to demonstrate the problem. Given the message you're quoting, your demo program should not have #include "ESPAsyncWebServer.h" or #include "AsyncTCP.h" in it at all. If you don't find the problem in the course of making the demo, post it as part of a new question.
    – timemage
    Jan 25, 2021 at 14:25
  • Ok still thanks for your answers, right now i settle on the version that works and if i have enough time i will come back to this problem
    – Xenoshell
    Jan 25, 2021 at 16:57
  • Thank you! This is a very helpful answer with expert analysis.
    – Sun Bee
    Sep 21, 2022 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.