Mangled Subscibe data

Hi there.

I am having trouble with a partcile.scubscribe event. I have two devices, one that will act as a door controller and temperature monitor publishing events. This one is mostly working fine. The second device is displaying the info sent with Publish events. I am however trying to send triggers back to the first device but the data in the publish is getting mangled some how.

Here is a segment of the code on the first device.


Particle.subscribe("DoorControl", doorControl, MY_DEVICES);


void doorControl(const char *event, const char *data) {
    Particle.publish("DEBUG", "Enter DoorControl");
    Particle.publish("DEBUG", data);
      if (strcmp(data,"Right")==0) {
        Particle.publish("DEBUG", "RIGHT");
  } else if (strcmp(data,"Left")==0) {
        Particle.publish("DEBUG", "LEFT");

Second device sending code.

    if (millis() > testInterval +5000) {    // for testing. this will be changed later.
        testInterval = millis();
        Particle.publish("DoorControl", "Left",60,PRIVATE);

And here is an example of what is getting to the first device.

The data changes every time and I suspect it I have messed something up with the pointer.

Can someone help?

You are having problems because you are publishing from the subscribe handler. There is only one circular buffer for pub/sub data and it gets confused in this case.

A better way is to set a flag in your subscribe handler so that the next you get back up to loop() it can do the publish from there.

1 Like

Your right. as soon as I commented the publish lines. It started working.


1 Like