# Callback function issue

**URL:** <https://forum.arduino.cc/t/callback-function-issue/689636>\
**Category:** Programming\
**Created:** [January 3, 2021, 12:40pm UTC](https://forum.arduino.cc/t/callback-function-issue/689636 "2021-01-03T12:40:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogiknaupi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rogiknaupi/32/652038_2.png) [@rogiknaupi](https://forum.arduino.cc/u/rogiknaupi)\
**Post date:** [January 3, 2021, 12:40pm UTC](https://forum.arduino.cc/t/callback-function-issue/689636/1 "2021-01-03T12:40:24Z")

</div>

Hi,  
I have created a simple lib to handle server calls of ESPAsyncWebServer with template handling function.  
If I use that in my main program it is working but I want to sent the callback funtion of my main program to the lib.  
I get the following error: 'callback' is not captured.

This is the code of my main program and the lib:

```cpp
**************************************WebServerTemplate.ino**************************************

#include LittleFS.h>

#include <RokTools.h>

#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

AsyncWebServer server(80);

String processor(const String& var){
  Serial.println("process variable " + var);
  if(var == "TITLE"){
    return "ESP - Template";
  }
  return String();
}

void setup() {
  Serial.begin(115200);
  if (LittleFS.begin()) {
    Serial.println("Filesystem mounted");
  }
  else {
    Serial.println("ERROR!!! Filesystem not mounted...");
  }
  addDefaultServerRoutes(&server, processor);
  server.begin();
}

void loop() {}

**************************************RokTools.h**************************************
#ifndef __ROKTOOLS__
#define __ROKTOOLS__
#include <ESPAsyncWebServer.h>    //Local WebServer used to serve the configuration portal
#include "Arduino.h"
extern void addDefaultServerRoutes(AwsTemplateProcessor callback);
#endif

**************************************RokTools.cpp**************************************
#include "RokTools.h"

#include <LittleFS.h>            //this needs to be first, or it all crashes and burns...
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
#include "ESPAsyncWebServer.h"

#include "Arduino.h"

void addDefaultServerRoutes(AsyncWebServer *server, AwsTemplateProcessor callback) {
  server->on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    //COMPILER ERROR =>  'callback' is not captured !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    request->send(LittleFS, "/index.html", String(), false, callback);
  });
}

```

It would be great if someone can help me!

Thanks a lot,  
Roger

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [January 3, 2021, 12:53pm UTC](https://forum.arduino.cc/t/callback-function-issue/689636/2 "2021-01-03T12:53:38Z")

</div>

You have to capture the callback variable if you want to use it inside of your lambda function:

```cpp
void addDefaultServerRoutes(AsyncWebServer *server, AwsTemplateProcessor callback) {
  server->on("/", HTTP_GET, [callback](AsyncWebServerRequest *request){
    request->send(LittleFS, "/index.html", String(), false, callback);
  });
}

```

[https://en.cppreference.com/w/cpp/language/lambda#Lambda\_capture](https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture)

Pieter

---

<div class="post-metadata">

**Author:** ![rogiknaupi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rogiknaupi/32/652038_2.png) [@rogiknaupi](https://forum.arduino.cc/u/rogiknaupi)\
**Post date:** [January 3, 2021, 1:11pm UTC](https://forum.arduino.cc/t/callback-function-issue/689636/3 "2021-01-03T13:11:10Z")

</div>

It works!  
Perfect... Thanks....  
Roger

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:55am UTC](https://forum.arduino.cc/t/callback-function-issue/689636/4 "2021-05-06T00:55:43Z")

</div>

Closed on May 6, 2021, 12:55am UTC


