Hi there!
I’m looking for the most efficient way to individually send data to my fleet of Photon’s arranged under a Product in the dashboard.
I’m implemented a timer functionality and I need to send the on-off timer events (day of week, hours, minutes of each timer entry) to be stored in the Photon “EEPROM”.
Looking through the reference pointed me to Particle.function(). I’m not sure if Particle.subscribe() could be used as well (not sure on how to talk to each Photon individually in this case). I’m missing something? If not, which of the above is better in my case?
As always your help will be very appreciated.
Cheers,
Diego
Particle.function() is definetly a way but Particle.subscribe() can be used as well - in two ways.
For non-product and product devices you an append the device ID or some other unique identifier to the event name, and check in the event handler if that event concerns your particular device or not. Particle.subscribe() fires for every event that starts with the eventname filter.
For products only, you can use the product webhook feature to get the device ID prepended to the event name and so you can filter for the device id.
Which is better? That depends on your special needs.
Hi @ScruffR and thank you for your swift reply.
For a large fleet of devices, couldn’t Particle.suscribe crash my code as it would be fired repeatedly -and need to be “scanned” to determine if a particular device is the true recipient of the event-?
It might “distract” your device from its original tasks, but it should not crash the device, since the handler is not actually called like an ISR at any arbitrary time, but is called by the system thread in an orderly manner.
You could also make the event filter fully qualified (e.g. eventPrefix + System.deviceID()) to let the cloud do the filtering.
Hi @ScruffR. Thank you once again for your reply.
I have successfully implemented a webhook for a while and I understand how it works from device to my server.
In this case, I suppose that my server would have to trigger a webhook, and the webhook will sent an event to the selected Photon…I’m right?
The documentation only discuss the device > webhook implementation and details how to use Particle.subscribe to process a response from the server after a webhook was triggered. How I create a webhook that can be triggered from my server and which URL I should hit for this purpose? What data I need to sent?
@gavpret, webhooks are used to give Particle devices cloud-brokered access to HTTP/HTTPS sites. Your server does not need to fire a webhook to communicate with your devices. Instead, use Particle.publish() with an event name that your device(s) subscribe to. As @ScruffR indicated, the cloud will filter the events based on the subscribe event filter so only the affected devices will get the subscription call.
I need that my server sends data to a particular Photon in my fleet. This action should be triggered by the server when the end user wants to change the programmed timer events.
The only way is to Particle.Publish(), trigger the webhook and get the server response with the timer settings data?
@gavpret, since the change is driven from the server side then you may want to consider calling the Particle.function() of the specific device (using its device ID) using a POST call to the cloud from your server. Another way would be to use the publish mechanism which would include the device ID as part of the event name so that listening photons can define a subscribe filter for each unique device. Neither of these scenarios requires a webhook. I suggest you read up on Particle.publish()/subscribe and Particle.function() to see which is most suitable for your application. If you are using particle.js on the server side, you may want to read up on the client side functions, including CALLFUNCTION.
Thank you, @peekay123.
I have successfully implemented Particle.function() on my server (to remote activate a load) and it was one of the two options that seemed plausible for my needs. But since @ScruffR mentioned that Particle.subscribe could be used as well, I’m curious about how to implement it that way. I have read all the docs but I cannot figure out that possibility.
I really appreciate your time and patience!
Cheers,
Diego
@gavpret, Particle.function() provides a robust, verifiable way of interacting with a Photon. It is verifiable because Particle.function() returns a value to the caller allowing for validation of the call.
Publish/subscribe (pub/sub) on the other hand are “stateless” in the sense that the publisher will send an event “into the wild” and only subscribers of the events will receive them. The receipt of these events by their intended targets is not guaranteed. However, a cleverly designed system could publish and event and subscribe to a response event while the subscriber would, upon receiving an event, publish a response event which the original publisher would receive and thus complete the “loop”.
In your case, the one-to-one and verifiable nature of Particle.function() seems the best approach for your application IMO. Think of it as a dedicated pub/sub with verification!