Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

How to use mgos_connect()

rodrmigurodrmigu Fairfax, Virginia

Hello,

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

Thanks in advance
Rodrigo

output

  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://239.255.255.250:1900", 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://239.255.255.250:1900", 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/Makefile.build:415: 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....

main.c

#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://239.255.255.250:1900", udp_ev_handler, NULL);
        if (c != NULL) {
            const char *msg = "Hello World Again";
            mg_send(c, msg, strlen(msg));
        }            
    }

    (void)evd;
    (void)arg;
}

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

partial mos.yml

libs:
  - origin: https://github.com/mongoose-os-libs/mongoose
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/wifi
  - origin: https://github.com/mongoose-os-libs/mqtt
  - origin: https://github.com/mongoose-os-libs/mjs 

Comments

Sign In or Register to comment.