mongoose-os通过MQTT调用RPC函数

版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/fengyu09/article/details/82219261

mongoose-os 更新很快,但是把教程删掉了。
mos.yml库配置:

libs:
  # - origin: https://github.com/mongoose-os-libs/ca-bundle
  - origin: https://github.com/mongoose-os-libs/core   - origin: https://github.com/mongoose-os-libs/mqtt   - origin: https://github.com/mongoose-os-libs/wifi   #- 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/rpc-loopback
  - origin: https://github.com/mongoose-os-libs/rpc-common   - origin: https://github.com/mongoose-os-libs/rpc-mqtt

反馈函数。命令写出后,需要发信息回来。

void rpc_response(struct mg_rpc_request_info *ri, char *json_fmt, ...)
{
  struct mbuf fb;
  struct json_out out = JSON_OUT_MBUF(&fb);
  mbuf_init(&fb, 256);
  // 不把内存区域填0,printf函数及返回的json 字符串都会出错。字符串以'\0'结尾
  // memset(fb.buf, 0, 256);
  printf("json_fmt:%s\n", json_fmt);
  va_list ap;
  va_start(ap, json_fmt);
  json_vprintf(&out, json_fmt, ap);
  va_end(ap);
  printf("out is:%s, len is:%d\n", fb.buf,fb.len);
  mgos_rpc_send_response(ri, fb.buf);
  //(void)ri;
  // free
  ri = NULL;
  mbuf_free(&fb);
}

json_vprintf(&out, json_fmt, ap); 将结果字符串写到out指针指定的区域。如果out的字符串,不以’\0’结尾,会打印以下结果:
这里写图片描述

命令行,调用格式:
mos –port mqtt://192.168.1.206:1883/esp8266_432DBA call cg_num “{\”sum\”:1}”

阅读更多 登录后自动展开

更多精彩内容