Why the particle publish is not always work

Hi:
Please help me check this code.

        num_veh = triplane(NumDetect, sensor[0], sensor[1], sensor[2], sensor[3]);
        NumDetect = 0;
        Particle.publish("Num_Veh", String(num_veh), PRIVATE);
        Serial.printlnf("%d vehicle pass", num_veh);

I think this code should be correct because serial.printlnf is always valid. But particle publishing events sometimes don’t work. Typically, a serial print event occurs three or four times, and a particle device publishes event to cloud once.
This problem is just happening here. My program also uses particle.publish elsewhere, and all of this works fine. Just this one is not normal. Please help me think about possible mistakes.

Probably because you are violating the rate limit for publishes
https://docs.particle.io/reference/device-os/firmware/photon/#particle-publish-

But it’s hard to tell for a fact since you don’t provide enough context in your code snippet.

2 Likes

hey, regarding what @ScruffR mentions above and if indeed your firmware is violating the rate limit for publishes, you may want to use a library to handle those publishes for you:

Good luck!
Gustavo.

Thanks guys. Your suggestions are useful! Now, I combined some events to one event and publishes it to cloud. This problem is solved.