# Basic Power Loss Detection - Electron

**URL:** https://community.particle.io/t/basic-power-loss-detection-electron/20536
**Category:** Firmware
**Created:** [February 27, 2016, 7:07pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536 "2016-02-27T19:07:42Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [February 27, 2016, 7:07pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/1 "2016-02-27T19:07:42Z")

</div>

I’m futzing with my new toy 🙂 and thought about a basic power-loss detection setup.

Electronics:

A simple pull-down resistor between pin D1 and ground.  
Jumper from Vin (USB 5.0V) to pin D1.

The interrupt works, waking the Electron when I disconnect the USB cable from my computer, easy peasey.

But I cannot re-connect to send my alert via pushover. Can anyone see what’s wrong in my code?

the setup() publish works  
disconnecting power “wakes” the Electron  
it goes back to sleep after reconnection of power…

```cpp
#define REMIND_INTERVAL_MINS 1 //60
int toggleLed(String command);
const int triggerPin = D1;
void setup()
{
  pinMode(triggerPin,INPUT); // External Pulldown Resistor attached
  Particle.publish("pushover", "Particle Power Loss Detector Started");
}

void loop()
{
  System.sleep(triggerPin, FALLING);
  while(digitalRead(triggerPin) == LOW) // send message every 30mins until power is restored or battery croaks
  {
    //Cellular.on();
    Cellular.connect();
    while(!Cellular.ready())
    {
        //Particle.process();
    };
    Particle.publish("pushover", "Power Loss Detected!!!");
    Cellular.disconnect();
    System.sleep(REMIND_INTERVAL_MINS * 60); // test every REMIND_INTERVAL_MINS minutes
  }
  Cellular.connect();
  Particle.publish("pushover", "Particle Power Restored!!");
  Cellular.disconnect();
}

```

I guess I’m not understanding what is necessary to get back onto Cellular and transmit.

---

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 27, 2016, 7:45pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/2 "2016-02-27T19:45:57Z")

</div>

@BulldogLowell, it may be a separate problem, but I’m finding that the duration the electron is asleep seems to affect reconnect to Particle [https://github.com/spark/firmware/issues/888](https://github.com/spark/firmware/issues/888)

---

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [February 27, 2016, 8:04pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/3 "2016-02-27T20:04:25Z")

</div>

thanks @bpr .

I also read [this](https://community.particle.io/t/electron-not-publishing-after-sleep-stop-mode/20348) which (as @BDub explains) is disconcerting… suggests that power-cycle of cellular radio cannot be un-done in code with the current firmware…

A fix seems to be coming…

---

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 27, 2016, 8:40pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/4 "2016-02-27T20:40:14Z")

</div>

@BulldogLowell Try this:

```
//@BulldogLowell
//must use develop branch with PR#845 fix merged
#include "application.h"
#define REMIND_INTERVAL_MINS 1 //60
int toggleLed(String command);
const int triggerPin = D1;
const int dummytriggerPin = D3;
uint32_t start;

//10k PULLDOWN resistors on D1 & D3
void setup()
{
  pinMode(triggerPin,INPUT); // External Pulldown Resistor attached
  pinMode(dummytriggerPin,INPUT); // External Pulldown Resistor attached
  Particle.publish("e1PO", "Started");
}

void loop()
{
  System.sleep(triggerPin, FALLING);
  while(digitalRead(triggerPin) == LOW) // send message every 30mins until power is restored or battery croaks
  {

    start = millis();
    while (millis() - start < 1000UL) {Particle.process();}

    Particle.publish("e1PO", "Loss Detected");

    System.sleep(dummytriggerPin, RISING, REMIND_INTERVAL_MINS * 30); // 30 secs test
  }

  start = millis();
  while (millis() - start < 1000UL) {Particle.process();}

  Particle.publish("e1PO", "Power Restored");

}
```

---

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [February 27, 2016, 9:02pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/5 "2016-02-27T21:02:04Z")

</div>

@bpr, Since I don’t have local tool-chain… I’ll have to look for instructions here on the forum, I guess.

---

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [February 27, 2016, 9:43pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/6 "2016-02-27T21:43:03Z")

</div>

@bpr,

I could not find the toolchain instructions, but I noticed that after the initial failed notification, I received some of the subsequent times… using your adaptation of my earlier code, adding your fuel gauge piece and using the Wev IDE build:

```cpp
#define REMIND_INTERVAL_MINS 1 //60
int toggleLed(String command);
const int triggerPin = D1;
const int resetPin = D3;

FuelGauge fuel;

char publishString[64] = "";

void setup()
{
  pinMode(triggerPin,INPUT); // External Pulldown Resistor attached
  pinMode(resetPin, INPUT); 
  sprintf(publishString, "Power Detector Started, Fuel:%.1f%%", fuel.getSoC());
  Particle.publish("pushover", publishString);
}

void loop()
{
  System.sleep(triggerPin, FALLING);
  while(digitalRead(triggerPin) == LOW) // send message every 30mins until power is restored or battery croaks
  {
    unsigned long start = millis();
    while (millis() - start < 1000UL) 
    {
      Particle.process();
    }
    sprintf(publishString, "Power Loss Detected, Fuel:%.1f%%", fuel.getSoC());
    Particle.publish("pushover", publishString);
    System.sleep(resetPin, RISING, REMIND_INTERVAL_MINS * 60); // test every X minutes
  }
    unsigned long start = millis();
    while (millis() - start < 1000UL) 
    {
      Particle.process();
    }
  Particle.publish("pushover", "Particle Power Restored!!");
}

```

---

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 27, 2016, 10:01pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/7 "2016-02-27T22:01:22Z")

