How/where do I save Published Events?

BTW, when looking at the docs again, the sample code for name request is using a PUBLIC event, not PRIVATE

    Particle.subscribe("spark/", handler);
    Particle.publish("spark/device/name");

Yeah, I looked at that as well the first time you mentioned it. I took it out of the code and tested again, but it didnt seem to fix the issue.

Now I see you do the subscribe five times here

void setup() {
	Serial.begin(9600);
	for(int i=0;i<5;i++) {
          Serial.println("waiting... " + String(5 - i));
          delay(1000);
          Particle.subscribe("spark/", handler); // <-- ????????????
	}
}

I'd rather do this

char myName[64];
void handler(const char *topic, const char *data) {
    strncpy(myName, data, sizeof(myName)-1); // save data in global var
    Serial.println("received " + String(topic) + ": " + String(data));
}

void setup() {
    Serial.begin(9600);
    Particle.subscribe("spark/", handler);

    Particle.connect();
    waitUntil(Particle.connected);
    Particle.publish("spark/device/name");
}

and put the original publish back in its place.

Now it just works as before - events get published but without info on the Sensor name. Which makes sense I suppose because publishing the name only appears in void setup() now?

ideally, the code could even be something like Particle.publish(eventName, ā€œspark/device/nameā€);
as I don’t really need frequent updates on battery status. Just having the sensor ping to me that it was picked up, and relaying its own name, already is sufficient.

You haven’t read the docs carefully enough, it seems :wink:

The Particle.publish("spark/device/name"); only publishes an event to the Particle cloud to request the name of the device and as a response the cloud sends an event containing the name to your device.
Your Particle.subscribe() just catches that event and stores the name - which my code does in myName.

What you do with that name in your code is entirely up to you - currently it’s not used, it just hangs around in the device’s RAM.

So try changing your publish to

  Particle.publish(eventName, myName, PRIVATE);

You sir, are the best. Hat off to you! :smiley:

As I was reading your post, I realized I indeed misread ā€˜ā€˜gives you device name’’ as ā€˜ā€˜sends device name as output’’. But you explained this very well to me; everything is working great now. The battery update is not so important to me so it doesn’t matter that it’s left out; my sensor is asleep 99.9% of the time so these batteries should last a really long time anyway.

Either way, thanks a lot for all your help, I really really appreciate it! :slight_smile:

1 Like

But you can have both anyway, just do this

      char data[96];
      float stateOfCharge = batteryMonitor.getSoC();
      snprintf(data, sizeof(data), "%s: %.2f", myName, stateOfCharge);

      Particle.publish(eventName, data, PRIVATE);

BTW, you can remove the 5sec wait loop from setup() too. It's not needed for the name retrieval.