Sleep after publish?

Hi,

I would like to send my Photon to sleep after it has published a message. The problem is that the dashboard never receives my message.

My loop code:

// Read the volatge of the LiPo
float cellVoltage = batteryMonitor.getVCell();
// Read the State of Charge of the LiPo
float stateOfCharge = batteryMonitor.getSoC();

// Send the Voltage and SoC readings over serial
Serial.println(cellVoltage);
Serial.println(stateOfCharge);

// wait for the cloud connection to be connected or timeout after 10 seconds
    if (waitFor(Particle.connected, 10000)) {
        bool sent = Particle.publish("Data", String::format("voltage=%.2f;charge=%.0f"), cellVoltage, stateOfCharge)); //data,name=Maraiah internal=29.5,external=13.3 1434055562005000038
        Particle.process();
        if(sent == true){
            System.sleep(pin, RISING, sleep);
        }
    }

I found the hint with Particle.process(); here but it does not seem to work for me. I don’t like to use a delay because it blocks my code.
The if(sent == true) does not work either.
Without sleep everything works fine.
Is there a new solution for the 0.4.9 firmware?

bool sent = Particle.publish("Data", String::format("voltage=%.2f;charge=%.0f"), cellVoltage, stateOfCharge));

Does this build at all?
The parentheses in this statement don’t seem to match up.

Another option to make sure your publish got delivered is to self-subscribe and place the sleep command into the subscribe handler.

BTW: The return value of Particle.publish() does not tell anything about the actual publication but only if the command has a chance to get executed at all.

Thanks for your reply!

It should be:

bool sent = Particle.publish("Data", String::format("voltage=%.2f;charge=%.0f", cellVoltage, stateOfCharge));

I have shortened my Particle.publish to make it easier to read and forgot the bracket.

I’ll try to self-subscribe and report back :smile:

Another thing you might consider is that you are not alone there and Data is a rather ubiquitous term, so you might receive events from others who have also used such an all but unique event name :wink:
You should either go for a less common term or send as PRIVATE (or both).

1 Like

Had the same problem on the previous firmware, ended up doing both process and delay, less than ideal though.

Particle.publish("log", "Occu", 60, PRIVATE);
delay(1000);
Particle.process();

I am now using the code from here to subscribe to my sleep event and so far it works reliable. The only thing I have changed is that I added

canSleep = false;

before System.sleep because the Photon does not reset when it goes to sleep.

It takes 10 seconds from the device came online event to the device offline event.