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.

Setup:

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

Function:

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

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.
{“data”:")�D��W\u0013.#�-�N�ŷ�",“ttl”:60,“published_at”:“2018-02-03T14:51:54.531Z”,“coreid”:“2a0021000f47353136383631”,“name”:“DEBUG”}

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.

Thanks.

1 Like