Enabling RPC MQTT.Pub over ESP32 serial 1 inside a Mongoose App

Hi guys,

I have created a Mongoose App which reads a sensor over ESP32 Serial port 2 (GPIO16 and 17) and publish sensor data on a MQTT Broker.
Now I would Like to activate RPC over UART to receive RPC json commands (MQTT.Pub) from an ATMEGA328 over ESP32 Serial port 1 (GPIOs 32 and 34) and publish to the same MQTT broker.
I tried to include CCM (cloud communication module) on my Mongoose App without success.
I will appreciate any directions about how can I do this.

Below my config schema and libs:

config_schema:

  • [“wifi.sta.enable”, true]

  • [“wifi.sta.ssid”, “********”]

  • [“wifi.sta.pass”, “********”]

  • [“dash.enable”, true]

  • [“dash.token”, “**************************”]

  • [“mqtt.server”, “192.168.10.200:1883”]

  • [“mqtt.enable”, true]

  • [“rpc”, “o”, {title: “RPC settings”}]

  • [“rpc.enable”, “b”, true, {title: “Enable RPC”}]

  • [“rpc.uart”, “o”, {title: “RPC-over-UART settings”}]

  • [“rpc.uart.uart_no”, “i”, 0, {title: “UART number (-1 to disable)”}]

  • [“rpc.uart.baud_rate”, “i”, 115200, {title: “Baud rate”}]

  • [“rpc.uart.tx_pin”, “i”, 32, {title: “Tx Pin”}]

  • [“rpc.uart.rx_pin”, “i”, 34, {title: “Rx Pin”}]

  • [“rpc.uart.cts_pin”, “i”, -1, {title: “CTS Pin”}]

  • [“rpc.uart.rts_pin”, “i”, -1, {title: “RTS Pin”}]

  • [“rpc.uart.fc_type”, “i”, 0, {title: “Flow control: 0 - none, 1 - CTS/RTS, 2 - XON/XOFF”}]

  • [“rpc.uart.dst”, “s”, “”, {title: “Destination reachable via this channel”}]

libs:

Thank You

Regards

José Luiz

@Jose_Luiz_Sanchez_Lo, you’re using an outdated version.

Please follow https://vcon.io/docs/#vcon--arduino-nano

Hi Ism, thanks for replay.

I am using a customized mongoose app on my ESP32, so the procedure you mentioned does not attend because it use a binary file firmware.
As I said before, I have a serial sensor on the other serial port that is controlled by a routine i included inside mongoose firmware. I used this link as a reference. (https://mongoose-os.com/docs/mongoose-os/quickstart/develop-in-c.md).

Please, let me know where can I find an example of how to use mongoose RPC-UART library (https://github.com/mongoose-os-libs/rpc-uart).

I will appreciate.

Thank you and Regards

José Luiz

From your description I have understood that you have two micros - one ESP32, another AVR. The AVR micro runs an Arduino firmware. For such setup, my link is correct.

If your setup is different, then please disregard. I have missed your point then.

Hi Ism,

Yes I have two micros as you understood, arduino send sensors readings in Json format (MQTT.PUB) to ESP32:

{“method”:“MQTT.Pub”,“params”:{“topic”:"/home/temperature",“message”:“28 oC”}}

But my ESP32 runs a customized mongoose app, that reads another sensor and generates a protocolBuffer message and publish it to MQTT.

Now I need to incorporate on ESP32 app, RPC-UART to get Json RPC from Arduino and also publish on MQTT.
The link you provide uses a .hex firmware who not allow customization to my ESP32 reads my other sensor. I need to use a customized firmware on ESP32.

I am trying to do the following inside mongoose App:

//adding a handler to MQTT.Pub on "mgos_app_init"
mg_rpc_add_handler(mgos_rpc_get_global(), "MQTT.Pub", "topic: %s, message: %s", mqtt_publish, args);

// and now I am trying to get "topic" and message from "MQTT.Pub" json
static void mqtt_publish(struct mg_rpc_request_info *ri, void *cb_arg,
                   struct mg_rpc_frame_info *fi, struct mg_str args) {

  char topic[50];
  char message[100];

  json_scanf(args.p, args.len, ri->args_fmt, &topic, &message);

bool res = mgos_mqtt_pub(topic,message, (len),1, false /* retain */);

 LOG(LL_INFO,("topic: %s , message: %d ",topic ,message));

  (void) cb_arg;
  (void) fi;
}

When I send Json over serial to ESP32 , it detects RPC command “MQTT.Pub” and “mqtt_publish” handler is called but I can not figure out how to get “topic” and “message” from json RPC command.

Do you have a direction for me ?

I will appreciate your help.

Thank you

José Luiz

Thank you @Jose_Luiz_Sanchez_Lo

What you are trying to do, is to reimplement the functionality of https://vcon.io
We already have that - and more, like Arduino firmware OTA, as a ready-to-go product.
Therefore either use vcon.io, or figure all the details on your own.

mg_rpc_add_handler(mgos_rpc_get_global(), "MQTT.Pub", "topic: %s, message: %s", mqtt_publish, args);

should be

  mg_rpc_add_handler(mgos_rpc_get_global(), "MQTT.Pub",
                     "{topic: %Q, message: %Q}", mqtt_publish, NULL);

and the handler

static void mqtt_publish(struct mg_rpc_request_info *ri, void *cb_arg,
                         struct mg_rpc_frame_info *fi, struct mg_str args) {
  char *topic = NULL;
  char *message = NULL;

  json_scanf(args.p, args.len, ri->args_fmt, &topic, &message);
  if ((topic != NULL) && (message != NULL)) {
    bool res =
        mgos_mqtt_pub(topic, message, strlen(message), 1, false /* retain */);
    LOG(LL_INFO, ("topic: %s , message: %d, res: %d", topic, message, res));
    mg_rpc_send_responsef(ri, NULL);
    goto clean;
  }
  mg_rpc_send_errorf(ri, 400, "topic and message are required");

clean:
  if (topic != NULL) {
    free(topic);
  }
  if (message != NULL) {
    free(message);
  }
  (void) cb_arg;
  (void) fi;
}
1 Like

Hi nliviu,

Thank You very much for your answer, now its running as expected.

Now I just need to remap ESP32 Serial 1 to GPIOs 32(TX) and 34(RX) .

I tried below changes on mos.yml but its not working.

  - ["rpc.uart", "o", {title: "RPC-over-UART settings"}]
  - ["rpc.uart.uart_no", "i", 1, {title: "UART number (-1 to disable)"}]
  - ["rpc.uart.baud_rate", "i", 115200, {title: "Baud rate"}]
  - ["rpc.uart.tx_pin", "i", 32, {title: "Tx Pin"}]
  - ["rpc.uart.rx_pin", "i", 34, {title: "Rx Pin"}]
  - ["rpc.uart.cts_pin", "i", -1, {title: "CTS Pin"}]
  - ["rpc.uart.rts_pin", "i", -1, {title: "RTS Pin"}]
  - ["rpc.uart.fc_type", "i", 0, {title: "Flow control: 0 - none, 1 - CTS/RTS, 2 - XON/XOFF"}]
  - ["rpc.uart.dst", "s", "", {title: "Destination reachable via this channel"}]

Regards

José Luiz