Particle.subscribe() not listening

This seems like an obvious question, so I’m sorry in advance if it has been asked and answered before. I could not find a post with this set of facts.

I’ve got a device running 1.4.4 in AUTOMATIC mode that I am using the following communication model:

  • Device starts up and registers a subscription to {deviceId}/hook-response
  • Device publishes data
  • Webhook sends data to my API
  • My API responds with data for device
  • Particle cloud sends the event to {deviceId}/hook-response

This works great most of the time. However, in some cases the device can publish data but it does not get the hook response via its subscription.

I can fix the issue by disconnecting the device from the Particle cloud:

curl --request PUT \
  --url 'https://api.particle.io/v1/products/{productId}/devices/{deviceId}/disconnect?access_token={token}'

I imagine this works because it causes the device to reregister its subscription, which means that somehow the device either never created the subscription or it lost the subscription somehow.

I’m about to codify this into my backend or application firmware via some sort of logic that disconnects a device if it experiences this issue. But before doing that, I’m wondering if anyone here has best practices related to maintaining subscriptions. I’d like to avoid using a hack like this if possible.

This is possible and raises the question where and when do you register the subscription in your code?
With AUTOMATIC mode registering functions, variables and subscriptions should be the first thing you do in setup() - no delays, not other actions that may take more than a few microseconds should be added before.

Ok I think this might be my issue. I had a 10 second delay for Serial.isConnected before registering the subscription.

I will test without the delay, but I’m still curious if anyone has advice on maintaining these connections and subscriptions over long periods of time. Are there any tricks like resetting once per day or sending test messages?

You can in fact register functions, variables and subscriptions before a connection is established as these functions only tell the device what it has (or will have) to register with the cloud once the connection is established.
So no need to wait for the connection before registering.

Are there any other tips you have in regards to maintaining and managing these connections? Any “best practices” for Particle <-> Device communications? Ideally we would leave the device in AUTOMATIC mode – I’d like to understand why we wouldn’t choose to do that and what, if any, connection management we should perform via our own backend services.

This is part of me trying to update my Particle backend understanding. Our current product uses a custom backend for most data transfer which is reliable, but a little unnecessary. We developed it at a time when Particle’s services for cellular weren’t as feature rich as we would have liked. We’d like to move all of this to Particle’s webhooks so I am looking for the rough edges in the current version of the product.

If your device is meant to stay online AUTOMATIC is fine but I’d pair it with SYSTEM_THREAD(ENABLED).
If your device is supposed to connect and disconnect at will, I’d opt for SEMI_AUTOMATIC or MANUAL but again with SYSTEM_THREAD(ENABLED).

Its a powered device and will stay online at all times.