Photon instantly wakes up on SLEEP_MODE_DEEP

Hi everyone,

I’m having a blast with my new Photon :slight_smile: Now I’m trying to get it to sleep properly using SLEEP_MODE_DEEP but it doesn’t work, it wakes immediately and runs the loop.

#include "SparkFunMAX17043/SparkFunMAX17043.h" // Include the SparkFun MAX17043 library

double voltage = 0; // Variable to keep track of LiPo voltage
double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC)
bool alert; // Variable to keep track of whether alert has been triggered

void setup()
{
    RGB.control(true);
    
	Serial.begin(9600); // Start serial, to output debug data

	// Set up the MAX17043 LiPo fuel gauge:
	lipo.begin(); // Initialize the MAX17043 LiPo fuel gauge

	// Quick start restarts the MAX17043 in hopes of getting a more accurate
	// guess for the SOC.
	lipo.quickStart();

	// We can set an interrupt to alert when the battery SoC gets too low.
	// We can alert at anywhere between 1% - 32%:
	lipo.setThreshold(20); // Set alert threshold to 20%.
}

void loop()
{
	RGB.color(255, 0, 0);
	delay(1000);
	
	RGB.color(0, 255, 0);
	delay(1000);
	
	RGB.color(255, 0, 0);
	delay(1000);
	
	RGB.color(0, 255, 0);
	delay(1000);

	// lipo.getVoltage() returns a voltage value (e.g. 3.93)
	voltage = lipo.getVoltage();
	// lipo.getSOC() returns the estimated state of charge (e.g. 79%)
	soc = lipo.getSOC();
	// lipo.getAlert() returns a 0 or 1 (0=alert not triggered)
	alert = lipo.getAlert();

        String state_of_charge(soc, 5);
        String voltage_string(voltage, 5);
    
	Particle.publish("voltage", voltage_string);
	Particle.publish("state_of_charge", state_of_charge);
	
	delay(10000); // wait for publish
	
	RGB.color(0, 255, 0);
	delay(1000);

        System.sleep(SLEEP_MODE_DEEP, 60);
}

Any clues on what I’m doing wrong?

Sincerely

David

Are you using an external weak pull-down on WKP?
What firmware version have you got on it?

But most confusing: I can’t find your System.sleep() statement :confused:

BTW: If you intend to sleep a lot, there is little use in Particle.variable() since the device needs to be awake to service the variable request.

Hi,

yeah I managed to remove that line before copy & pasting it. Lesson learned don’t make a lot of changes and ctr-z before posting the contents.

Anyway System.sleep(SLEEP_MODE_DEEP, 60); should be added just before the closing curly. I’ll edit and add it.

I’ve removed the Particle.variables as well since I won’t use them.

BR,

David

1 Like

Have you got your Photon powered via LiPo only or USB connected too?
If the latter, could you have some noise on the USB DC?
Could it be that the SparkFunMAX17043 board does something funky when the Photon goes to sleep?
e.g. trigger a WKP due to lipo.setThreshold(20);? (I have no experience with this board)

It’s only powered via LiPo right now, I tried using only usb after reading your reply and it acts the same. What is the easiest way to check the firmware version? In build.particle.io under Particle Devices it says Building with firmware: No firmware available.

Your guess is better than mine I have not a lot of experience with this board either.

I removed the lip.setThreshold(20); and it still acts the same.

That’s odd :confused:
Have you ever flashed via Build before?
Have you got CLI installed? That would help with a lot of things.
Can you post a screenshot of the Build Target drawer with the firmware drop down open?

If your device still has the original (“old”) system firmware sleep() had a bad start :wink:
But usually you should have seen an auto-update on first connect.

Here is the screenshot you wanted. The dropdown is disabled.

Yes I’ve gone through the usual blink a light and turn on/off remotely and so on. The Photon is flashed everytime since I can see the lights flashing and the behavior is changing.

I will install CLI and check the firmware version and post here again.

I see, your device is not recognized as § Photon but as (?) Other either because the claiming was not successful or the device ID got muddled.
Maybe support or @Dave can help out here, but that might take some time due to weekend and Memorial Day on Monday.

1 Like

I installed particle-cli and ran a firmware update. It now appears as a Photon with firmware version 0.5.1 but it still has the same behavior with an instant wake up directly after the .sleep() call.

Again, have you got an external pull-down on WKP in place?

Also try removing the Photon from the battery shield, power via USB and see what happens then.

Sorry, I don’t have an external pull-down on WKP. I removed everything including the battery shield and removed all code except the sleep and a single Particle.publish() and now it works. Seems to be something with the MAX17043 library that is causing these issues I guess.

Thank you for all your help. I really appreciate it.

So after readding LiPo and the MAX17043 code piece by piece, everything now works flawlessly. Really weird, this is the exact same code that didn’t work. Isn’t the application code wiped properly on flash?

Nope, that should be no problem and I have never seen this happen, but I have seen OTA flashes not succeede due to user code interrupting the process (not in your case tho’).

It might seem that it is exactly the same as before, but sometimes it’s minute things that make the difference - even bad contacts might have contributed. Unplug/replug somethimes cures such things.