I’m trying to track down a menacing bug in my code when trying to call a Particle.subscribe() handler. I want to send my device name in a JSON payload to parse with a firebase DB. In order to do that I’m using the code provided in the reference files of the particle website. Here is the referenced stock code:
I now am going to paste the code I have on my device, I have it split up because its referenced in different areas of the code so I’m snipping here for clarity:
Here is the setup of the subscribe command and the publish command at the beginning of my code
The weird part is that I got it working last night but now its not working at all. I double-checked everything and its the same as it was last night.
This is what I’m seeing In my Event Viewer:
Could you share the code that formats the Report_Payload event. Also, how do you define deviceName? If deviceName is a String you can’t just use equal operator to copy it from a const char* type.
Sure: this is the payload function, I’m using a JsonParserGeneratorRK to parse the information for me in the first part and I’m also sending it outside as part of me debugging. I was also having this problem without the JsonParserGeneratorRK being used so I don’t think its related to the library:
(for particle/... events the scope is irrelevant but in general you need to publish as PRIVATE when subscribing for MY_DEVICES and PUBLIC when subscribing for All_DEVICES)
You subscribe to the event name (first parameter) not to the payload (second string parameter).
First, you are publishing to the "admin" topic, with "particle/device/name" as the data. In the docs it says to publish to the "particle/device/name" topic with no data. Are you sure this was working last night?
Second, in deviceNameHandler the following line may have unexpected results:
deviceName = data;
Since data is a pointer to a char array, when you set deviceName to the same pointer, the data at that address likely changes before you dereference deviceName later in your application.
As @armor asked, how have you defined deviceName in your code? In order for your code to work as expected, it should be a char array. (If it was a String object the compiler likely gave you a warning.)
It’s almost as if the handler isn’t getting called from the Particle.subscribe command because if it was I would see this debug Particle.publish(“device handler called” in console:
Do you have physical access to the device? Printing debug messages over serial is much preferable over publishing events because events can get throttled and publishes and subscriptions use the same buffer.
A quick Serial print in deviceNameHandler could tell you a lot:
// At the beginning of the function
Serial.println(data);
// At the end of the function
Serial.println(deviceName);
Also, on line 1029, that publish might not be getting through because you didn’t specify PRIVATE.
Also, you defined the length of deviceName to 24, but you set the null terminator on index 24, which is out of range. You should set it on index 23, which is the last index. That yellow squiggle might be a warning about this. You could also define the length as 25. Also, since data is likely less than 24 characters, you could remove that line setting the null terminator altogether.
My code was set up in a way that the Particle.publish() command was only sent once, so the handler was not called.
I don’t know if that’s a bug or something specific to my code but that’s what seems to have fixed it.
Here’s some proof: Look at my Event viewer, the first time my do_report was called the deviceName was not updated, but the second time the Particle.publish command was called it did work.