Mqtt example test failed

#1

Hi, I am trying to test the mqtt example in C/C++ but I cannot make it work.
mos build keeps complaining as follow

make: Entering directory '/app'
  CC    /data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/src/main.c
/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/src/main.c:51:15: error: expected declaration specifiers or '...' before string constant
 mgos_mqtt_pub("my/topic", "hi", 2, 1, 0);   /* Publish */
               ^
/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/src/main.c:51:27: error: expected declaration specifiers or '...' before string constant
 mgos_mqtt_pub("my/topic", "hi", 2, 1, 0);   /* Publish */
                           ^
/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/src/main.c:51:33: error: expected declaration specifiers or '...' before numeric constant
 mgos_mqtt_pub("my/topic", "hi", 2, 1, 0);   /* Publish */
                                 ^
/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/src/main.c:51:36: error: expected declaration specifiers or '...' before numeric constant
 mgos_mqtt_pub("my/topic", "hi", 2, 1, 0);   /* Publish */
                                    ^
/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/src/main.c:51:39: error: expected declaration specifiers or '...' before numeric constant
 mgos_mqtt_pub("my/topic", "hi", 2, 1, 0);   /* Publish */
                                       ^
/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/deps/modules/mongoose-os/platforms/esp8266/Makefile.build:398: recipe for target '/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/build/objs/main.c.o' failed
make: *** [/data/fwbuild-volumes/latest/apps/app2/esp8266/build_contexts/build_ctx_504698158/build/objs/main.c.o] Error 1
make: Leaving directory '/app'
Error: exit status 2
/go/src/github.com/mongoose-os/mos/mos/build_local.go:679: 
/go/src/github.com/mongoose-os/mos/mos/build_local.go:666: 
/go/src/github.com/mongoose-os/mos/mos/build_local.go:431: 
/go/src/github.com/mongoose-os/mos/mos/build.go:223: 
/go/src/github.com/mongoose-os/mos/mos/build.go:166: 
/go/src/github.com/mongoose-os/mos/mos/main.go:196: build failed
Error: /build/mos-latest-CaoOjL/mos-latest-201906271728+d37d397~disco0/go/src/github.com/mongoose-os/mos/mos/build_remote.go:326: build failed
/build/mos-latest-CaoOjL/mos-latest-201906271728+d37d397~disco0/go/src/github.com/mongoose-os/mos/mos/build.go:223: 
/build/mos-latest-CaoOjL/mos-latest-201906271728+d37d397~disco0/go/src/github.com/mongoose-os/mos/mos/build.go:166: 
/build/mos-latest-CaoOjL/mos-latest-201906271728+d37d397~disco0/go/src/github.com/mongoose-os/mos/mos/main.go:196: build failed
exit status 1

I tried to replace " by ’ single quotes’ .

No success so far. Really intrested to implement but , it is quite frustrating…
Thanks a lot in advance.

Any example where we can check? all examples in github are JS not C/C++.

Also subscribing handler complaints with example.

unused parameter 'c' [-Werror=unused-parameter]
 static void handler(struct mg_connection *c, const char *topic, int topic_len, const char *msg, int msg_len, void *userdata) {...

The code I am trying to run is this one

#include "mgos.h"
#include "mgos_dht.h"
#include "mgos_rpc.h"
#include "mgos_mqtt.h"

static void timer_cb(void *dht) {
  LOG(LL_INFO, ("Temperature: %lf Humidity: %lf", mgos_dht_get_temp(dht), mgos_dht_get_humidity(dht)));
}


static void rpc_cb(struct mg_rpc_request_info *ri, void *cb_arg,
                   struct mg_rpc_frame_info *fi, struct mg_str args) {
  mg_rpc_send_responsef(ri, "{value: %lf}", mgos_dht_get_temp(cb_arg));
  (void) fi;
  (void) args;
}


enum mgos_app_init_result mgos_app_init(void) {
  struct mgos_dht *dht = mgos_dht_create(mgos_sys_config_get_app_pin(), DHT22);
  mgos_set_timer(1000, true, timer_cb, dht);
  mg_rpc_add_handler(mgos_rpc_get_global(), "Temp.Read", "", rpc_cb, dht);
  //mg_rpc_add_handler(mgos_rpc_get_global(), "Hum.Read", "", rpc_cb, dht);

  return MGOS_APP_INIT_SUCCESS;
}

static void handler(struct mg_connection *c, const char *topic, int topic_len, const char *msg, int msg_len, void *userdata) {
  LOG(LL_INFO, ("Got message on topic %.*s", topic_len, topic));

}

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

Your C code is not valid. Calling mgos_mqtt_sub and mgos_mqtt_pub outside any function is not permitted.

You can move mgos_mqtt_sub in mgos_app_init and mgos_mqtt_pub in the timer callback.