Global variables / Pointer ESP32 Face Recognition

Hello poeple,

my knowledge gaps in programming c/c++ are preventing me from moving further in my small project.

I have an ESP32-Cam and i want to use face recognition in combination with mqtt. So i modified the example with an mqtt server etc, everything is working till that.

The Problem is, that to the example project belongs a secend file "app_httpd.cpp" (where the face recognition is done). So in the second file i cant access my mqtt-instance (PubSubClient) which is defined in the main file.

Is there any way how i can solve that? Sorry, i know the example is above my level but maybe ich can put it into operation anyway.

Thank you very much!

Got some code? Or at least links to GitHub code?

Below are the two files. In the function where i want to put the code in, i have writte down "// MARKER" for you (second file). I cut off not important parts, cause the maximum allowed length here.

Thank you very much!

Main-File: ESP32-Cam-FaceRecognitionMQTT

WiFiClient espClient; 
PubSubClient client(espClient); 
void startCameraServer(); 

void setup() {

  Serial.println("Init Cam");
  // camera init
  esp_err_t err = esp_camera_init(&config); //SBS esp_camera_init ist eine Funktion zur Initialisierung der Kamera (aus esp_camera.h). esp_err_t ist ein Datentyp für ESP-Errors
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);

  Serial.println("Start Wifi");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");


  client.setServer(mqtt_server, 1883);


void loop() {
  if (!client.connected()) { 
  long now = millis();
  if (now - lastMsg > 10000) {
    lastMsg = now;
    snprintf (msg, 50, "hello world #%ld", value);
    Serial.print("Publish message: ");
    client.publish("esp32/ichbinda", msg);

Second file "app_httpd.cpp"

static int run_face_recognition(dl_matrix3du_t *image_matrix, box_array_t *net_boxes){
    dl_matrix3du_t *aligned_face = NULL;
    int matched_id = 0;

    aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);
        Serial.println("Could not allocate face recognition buffer");
        return matched_id;
    if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK){
        if (is_enrolling == 1){
            //int8_t left_sample_face = enroll_face(&id_list, aligned_face);
            int8_t left_sample_face = enroll_face_id_to_flash(&id_list, aligned_face); //SBS

            if(left_sample_face == (ENROLL_CONFIRM_TIMES - 1)){
                Serial.printf("Enrolling Face ID: %d\n", id_list.tail);
            Serial.printf("Enrolling Face ID: %d sample %d\n", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
            rgb_printf(image_matrix, FACE_COLOR_CYAN, "ID[%u] Sample[%u]", id_list.tail, ENROLL_CONFIRM_TIMES - left_sample_face);
            if (left_sample_face == 0){
                is_enrolling = 0;
                Serial.printf("Enrolled Face ID: %d\n", id_list.tail);
        } else {
            matched_id = recognize_face(&id_list, aligned_face);
            if (matched_id >= 0) {
                Serial.printf("Match Face ID: %u\n", matched_id);
                rgb_printf(image_matrix, FACE_COLOR_GREEN, "Hello Subject %u", matched_id);
// Here i want to run code, publish MQTT for example:
// client.publish("esp32/ichbinda", msg);
// but it is not the realm of the object "client"

            } else {
                Serial.println("No Match Found");
                rgb_print(image_matrix, FACE_COLOR_RED, "Intruder Alert!");
                matched_id = -1;
    } else {
        Serial.println("Face Not Aligned");
        //rgb_print(image_matrix, FACE_COLOR_YELLOW, "Human Detected");

    return matched_id;

The snippets are insufficient. Can't see how the project fits together. Is this your code? If not, where'd you get the examples?

The main code is the arduino ide example for ESP32. (ESP32 -> Camera -> CameraWebserver). I added the MQTT (PubSub)-Parts.

Sorry i dont know what is missing for you. Its only about an object in the "main" file (that with setup() and loop() function) and how i can access this object from the additional project file "app_httpd.cpp" which is within the project.

The easiest why would be to make the object global across files. Declare it as ‘extern’ both in the file where it’s defined and the other file where you want to use it:


Sadly i didnt make it to make the "client"-Objekt global, but instead i have done it with an simple string, so in the main code i get the string if it changes and publish it via mqtt.

Thank you very much!!!