Reading particle variable on second photon

It seems odd to me that a particle cloud variable can’t currently be “read” by a Photon - surely that would be relatively easy to implement and very useful?

You can use the publish and subscribe model. One device can publish and many devices can subscribe to that publish and act on the value.

5 Likes

mmm, I know, just seems more complicated than being able to poll a variable exactly when you want. Just wondering if there were plans afoot to enable Photons to be able to poll variables.

There is currently no plan to allow querying variables from a device. Or calling functions on other devices.

It’s not an unreasonable request, but it’s also not trivial to implement. Remember that devices don’t communicate with the cloud using the web-based cloud API like the console, mobile, and web apps do. They communicate using the CoAP protocol, which would need to be extended, and then the server code to actually do the work, and the additions to the on-device system firmware.

Given the complexity and that it hasn’t been a highly requested feature, it’s never been scheduled for implementation.

6 Likes

Thanks for this, @rickkas7 - just trying to implement an alternative now :slight_smile: whilst also being a good Particle ecosystem citizen !

I’ve got it working whereby I can (from one device) ask my other devices what their status are, but what I’d like to do instead of:-

void SendStatus()
{
  if (windowlocked == 1) {
    Particle.publish(DEVICE_NAME, WINDOWOPEN_MSSG, 60, PRIVATE);
  } else {
    Particle.publish(DEVICE_NAME, WINDOWLOCKED_MSSG, 60, PRIVATE);
  }
 delay(1000);
  if (doorlocked == 1) {
    Particle.publish(DEVICE_NAME, DOOROPEN_MSSG, 60, PRIVATE);
  } else {
    Particle.publish(DEVICE_NAME, DOORLOCKED_MSSG, 60, PRIVATE);
  }
}

ideally condense those two publishes into one publish, but for that I’d need four states:

  • the door is locked and the window is open
  • the window is locked and the door is open
  • neither the door nor the window are locked
  • both the door and the window are locked

And I don’t know what to Google for, for what that kind of comparison is called. I’ve not had success with combining if/else blocks and && || before - it seems to get too complicated and stop working when I combine things.

Other than that my main concern is a Photon will drop WiFi for a crucial minute, miss a publish, and its status will be effectively out of date and no longer true. I suppose the only answer is to do periodic “Request Status” publishes. But then that would be true for the querying variables method, too! :slight_smile: :slight_smile:

How about this

  char data[64];
  snprintf(data, sizeof(data), "Window: %s, Door: %s", windowlocked ? "OPEN" : "LOCKED", dorlocked ? "OPEN" : "LOCKED");
  Particle.publish(DEVICE_NAME, data, PRIVATE);
  delay(1000);

If you don't need the elaborate wording, you could just go with

  snprintf(data, sizeof(data), "W%d, D%d", windowlocked, dorlocked);

And to parse that on the receiving end

  int windowState = 0;
  int doorState = 0;
  sscanf(data, "W%d, D%d", &windowState, &doorState);

If you have more complex data to send/receive, you'd just add that to the string, which would be more complicated with multiple Particle.variable() requests, unless you go for a string variable, but then the theoretic advantage of Particle.variable() over Particle.publish()/Particle.subscribe() vanishes just the same :wink:

BTW, if you want maximum conciseness, you could just send one single integer number where each bit represents the binary state of your objects (e.g. 0b00 both locked, 0b01 one locked other open, 0b10 on open other locked, 0b11 both open).

3 Likes

Sounds great Mr Scruff - will give it a try! :slight_smile: Thank you