[SOLVED] My events show up as rubbish

I’ve written some simple code to subscribe to an event, and i’m creating the events using postman to POST to the https://api.particle.io/v1/devices/events end point. I can see the published events clearly in https://dashboard.particle.io/user/logs but when i examine them in my p0 and then publish an event containing the data, i see gibberish. For example:

here’s the code:

#include <string.h>

// Log message to cloud
void debug(String message) {

    Particle.publish("DEBUG", message.c_str(), PRIVATE);
}

void setup(void)
{

    /* Register a particle.function so that we can receive events */
    Particle.subscribe("displayScreenOutsideTemperature", updateOutsideTemperature);

    debug("setup done");
}

void loop(void)
{

}
   
void updateOutsideTemperature(const char *topic, const char *data) 
{
    
    debug("updateOutsideTemperature: received event");
    
    char msg[100];
    sprintf(msg, "Topic received %s : %s", topic, data);
    
    debug(msg); // this shows up as "Topic received " and then gibberish
    
}

Hi @mnbf9rca

You are calling publish from inside a subscribe function. The internal data structures used to store the subscribed data and the published data are the same so they conflict. Try copying the string into you own storage before publishing:
[Edit–you are using both the event name and the data]

void updateOutsideTemperature(const char *topic, const char *data) 
{
    
    char rxStr[64];
    char rxName[64];
    char msg[100];
    strcpy(rxName, topic);
    strcpy(rxStr, data);
    debug("updateOutsideTemperature: received event"); 
    sprintf(msg, "Topic received %s : %s", rxName, rxStr);
    debug(msg); // this shows up as "Topic received " and then gibberish
    
}

Better yet would be to set a flag in your event handler and do the publishing in loop().

1 Like

thank you so much! I spent hours trying to figure this out :smile:

it might be worth updating the core reference (https://docs.particle.io/reference/firmware/core/#particle-publish-) to highlight the conflict.

1 Like