MQTT custom outbox implementation

FrankJensen
Posts: 51
Joined: Sun Mar 10, 2024 9:34 pm

MQTT custom outbox implementation

Postby FrankJensen » Mon Jun 03, 2024 8:36 pm

Hi Forum.

I have tried to implement a custom outbox for esp-mqtt. I would like to save some RAM and move it to PSRAM. But I am lacking a good example, and have some difficulties. Can someone point me to a good implementation?

My problem is, as soon as I enable custom outbox in menuconfig, I get a lot of link-errors, even though I think, I have implemented the needed functions in my code, like outbox_set_pending and process_outbox.

I use ESP32-S3 and the latest ESP-IDF.

Any help would be apreciated. A link to a good implementation would be great :-)

Br. Frank.

FrankJensen
Posts: 51
Joined: Sun Mar 10, 2024 9:34 pm

Re: MQTT custom outbox implementation

Postby FrankJensen » Wed Jun 05, 2024 8:34 pm

Here is my aproach. What am I doing wrong?

Initially I get undefined reference to `outbox_set_pending'

Then I add this
CMakeList.txt
  1. idf_component_register(SRCS "main.cpp" "outbox.cpp"
  2.             INCLUDE_DIRS "."
  3.             REQUIRES driver freertos u8g2
  4.             PRIV_REQUIRES esp-mqtt)
and then I get Failed to resolve component 'esp-mqtt'

I have checked, that the esp-mqtt is indeed in my package - I just upgraded to 5.2.1.

I have implemented the outbox like this (preliminary code)

Code: Select all

#ifndef OUTBOX_H
#define OUTBOX_H

#include "mqtt_client.h"

void setup_outbox();
void publish_message(const char* topic, const char* payload, int qos);
void outbox_set_pending(const char* topic, const char* payload, int qos);
void outbox_destroy();

#endif // OUTBOX_H

Code: Select all

#include <esp_log.h>
#include <esp_heap_caps.h>
#include "mqtt_client.h"
#include "outbox.h"
#include "sdkconfig.h"

static const char* TAG = "Outbox";
extern esp_mqtt_client_handle_t client;

// Example structure to hold messages, using PSRAM
typedef struct {
    char* topic;
    char* payload;
    int qos;
} outbox_message_t;

void setup_outbox() {
    ESP_LOGI(TAG, "Outbox setup complete");
}

void outbox_set_pending(const char* topic, const char* payload, int qos) {
    // Allocate memory in PSRAM for the outbox message
    outbox_message_t* message = (outbox_message_t*) heap_caps_malloc(sizeof(outbox_message_t), MALLOC_CAP_SPIRAM);
    if (message == NULL) {
        ESP_LOGE(TAG, "Failed to allocate memory for outbox message");
        return;
    }

    // Allocate memory for the topic and payload in PSRAM
    message->topic = (char*) heap_caps_malloc(strlen(topic) + 1, MALLOC_CAP_SPIRAM);
    message->payload = (char*) heap_caps_malloc(strlen(payload) + 1, MALLOC_CAP_SPIRAM);
    if (message->topic == NULL || message->payload == NULL) {
        ESP_LOGE(TAG, "Failed to allocate memory for message content");
        heap_caps_free(message);
        return;
    }

    strcpy(message->topic, topic);
    strcpy(message->payload, payload);
    message->qos = qos;

    // Store the message in the outbox
    ESP_LOGI(TAG, "Stored message in outbox: topic=%s, payload=%s, QoS=%d", topic, payload, qos);
    // Add code to manage the storage and retrieval of outbox messages
}

void outbox_destroy() {
    // Implementation for destroying the outbox and freeing memory
    ESP_LOGI(TAG, "Destroying outbox");
    // Add code to release resources used by the outbox
}

void process_outbox() {
    // Implementation to process and resend messages from the outbox
    ESP_LOGI(TAG, "Processing outbox");
    // Add code to handle processing of outbox messages
}
I should mention, that I have set 'Enable custom outbox implementation' in the menuconfig. I have NOT set the 'MQTT using custom configurations'.

FrankJensen
Posts: 51
Joined: Sun Mar 10, 2024 9:34 pm

Re: MQTT custom outbox implementation

Postby FrankJensen » Fri Jun 07, 2024 7:51 am

I simply can not get this to work, tried several aproaches.
Is this custom outbox implementation working in IDF 5.2.1?

If anyone have a working implementation, I would really like to see it. Any input would be apreciated, I am stuck....

Br. Frank.

Who is online

Users browsing this forum: Bing [Bot], derricksenva and 241 guests