# Sleep Electron if no cell connection is available

**URL:** <https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540>\
**Category:** General\
**Created:** [July 13, 2017, 4:11pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540 "2017-07-13T16:11:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cliquot22](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cliquot22](https://community.particle.io/u/cliquot22)\
**Post date:** [July 13, 2017, 4:11pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540/1 "2017-07-13T16:11:38Z")

</div>

I have an Electron that wakes up, publishes data to the cloud, then goes back to sleep. Sometimes I am out of cellular service range though. Is there a way to run a timer and put the Electron to sleep after some set time if there is no cellular connection available?

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [July 13, 2017, 4:15pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540/2 "2017-07-13T16:15:18Z")

</div>

It’s all in the docs: [https://docs.particle.io/reference/firmware/electron/#waiting-for-the-system](https://docs.particle.io/reference/firmware/electron/#waiting-for-the-system)

---

<div class="post-metadata">

**Author:** ![cliquot22](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cliquot22](https://community.particle.io/u/cliquot22)\
**Post date:** [July 13, 2017, 8:25pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540/3 "2017-07-13T20:25:17Z")

</div>

That doesn’t seem to be working. The waitFor() function never times out.  
My Electron (without the antenna connected) blinks green indicating it is searching for cellular. But it won’t be able to find it so I want it to time out and go to sleep. It won’t timeout. Am I missing something?

```auto
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

void setup() {
    Serial.begin(9600);
    delay(2000);
    Serial.println("Connecting");
    Particle.connect();
    
}

void loop() {
    if (waitFor(Particle.connected, 2000)) {
        if (Particle.connected()) {
            Serial.println("connected");
        } else {
            Serial.println("timed out");
        }
        delay(100000);
    }
}

```

---

<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:** [July 13, 2017, 9:16pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540/4 "2017-07-13T21:16:46Z")

</div>

A cellular connection will hardly be established in two seconds from cold start. I can take up to 5 minutes (300000ms).

And after you entered the true branch of the `if(waitFor())` condition, I’d be rather surprised if you ever got a `timed out` message since the very fact that you find yourself there already means `Particle.connected()` _has to be true_ and nothing else.

Rather try this

```cpp
    if (waitFor(Particle.connected, 60000)) {
      Serial.println("connected");
    } else {
      Serial.println("timed out");
    }

```

---

<div class="post-metadata">

**Author:** ![cliquot22](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cliquot22](https://community.particle.io/u/cliquot22)\
**Post date:** [July 13, 2017, 9:41pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540/5 "2017-07-13T21:41:02Z")

</div>

I am trying to put the Electron to sleep if there is no cellular connection available. So, yes, I expect to NOT receive a connection in 2 seconds.

But I see I was mis-interpreting the waitFor function. I want it to evaluate to false when there is no connection. I was able to fix the code to do what I want. Thanks @ScruffR

```cpp
SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
    Serial.begin(9600);
    delay(2000);
    Serial.println("Connecting");
    Particle.connect();
    
}

void loop() {
    if (waitFor(Particle.connected, 10000)) {
        Serial.println("connected");
        delay(100000);
    } else {
        Serial.println("timed out");
        System.sleep(SLEEP_MODE_DEEP, 15);
    }
}

```

---

<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:** [July 13, 2017, 9:46pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540/6 "2017-07-13T21:46:01Z")

</div>

> [@cliquot22](#):
>
> I am trying to put the Electron to sleep if there is no cellular connection available. So, yes, I expect to NOT receive a connection in 2 seconds.

OK, but 2sec is even too little to even check whether there would be a cellular network available.

Even with 10sec your device will probably never do anything else than go back to sleep.

---

<div class="post-metadata">

**Author:** ![cliquot22](https://avatars.discourse-cdn.com/v4/letter/c/8edcca/32.png) [@cliquot22](https://community.particle.io/u/cliquot22)\
**Post date:** [July 13, 2017, 11:11pm UTC](https://community.particle.io/t/sleep-electron-if-no-cell-connection-is-available/34540/7 "2017-07-13T23:11:42Z")

</div>

Understood. That was just for testing (to guarantee no connection).
