Particle.publish() issue

I notice that in about 10% of the cases, when an Electron connects it does not attempt to publish its data. I haven’t been able to replicate this issue yet using the serial monitor so I’m not 100% what exactly happens with the Electron - but I have 10 field Electrons atm and often I only get 8-9 webhook responses.

I currently have the following code:

     case CONNECT:
 ...
         if (Particle.connected()) {
             initialized = false;
     	     Serial.println("connection established!");
    	     stateTime = millis();
 	         state = PUBLISH;
 	         break;
             }
 ...

and:

     case PUBLISH:
         sig = Cellular.RSSI();
         rssi = sig.rssi;
         snprintf(data, sizeof(data), "{\"xx1\": \"%i\", \"xx2\": \"%i\", \"cll\": \"%i\", \"fd2\": \"%i\", \"fll\": \"%i\", \"lat\": \"%.02f\", \"lng\": \"%.02f\", \"tmz\": \"%i\", \"rsi\": \"%i\"}", X1, X2, collection, fd2, fillLevel, lat, lng, tmz, rssi);
         Serial.println(data);
         Particle.publish(publish, data, PRIVATE);
         state = SLEEP;
         break;  

Is there anything in here that could create an issue? Would Particle.connected() ever return false even if the device is connected? Is it possible a Particle.Publish() may fail once? Is there any way to verify that data has been published, before putting the Electron into Sleep mode?

I think to remember having Particle.connected() sometimes report wrong in either direction, but …

You can use the WITH_ACK flag and test for the return value of Particle.publish().

2 Likes

Thanks, good idea that I wasn’t aware of yet. I will give it a try tomorrow

1 Like

Thanks, I did it as follow. Everything seems to be working correctly :slight_smile:

    case PUBLISH:
        sig = Cellular.RSSI();
        rssi = sig.rssi;
        snprintf(data, sizeof(data), "{\"xx1\": \"%i\", \"xx2\": \"%i\", \"cll\": \"%i\", \"fd2\": \"%i\", \"fll\": \"%i\", \"lat\": \"%.02f\", \"lng\": \"%.02f\", \"tmz\": \"%i\", \"rsi\": \"%i\"}", X1, X2, collection, fd2, fillLevel, lat, lng, tmz, rssi);
        Serial.println(data);
        publishSuccess = Particle.publish("publish-with-ack", data, PRIVATE, WITH_ACK);
        if (!publishSuccess && publishCount < 5) {
            Serial.println("error: data was not sent");
            publishCount++;
            delay(2000);
            }
        else {
            Serial.println("data was correctly sent");  
            state = SLEEP;
            break;
            }
        break;
1 Like

For the past 3 weeks I’ve had had 12 devices running this code for their publishing. A few times I actually experienced publishSuccess returning false (see example above) even though the data was sent; So they published the max. number of times (rather than once). This happened in about 5% of the cases.

Does anyone have any idea on how to improve this code to prevent this from happening? Or is it just a limitation of publish-with-ack?