Code help needed coming from particle

i am trying to migrate my designs from the particle cloud and p0 mcu, to the esp32 platform and mongoose OS. while i understand the concepts, the api is a bit different and i am having trouble converting my code from the previous platform. this is just an excerpt of what i am after. also, i dont quite get where my voids are, which one is for one time variables (setup), which one loops (loop)?
simple things like mqtt sub/pub, reading values etc, are not so obvious to me now.

void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  returned = sscanf(p, "r=%d, g=%d, b=%d, l=%d, c=%d, p=%c", &red, &green, &blue, &luminosity, &control, &pattern);
  }

Did you read Mongoose OS quick start quide?
If you are developing in C, replace demo-js with demo-c in step 4.

Another good to read topic is Advanced guide: C/C++

Also there is a good selection of apps here

yes, followed the guides rather extensively, but adding features is boggling me. MQTT for example, yes, the examples work, but if I try to use the example code on the demo app, nothing happens. I’m fairly positive that I put the sub/pub on teh wrong section, but I dont have a “void loop” so I’m stuck. I’ll give the advanced doc a read now too, thanks!

Are you trying to use c code or mjs?
Aws or Mqtt server?

C code, here is my app init. I have a MQTT server on my private network that I am trying to get this talking to. credentials and address are in my mos.yml already

enum mgos_app_init_result mgos_app_init(void) {
  char buf[8];
  s_strip = mgos_neopixel_create(PIN, NUM_PIXELS, ORDER);
  mgos_set_timer(150 /* ms */, MGOS_TIMER_REPEAT, pixel_timer_cb, NULL);
  /* Simple repeating timer */
  mgos_set_timer(1000, MGOS_TIMER_REPEAT, timer_cb, NULL);

  /* Publish to MQTT on button press */
      int btn_pin = mgos_sys_config_get_board_btn1_pin();
  if (btn_pin >= 0) {
    enum mgos_gpio_pull_type btn_pull;
enum mgos_gpio_int_mode btn_int_edge;
if (mgos_sys_config_get_board_btn1_pull_up()) {
  btn_pull = MGOS_GPIO_PULL_UP;
  btn_int_edge = MGOS_GPIO_INT_EDGE_NEG;
} else {
  btn_pull = MGOS_GPIO_PULL_DOWN;
  btn_int_edge = MGOS_GPIO_INT_EDGE_POS;
}
LOG(LL_INFO, ("Button pin %s, active %s", mgos_gpio_str(btn_pin, buf),
              (mgos_sys_config_get_board_btn1_pull_up() ? "low" : "high")));
mgos_gpio_set_button_handler(btn_pin, btn_pull, btn_int_edge, 20, button_cb,
                             NULL);
  }

  /* Network connectivity events */
  mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_cb, NULL);

#ifdef MGOS_HAVE_WIFI
  mgos_event_add_group_handler(MGOS_WIFI_EV_BASE, wifi_cb, NULL);
#endif

  mgos_event_add_handler(MGOS_EVENT_CLOUD_CONNECTED, cloud_cb, NULL);
  mgos_event_add_handler(MGOS_EVENT_CLOUD_DISCONNECTED, cloud_cb, NULL);
  mgos_mqtt_sub("my/#", handler, NULL);       /* Subscribe */
  mgos_mqtt_pub("my/topic", "hi", 2, 1, 0);   /* Publish */

  return MGOS_APP_INIT_SUCCESS;
}

The pub and sub lines need to be moved inside the tmer_cb handler or inside the btn event handler

ok, making progress, so timer_cb is my “loop”. MQTT is posting, but to the internal server, not my defined one. double checking my yml now.

See this

got it, i added the port to my server define, which is the default, but weird, ok, good here not to get my subscribe parser implemented :wink: thanks for the help!

Mongoose os uses events for mjs and c code. If not using events in your c code, then you create a main loop in your main.c

I believe that if you don’t specify a port it defaults to 1883

There is a built-in json library for handling json messages, objects and strings
https://mongoose-os.com/docs/mongoose-os/api/core/frozen.h.md

its supposed to work taht way accoring to the docs, but I added 1883 and it fired right up…
on the loop part, so I can just create a void loop() and it would work? sorry, not a real programmer, learning by trial and error still.

I found that starting with mjs a bit less of a struggle getting things to work. Then adding in c calls as needed. No main loop needed with mjs. Stick to timers and events then. Mjs has lots of hooks.

ah, cool. ok, last question for now, hopefully. Is there a list or document of all the internal calls? if i want to use the DeviceID, or MAC, or uptime, etc, as a varible? if you know particle, I want to do this with Mongoose:

 strcpy(DevID, (const char*)System.deviceID());
 snprintf(MY_ID, sizeof(MY_ID), "%s%s", Project, DevID);
 snprintf(Update_ID, sizeof(Update_ID), "%s-update", MY_ID);
client.connect(MY_ID)
client.subscribe(Update_ID);
...
client.publish(MY_ID, payload);

There are two topics, one for publishing, one for subbing, per device.

Mjs demo example

Mjs:
Use let w=Cfg.get (‘mqtt.whatever’), w has whatever in it. Similar call for c code.
Device Id has a value use mos config-get to see.
Never seen particle.

so
let MY_ID = cfg.get(mac);

would pull the MAC and set it as MY_ID, right?

Something like that for every variable in config-get. You can also create your own vars in yml file. Need quotes around var in call.
Also in yml file the ??? Are derived from Mac address. Removing and adding ??? Sub in from the
Mac.
See config-get for ssid for example.