Copyright ©

Mongoose OS Forum

ATTENTION! This forum has moved to:

Do not post any new messages.

How to use mgos_connect()

rodrmigurodrmigu Fairfax, Virginia


I'm trying to use mgos_connect() but I'm getting compiler errors. Any suggestions?

Thanks in advance


  CC    /fwbuild-volumes/2.9.0/apps/my-app/esp8266/build_contexts/build_ctx_636252458/libs/atca/cryptoauthlib/lib/atca_command.c
/fwbuild-volumes/2.9.0/apps/my-app/esp8266/build_contexts/build_ctx_636252458/src/main.c: In function 'wifi_ev_handler':
/fwbuild-volumes/2.9.0/apps/my-app/esp8266/build_contexts/build_ctx_636252458/src/main.c:21:16: error: implicit declaration of function 'mgos_connect' [-Werror=implicit-function-declaration]
         struct mg_connection *c = mgos_connect("udp://", udp_ev_handler, NULL);
/fwbuild-volumes/2.9.0/apps/my-app/esp8266/build_contexts/build_ctx_636252458/src/main.c:21:35: error: initialization makes pointer from integer without a cast [-Werror]
         struct mg_connection *c = mgos_connect("udp://", udp_ev_handler, NULL);
cc1: all warnings being treated as errors
make: *** [/fwbuild-volumes/2.9.0/apps/my-app/esp8266/build_contexts/build_ctx_636252458/build/objs/main.c.o] Error 1
/mongoose-os/fw/platforms/esp8266/ recipe for target '/fwbuild-volumes/2.9.0/apps/my-app/esp8266/build_contexts/build_ctx_636252458/build/
objs/main.c.o' failed
make: *** Waiting for unfinished jobs....


#include "mgos.h"
#include "mgos_mongoose.h"

#include "mgos_net.h"
#include "mgos_wifi.h"

static void udp_ev_handler(struct mg_connection *nc, int ev, void *ev_data, void *user_data) {
    LOG(LL_INFO, ("%s %d", "UDP Event", ev));
    if (ev == MG_EV_CONNECT) {
        const char *msg = "Hello World";
        mg_send(nc, msg, strlen(msg));

    (void) user_data;
    (void) ev_data;
    (void) nc;    

static void wifi_ev_handler(int ev, void *evd, void *arg) {
    if (ev == MGOS_NET_EV_CONNECTED) {
        struct mg_connection *c = mgos_connect("udp://", udp_ev_handler, NULL);
        if (c != NULL) {
            const char *msg = "Hello World Again";
            mg_send(c, msg, strlen(msg));


enum mgos_app_init_result mgos_app_init(void) {
  mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, wifi_ev_handler, NULL);

partial mos.yml

  - origin:
  - origin:
  - origin:
  - origin:
  - origin:
  - origin: 


  • mgos_connect is in mgos_mongoose_internal.h.

    Thanked by 1rodrmigu
  • rodrmigurodrmigu Fairfax, Virginia

    Thanks nliviu.

    That helped with compiler errors, however it's not working in Esp8266, but it works fine in Esp32.
    I've a known issue using PlatformIO and Esp8266 regarding UDP, which only works if I use something like this, but this doesn't works with mgos.

    I also tried your Example which works ok only for Esp32.

    Any ideas or suggestions?


  • rodrmigurodrmigu Fairfax, Virginia


    Just an update. I had to change to the wifi_ev_handler to MGOS_NET_EV_IP_ACQUIRED, so need to wait for ip to be acquired, of course :smile:
    Unfortunately Esp8266 only works if I use I was expecting that, based on previous experience, that's ok since my application server handles both, multicast and local ip.
    Esp32 works using both: or


  • What do you mean by "it's not working"?

  • rodrmigurodrmigu Fairfax, Virginia
    edited December 2018

    There was a mistake about MGOS_NET_EV_IP_ACQUIRED, explained in the previous post


Sign In or Register to comment.