</div>

@BulldogLowell I’m getting good results with (though may run afoul of an apparent 25 minute timeout issue “bug”:

```
//@BulldogLowell
//must use develop branch with PR#845 fix merged
#include "application.h"

const int triggerPin = D1;
uint32_t start;
//prevent wasting data on serial identical alerts
bool alreadyOFFAlerted = false;
bool alreadyONAlerted = false;

//10k PULLDOWN resistors on D1
void setup()
{
  pinMode(triggerPin,INPUT); // External Pulldown Resistor attached
}

void loop()
{
  System.sleep(triggerPin, CHANGE);

  start = millis();
  while (millis() - start < 10UL) {Particle.process();}

  if ((digitalRead(triggerPin) == LOW) && (!alreadyOFFAlerted))  
  {
    alreadyONAlerted = false;

    start = millis();
    while (millis() - start < 2000UL) {Particle.process();}

    if(!alreadyOFFAlerted) { Particle.publish("e1", "PowOff");}

    alreadyOFFAlerted = true;
  }

if ((digitalRead(triggerPin) == HIGH) && (!alreadyONAlerted))
{
    alreadyOFFAlerted = false;
    start = millis();
    while (millis() - start < 2000UL) {Particle.process();}

    Particle.publish("e1", "PowOn");

    alreadyONAlerted = true;
}
}

```

Edit: Made some timing tweaks  
Edit2: Could move System.sleep to bottom of loop with waketime added for periodic monitoring, e.g.,

```
  start = millis();
  while (millis() - start < 1000UL) {Particle.process();}

#ifdef Repeat_Status
//won't yet work beyond 24-25 min
System.sleep(triggerPin, CHANGE, 20);//reports every 20 secs 
  alreadyONAlerted = false;
  alreadyOFFAlerted = false;
#else
  System.sleep(triggerPin, CHANGE);
#endif

  start = millis();
  while (millis() - start < 1000UL) {Particle.process();}

}//loop
```

---

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [February 27, 2016, 11:20pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/8 "2016-02-27T23:20:40Z")

</div>

@bpr,

Not here… I gotta figure out how to do the local build with the Git repo (mac).

It seems to me that this should be an easy one… and one that was conceived of during testing.

😉

---

<div class="post-metadata">

### Author: ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)
#### Post date: [February 28, 2016, 3:26am UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/9 "2016-02-28T03:26:44Z")

</div>

@bpr Hey how did you go about adding all the programs required to build locally? Are you using Windows? I’m on Windows 8.

I use this [Toolchain for Windows Installer](https://community.particle.io/t/toolchain-for-windows-installer/13217/17) but the Mingw.zip program did not install correctly so I need to install that manually. You can choose to not install that via clicking the box next to that program.

Are there any instructions you are following that taught you the flow of building locally?

---

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 28, 2016, 3:36am UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/10 "2016-02-28T03:36:16Z")

</div>

I’m on Windows 10 now. I’m a little fuzzy on the whole process, it’s been a little while. This thread documents most of my stumbling around to the final success: [Photon, local build, on Windows](https://community.particle.io/t/photon-local-build-on-windows/12777)  
I lucked out and somehow manually installed it all without @mumblepins well-regarded installer  
Turned out to essentially be the same to set up the toolchain on 10 as on win 7, i think, though I found and installed a more recent version of gcc (5.2.1 20151202).  
The key for me was the path order stuff as mentioned in that thread

---

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [February 28, 2016, 4:05pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/11 "2016-02-28T16:05:16Z")

</div>

@bpr,

So, I noticed that I was getting pretty consistent messages at startup. Reviewing the [documentation on sleep](https://docs.particle.io/reference/firmware/electron/#sleep-sleep-) (_which seems to skirt the whole wakeup part, ironically_) I scrolled lower to `reset()` and voilà. It occurred to me that I could try to just trigger a reset. Well it simplified everything and it \*\*works.

latest:

```cpp
const int triggerPin = D1;

FuelGauge fuel;

char publishString[128] = "";

void setup()
{
  pinMode(triggerPin,INPUT); // External Pulldown Resistor attached
  int startupTime = millis();
  while(millis() - startupTime < 2000UL) // allowing time to take digitalRead()
  {
      Particle.process();
  }
  sprintf(publishString, "Power %s, Fuel:%.1f%%", (digitalRead(triggerPin) == HIGH) ? "Restored" : "NOT Detected", fuel.getSoC() );
  Particle.publish("pushover", publishString);
}

void loop()
{
  System.sleep(triggerPin, CHANGE);
  System.reset();
}

```

\*\* _**by works, I mean my Electron seems to take a lot of time negotiating with the cellular network, occasionally timing out (burst of red flashing) but eventually attaches and I get the message.**_

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/5/52b05428343c1078b1d0321af2fb728381f208cb.PNG)

---

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 28, 2016, 5:00pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/12 "2016-02-28T17:00:47Z")

</div>

Nice and simple. Just don’t use it if it’s likely to trigger often. The data use penalty for renegotiating the cellular connection is very very high.

---

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [February 28, 2016, 5:15pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/13 "2016-02-28T17:15:10Z")

</div>

@bpr,

Well, it is hard to optimize when one cannot figure out how to simply reconnect to cellular!

Right now I am using a SIM card from my unlimited data plan, and I’m not sure If I will even use this application.

Trying to come up with a use for my new Electron, I thought to myself, “what is the easiest and most obvious use of this device” and came up with this one.

BUT, I know that I will enjoy the ride up the learning curve again!

thanks for your ongoing assistance!

---

<div class="post-metadata">

### Author: ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)
#### Post date: [February 28, 2016, 5:48pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/14 "2016-02-28T17:48:12Z")

</div>

Yea same here. One use I have is a simple mail box delivery detector but that’s not going to work how I want it until System.sleep() is working correctly. I have not gotten any feedback from @Bdub on the latest System.sleep issue where it will not work after 25 mins but considering its a critical feature I’m sure it will get fixed sometime in the future 😄

---

<div class="post-metadata">

### Author: ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)
#### Post date: [March 4, 2016, 11:39pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/15 "2016-03-04T23:39:37Z")

</div>

@RWB, @bpr

I’m not seeing any Elites chiming in on this issue… maybe the groundhog didn’t see its shadow.

---

<div class="post-metadata">

### Author: ![WoobaGooba](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/woobagooba/32/16369_2.png) [@WoobaGooba](https://community.particle.io/u/WoobaGooba)
#### Post date: [January 1, 2018, 4:38pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/16 "2018-01-01T16:38:29Z")

</div>

Bump.

Is there a way to programmatically detect Electron loss of USB power source (without requiring external circuitry)?

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [January 1, 2018, 6:26pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/17 "2018-01-01T18:26:34Z")

</div>

There is another thread that might be intersting in this connection

> [@When is Electron Powered By Battery](https://community.particle.io/t/when-is-electron-powered-by-battery/20702/15):
>
> This is a deceptively difficult thing to detect. I’m not sure the method here is the best way to do it, but this is the first method I’ve found that actually produces the correct result, so there is that. I believe the problem is that when there’s no battery, the PMIC doesn’t go into some easy-to-read no battery state. It rapidly switches between fast charging and charge complete states, continuously. it’s possible to detect this, and this sample code breaks out three things that are sort of u…

---

<div class="post-metadata">

### Author: ![WoobaGooba](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/woobagooba/32/16369_2.png) [@WoobaGooba](https://community.particle.io/u/WoobaGooba)
#### Post date: [January 8, 2018, 10:38pm UTC](https://community.particle.io/t/basic-power-loss-detection-electron/20536/18 "2018-01-08T22:38:13Z")

</div>

Found it. PMIC::isPowerGood(). Thanks
