mongoose-os通过MQTT调用RPC函数

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}”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容沁风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值