Particle subscription to get a varibale

I have a photon that’s collecting information periodically, and going into deep sleep. As far as I understand it, when in deep sleep, it’s not possible to query a variable.

What I’d like to do is use a subscription to get the event that posts the data to the particle dashboard and pull the data of the event as a variable to another photon.

Is this possible?

It’s not clear to me exactly what you’re trying to do. Is the device that’s going into deep sleep publishing before it goes to sleep? If so, your other device can subscribe to that event, and get the data from the data argument of the published event.

Thanks Ric, sorry if my description wasn’t very clear.

Yes, that’s pretty much exactly what I need to do. I have a subscription working. I guess the part I don’t fully understand is how to take the data component of the subscription and turn it into a variable. I have it logging to serial (I found an example of some code to do that online) but I’m stuck on the next step.


Your handler should look like the following. If you just need to use “data” inside that function, you can just use it; you don’t need to turn it into a variable.

void myHandler(const char *event, const char *data) {
    // do what you want with "data" here

If you need to turn it into a global variable, so you can use it outside the handler, then just declare a char* variable at the top of your file, and copy the “data” into it.

char myGlobalData[256];

void myHandler(const char *event, const char *data) {
    strcpy(myGlobalData, data);

Now you can use myGlobalData anywhere else in your code. I usually just size my destination array to 256, since the maximum size of data is 255 bytes, but you can make it smaller, and use strncpy like @Muskie showed in his post to make sure you don’t have a buffer overrun.

Perfect, thanks Ric!
I figured it would be relatively simple, I just get lost in syntax sometimes :-/

I wouldn’t copy a string like that since you are going to clobber memory. Instead try this:

char   myGlobalData[20];

void myHandler(const char *event, const char * data)
   strncpy(myGlobalData, data, sizeof(myGlobalData));
   myGlobalData[sizeof(myGlobalData) - 1] = '\0';

Notice that I reserved a character buffer of 20 bytes and then did a limited copy into that buffer of no more than 20 bytes. Add the terminating null just in case the input was longer than 20. Make your array big enough to handle whatever data you are receiving, the 20 here is just an example.

Code defensively.

Oh, duh. Thanks for pointing that out. I was too hasty in my post, and left out the size. I’ve edited to show how I do it with a fixed size of 256 to allow for up to the maximum size that data can be (if I’m sending data of variable length that could be long). I don’t know if that is defensive enough – I don’t really know what happens if you try to publish data that is longer than 255 bytes. I should do a test and see.

After Edit: I did a test where I published with a data string that was 300 characters long, and when I looked at data, I saw that it was 256 bytes – 255 bytes of data with a terminating 0.

Does the other device need to be actively subscribed to the even in order to receive the data? Or can the other device wake up from deep sleep, subscribe, then read the data from even published by the first device?
From reading the forum I concluded that I can’t do that. Is there some work around? I want to publish data from one device and put it to sleep, then I want to wake up another device and read the first device’s data. Is there a way to do this?

Nope, that’s not possible yet, but is planned (at an extra small fee AFAIK, since it will call for extra server storage to keep the data alive).

At the moment you’d need a 3rd party service to store to and retrieve the data from.

Thanks for the quick answer. I think the simplest solution for me might be to use another Photon that is always on to store published data, turn that data into Particle.variable and have it ready to be read once the other sleeping Photons wake up and want to retrieve it. But I’ll look into some 3rd party service before I resort to wasting power on an always on Photon. Do you know of any such service I could use? Thanks.