nRF Cloud - sending custom data to custom topic

Hi 

I'm currently trying to modify the nRF Cloud multi-service sample (SDK2.6) to send custom data to custom topics. But I can't find a way to do this. 

I want to send a JSON package like this to the custom topic "tdg":

{
"ts":"1726226122",
"loc":"1000",
"id":"123456789",
"at":"2055",
"ap":"999",
"wt":"1255",
"wp":"2121",
"gt":"1255",
"gp":"2222"
}


Other similar messages should be sent to other topics ("pH", "O2"). Is this possible? And do you have some sample code? 

Parents
  • I would start from send_sensor_sample in nRF Cloud multi-service sample. That sends a single numeric value to a sensor appId ("TEMP" and "COUNT" in this sample).

    If I understand correctly, you want to send to appId "tdg", and send an object rather than a single value. Change `sensor` to "tdg" in create_timestampled_device_message call.

    Where `send_sensor_sample` calls nrf_cloud_obj_num_add, you'll want to use nrf_cloud_obj_object_add, but before calling that you'll need to create the object that you'll pass to it.

    There's a convenience macro to do that: NRF_CLOUD_OBJ_JSON_DEFINE, then call nrf_cloud_obj_init, and add the fields above. i.e. nrf_cloud_obj_str_add(obj, "gp", "2222", false).

    Remember to call nrf_cloud_obj_free if there are errors, to avoid memory leaks.

    Mariano

  • Thanks a lot, Mariano.

    This will certainly work, but maybe I wasn't clear about my objective. I want to send the object to a custom topic. We have a cloud service that subscribe to topics like: {mqtt_config.topic_prefix}m/d/{mqtt_config.client_id}/d2c/tdg, and hence, I want to send the JSON data to the same topic. The solution you suggest will send the data to {mqtt_config.topic_prefix}m/d/{mqtt_config.client_id}/d2c since this is the default for nrf cloud library (?). Using "tdg" in create_timestampled_device_message call will result in 

    appId:"TDG"
    messageType:"DATA"
    ts:1731675207965
    in nrf cloud, but it will not change the topic sent to(?).
Reply
  • Thanks a lot, Mariano.

    This will certainly work, but maybe I wasn't clear about my objective. I want to send the object to a custom topic. We have a cloud service that subscribe to topics like: {mqtt_config.topic_prefix}m/d/{mqtt_config.client_id}/d2c/tdg, and hence, I want to send the JSON data to the same topic. The solution you suggest will send the data to {mqtt_config.topic_prefix}m/d/{mqtt_config.client_id}/d2c since this is the default for nrf cloud library (?). Using "tdg" in create_timestampled_device_message call will result in 

    appId:"TDG"
    messageType:"DATA"
    ts:1731675207965
    in nrf cloud, but it will not change the topic sent to(?).
Children
Related