Electron not publishing after Sleep (Stop Mode)

Hi Guys,

I’ve been tinkering around with my Electron and trying to send data via Publish, which hits a webhook and sends it to the aggregation service I’m using. I currently have it publishing it’s Battery Level (SoC) to the aggregation service, sleeping for 20 minutes, and then publishing again. I was doing it to learn a bit more about publish/webhooks and to get a ballpark of the electron’s battery drainage…but for some reason only the first two publishes work! The timeline is something like this:

Flash Electron via USB/Particle CLI --> Run through loop, publish SoC value --> SLeep for 20 minutes (In “Stop” Mode) --> Wake up, wait for Particle.connected, Publish --> Sleep for 20 min -->???

I’ve tried two aggregation services (Both Librato and Adafruit IO), and it stops after the second publish for both services. Below is my code:

#define publish_delay 10000
FuelGauge fuel;

void setup() { }

void loop() {

    waitUntil(Particle.connected);
    Particle.process();
    delay(publish_delay);
    long value=fuel.getSoC();
    Particle.publish("aiobatt", String(value), 60, PRIVATE);
    System.sleep(D0,RISING,1200);
}

I’m sort of lost now, so any help is much appreciated.

-Chris

OK, that did not work after the first wake up from sleep.

Trying this code now which is closer to what your doing:

#include "application.h"

FuelGauge fuel;

char publishStr[20];

int led2 = D6; // Instead of writing D7 over and over again, we'll write led2
// This one is the little blue LED on your board. On the Photon it is next to D7, and on the Core it is next to the USB jack.

void setup() {
pinMode(led2, OUTPUT);
}

void loop() {

 waitUntil(Particle.connected);  
sprintf(publishStr, "%.2f %.2f", fuel.getVCell(), fuel.getSoC());
Particle.publish("e1", publishStr, 60, PRIVATE);
 digitalWrite(led2, HIGH);
 delay(1000);
 digitalWrite(led2, LOW);
 delay(1000);
System.sleep(D0,RISING,300);

}

I can confirm that I also do not see the 2nd publish even after the 1st sleep wakeup.

I’m trying to add a 2 second delay after the waitUntil(Particle.connected); to see if that helps any.

Thanks. I’ve tried a few different tweaks but to no avail. I pulled up the events stream for my electron via curl, and I can confirm the first two publishes go out just fine, but I never see any more than that second publish. I wanted to make sure that it was just publish that was having issues, and not webhook or the aggregation services, and it seems like it is the sleep/publish combo.

Yea I’m seeing it publish after the 2nd wakeup but then the next 2 publishes do not show up, then the next publish will show up.

I added a 2 second delay after the waitUntil(Particle.connected); but it’s not solving the issue.

I’m going to keep playing around but we should see what @Bdub has to say.

@BDub can you give this a try and see what you can find out?

The Publish using no sleep mode seems to be stable, but when you start publishing after waking up from sleep some publishes go missing for some reason.

DeepSleep seems to be working more reliably.

System.sleep(SLEEP_MODE_DEEP, 150);

Can you also give this a try and report back.

I know it does not allow the wake from pin but were just trouble shooting here to see what does work.

Hey all! I have been trying to spread the word that sleep (Stop) mode and waking are not going to be working exactly as desired until the next Electron firmware release:

Basically the modem is being power cycled when sleeping currently, and when waking we are also not notifying the server that the IP address may have changed (which it likely has). These things are fixed already, but need to be merged and released. Soon! Thanks for your patience until it's out :wink:

1 Like

@Bdub Thanks for the clarification on this.

Any ETA on new firmware?

If we build locally can we get the fix now?

@RWB I’m using develop local build and the problem is still there

@RWB @bpr Session move has been merged awhile back in develop, but this PR is necessary as well: https://github.com/spark/firmware/pull/845

@bdub I’ll have never built locally so it will be all new to me so excuse my ignorance on the following question.

If I get setup to build locally I will be able to compile your latest firmware and also add in the PR change you linked to above to fix the current sleep publish issue right? Just want to make sure.

And if I just want to wait for the latest firmware do you have any idea when we could expect to see it? :smiley:

@bpr Let me know if you add the fix code and get it working.

@BDub, I would love to try it but don’t know how to use a PR. Do I just add lines 49 and 51 in the appropriate spots?? Excuse my ignorance

You can create a new branch based on develop, then merge the feature/electron/sleep-resume branch into it:

git clone https://github.com/spark/firmware.git
git checkout develop
git checkout -b test/sleep
git merge feature/electron/sleep-resume
vi editor is likely going to pop up
type `:wq` and hit ENTER
now you can compile from the modules directory

firmware/modules $ make clean all PLATFORM_ID=10 APPDIR=~/fw-apps/my-sleep-app DEBUG_BUILD=y COMPILE_LTO=n PARTICLE_DEVELOP=1 -s program-dfu

put your app named whatever.cpp in the ~/fw-apps/my-sleep-app/ folder
3 Likes

@BDub @RWB I got an error on the fourth command “C:\particle\developmerge>git merge feature/electron/sleep-resume
merge: feature/electron/sleep-resume - not something we can merge” (I’m on Windows 10)
so I just manually added those two lines (49 and 51) to system_sleep.cpp and compiled from modules and my electron started pumping out beautiful little events as desired every minute under stop mode :grinning:
However, the only problem is that after 5 publish events the dashboard often but not always starts showing 2 identical events either at identical times or near identical (e.g., one at 2016-02-23T05:09:34.825Z the other at 2016-02-23T05:09:34.826Z)
Just to be clear the first test was meant to be every minute, just not multiple events per minute.
I’m going to change the interval to every 30 minutes and go to bed, see what’s up in the morning

#include "application.h"

FuelGauge fuel;
char publishStr[20];

void setup() {
  pinMode(D1, INPUT_PULLDOWN);
}//setup()

void loop() {
  sprintf(publishStr, "%.2f %.2f", fuel.getVCell(), fuel.getSoC());
  Particle.publish("e1", publishStr, 60, PRIVATE);
  delay(1000);
  System.sleep(D1, RISING, 30 * 60);
}//loop
1 Like

I started testing your code every 10 minutes and got to 2 publishes with no issue and decided I want to sleep soon so I changed it to every 1 minute just now :smile: We’ll see some results hopefully quickier

So I wasn’t seeing a double publish, but when I started looking at the local Serial1 logs things didn’t look right because we’re not waiting for ACK from the server before we sleep, so when we wake back up the first thing that was happening is the ACK would appear to be received, even though it already happened and was just in the buffer.

I added this before the Sleep call and logs cleared up, one publish, one acknowledge over and over.

  uint32_t start = millis();
  while (millis() - start < 5000UL) Particle.process();

@Bdub so this code is required once your PR update is included in the new firmware?

This will not fix the current Sleep Publish issue right?

Yes, but we still have an opportunity to fix the need for a delay before sleeping in the new firmware release (not out yet) :wink:

2 Likes

With the PR, running the 30-minute interval stop-mode sleep app overnight yielded no double publishes in the dashboard but it appears data usage was higher than when not using sleep mode at all the day before, but I can’t be too sure about the data usage given that the report is delayed and just now the dash says my "billing info is not available… please check back…"
Curious how I was getting that double publish (actually sometimes triple) at much shorter interval. @BDub, I’m very glad you found the likely problem. Thanks!

So I tried adding in this delay, and it worked while I had my sleep time set to two minutes! But I tried everything the same but increasing the sleep delay back to 1200 secs, and now I"m only getting the first publish, and nothing further. Very strange.