REQUEST: Mgos_mqtt_pub callback C++ example

Hello

I admire the Mongoose OS project and appreciate the time and effort the developers have invested in making it available. However, I’m struggling with the omission of documentation and examples for the C++ API.

I’m trying to construct a simple MQTT mgos_mqtt_pub that resets a counter. This code:

static void reset_cb(struct mg_connection *c, const char *topic, int topic_len, const char *msg, int msg_len, void *userdata)
{
  cum_on_time_millis = 0;
}

enum mgos_app_init_result mgos_app_init(void)
{
  mgos_mqtt_global_subscribe("10/command/reset", reset_cb, NULL);
}

fails on (at least) two points:

(1) all the unused variables in reset_cb, which the compiler is treating as an error
(2) could not convert '(const char*)"10/command/reset"' from 'const char*' to 'mg_str'

I know enough C++ to realise that mgos_mqtt_global_subscribe requires some sort of mg_str, but without any documentation for mg_str, I don’t know what to do about it.

Could I trouble someone to provide a short example of subscribing to an MQTT topic using the C++ API, in the absence of one in the documentation? I do appreciate your time.

The mg_str API is defined in mg_str.h

The code for ESP8266 builds with -Werror which means all the warnings are treated as errors.

static void reset_cb(struct mg_connection *c, int ev, void *ev_data,
                     void *userdata) {
  cum_on_time_millis = 0;
  (void) c;
  (void) ev;
  (void) ev_data;
  (void) userdata;
}

extern "C"  enum mgos_app_init_result mgos_app_init(void) {
  mgos_mqtt_global_subscribe(mg_mk_str("10/command/reset"), reset_cb, NULL);
  return MGOS_APP_INIT_SUCCESS;
}
1 Like

I appreciate the response - thank you. For anyone picking this thread up, I see mg_mk_str is defined in API reference/Core/String