nRFCloud / device-simulator-v2

An AWS IoT Thing simulator for nRF91 that does DFU, sensors and leverages the Device API
Other
12 stars 1 forks source link

Receiving c2d messages not working? #20

Open terrencebarr opened 1 week ago

terrencebarr commented 1 week ago

Hi,

I am using the device simulator to send d2c messages to nRF Cloud and have added my own service type. Works fine.

Now I am trying to receive c2d messages from nRF Cloud. According to the device simulator README.md, once the simulator displays subscribed to "dev/<your_tenant_id>/<your_device_id>/jobs/rcv" it should be "onboarded and ready to start sending and receiving device messages".

However, receiving is not working for me, or maybe I am misunderstanding how it is supposed to work.

I am using the nRF Cloud API with curl to send messages to topic d/<device_id>/c2d as well as d/<device_id>/c2d/r and I can verify that messages for the device_id have been published in nRF Cloud by subscribing with a MQTT tool.

I would expect the device simulator to subscribe to the c2d topic(s) and log these incoming messages via the client.on('message', ...) callback, but I don't see that happening.

While investigating, I realized I am not seeing a subscription message for c2d topic(s) during the device simulator startup, so I added a c2d topic in class NrfDevice.ts, like

        this.topics = {
            d2c: `${mqttMessagesPrefix}d/${deviceId}/d2c`,
            c2d: `${mqttMessagesPrefix}d/${deviceId}/c2d/r`,
            jobs: {
                request: `${stage}/${tenantId}/${deviceId}/jobs/req`,
                receive: `${stage}/${tenantId}/${deviceId}/jobs/rcv`,
                update: `${stage}/${tenantId}/${deviceId}/jobs/update`,
            },

but the c2d topic is still not subscribed during device simulator startup.

Is there an issue with the device simulator code with regard to receiving c2d messages? Or am I misunderstanding something?

Thanks, -- Terrence

terrencebarr commented 1 week ago

Update: After updating nrfDevice.ts in the following way

...
      await apiConn
        .get(`v1/devices/${deviceId}`)
        .then(async (res: AxiosResponse) => {
          if (res?.data?.tenantId === tenantId) {
            log.success(
              `confirmed that "${deviceId}" has been onboarded with account "${tenantId}"!`,
            );
            deviceAssociated = true;

            // subscribe to c2d messages
            const c2dtopic = `${mqttMessagesPrefix}d/${deviceId}/c2d/r`;
            await device.subscribe(c2dtopic);
            device.registerListener(c2dtopic, (message: { topic: string; payload: object}) => 
            {
              console.log(`listener called for ${message.topic}`);
            });
...

the c2d listener is now called when c2d messages are published in the nRF Cloud.

May I suggest to update the repo to either:

  1. modify the README.md to explain that c2d messages are not supported by default and a subscription and handler needs to be added

or preferably

  1. add a configuration option that enables a c2d topic subscription and default handler

Thanks for the device-simulator-v2 in general, it's a great starting point!

Thanks, -- Terrence

savvyintegrations commented 4 days ago

Hi @terrencebarr! Thanks for your issue. We're glad you've gotten some benefit from our simulator. I have fixed the issue with https://github.com/nRFCloud/device-simulator-v2/pull/19/commits/22efd89b9fa51aecd115fd55181737ab57ec7e11. That PR is still in code review and testing, but feel free to try running the simulator using that branch. Please note, that PR is a major overhaul of simulator, so read the CLI options carefully, b/c things have changed.

Please don't hesitate to reach out with any suggestions for improvement, or questions. Thanks!

terrencebarr commented 3 days ago

@savvyintegrations ok, will do, thanks!