Register functions after photon is claimed?

hello,

we have a code on a photon that registers a couple of functions (Particle.function). we noticed that an unclaimed photon that is claimed will need to restart before the functions actually register.

(here is what we do: claim photon, program, unclaim and claim again)

what is the proper way to deal with this?

thanks
frank

Not quite sure why you first claim the device and then unclaim it again.

The behaviour is not surprising when you know how the registration process works.
Functions, variables and subscriptions are only registered with the cloud a short time after the connection to the cloud got established.
So in your case the functions were already registered with the cloud (but not any user account) before the device got claimed but claiming the device does not mean that the cloud connection would be ended and reestablished - it just persists.
Hence no re-registration will happen.

However, you may not need to completely restart the device but only reconnect to the cloud - with one caveat: In order to reduce data traffic a re-registration may be omitted when the the set of registered “items” doesn’t change too much over the course of a short connection loss.

What the “proper” way is depends on multiple factors.
How do you program?
Why do you program/claim that way?
Who owns the device before and after programming?

hi scruffr,

thanks for your fast reply

the unclaim-claim is just to simulate what we get in the field with a new installation: the photon (p1) will be programmed in the factory and then claimed in the field (the user will use our mobile app to set the network and scan a 2d code which is passed on to the server who claims the device – just so we know which particle-id and external code belong together)

to act on the claiming (and restart or reconnect) the photon does need to know when it is claimed. how can we detect this?

i guess we could use “hasCredentials” but that is not very safe as the registration may failed or be stopped by the user for whatever reason

thanks
frank

WiFi.hasCredentials() would not help you distinguish whether the device is claimed or not.

My first stab at a way for a device to know whether it is claimed to a specific account would be to Particle.publish() a PRIVATE event which would trigger a specific webhook response which should match preshared value.

But @marekparticle may have a better solution. I wouldn’t be surprised when there already was a “system webhook” like the one to request the device name (particle/device/name).

I don’t know of any more elegant way to figure out whether or not a device has been claimed other than that suggested by @ScruffR (or a call to get all your claimed devices and then filter accordingly).

I’m not fully filled-in on this particular issue, but I’ve found this library to be really interesting: https://github.com/rickkas7/DeviceClaimRK - I haven’t had the time to really dive in and see what functions are exposed here but maybe there’s something relevant to your use-case.

1 Like

i like the “claimed” flag in eeprom but the use-case seems a bit different

my thoughts are now:

  • use the “areWeClaimed” flag in eeprom as “cache” of figuring out if we are claimed
  • add a “areWeClaimed” function that will determined if the device is claimed
    • the first time we figure out we are claimed we set the flag in eeprom and restart
  • call this function at the end of setup

will let you know if and how this works…

thanks
frank

1 Like