# Photon set Particle.connect() Timeout?

**URL:** <https://community.particle.io/t/photon-set-particle-connect-timeout/50207>\
**Category:** Firmware\
**Created:** [June 6, 2019, 6:38am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207 "2019-06-06T06:38:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![roger\_mary](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roger\_mary](https://community.particle.io/u/roger_mary)\
**Post date:** [June 6, 2019, 6:38am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/1 "2019-06-06T06:38:41Z")

</div>

I am attempting to set a timeout for the Particle.connect() function. If Photon is unable to connect to the WiFi within 15s, I want to stop the connection service so user application code can be executed normally. (I would prefer to have SYSTEM\_THREAD remain DISABLED)

With the code below, if Particle.disconnect() is called while Photon is attempting to connect, the approximate behaviour is that execution of the code in loop() takes approximately 20 times longer.

Are there any other solutions?

```auto
SYSTEM_MODE(SEMI_AUTOMATIC);
ApplicationWatchdog wifiWatchDog(15*1000, wifi_disconnect);
unsigned long last_wifi_connect_ms = 0;
unsigned long wifi_reconnect_delay = 15*1000;

void setup(){

  Serial.begin(9600);
  wifiWatchDog.checkin();

  "Some Other Code"
}

void loop()
{
  wifiWatchDog.checkin();
  last_wifi_connect_ms = millis();
  if(!Particle.connected() && (millis() - last_wifi_connect_ms > wifi_reconnect_delay || last_wifi_connect_ms < wifi_reconnect_delay)){
    Serial.printf("\nReattempt WiFi Connection\n");
    if (wifi_reconnect_delay < 4*60*1000){
      wifi_reconnect_delay *= 2;
    }
    Particle.connect();
    if(Particle.connected()){
      wifi_reconnect_delay = DEFAULT_WIFI_RECONNECT_DELAY;
      Serial.printf("\n WiFi Connected\n");
    }
  }
 "Some Other Code"
}

void wifi_disconnect(){
  Particle.disconnect();
  Serial.println("\nWiFi Discounnected\n");
}

```

---

<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:** [June 6, 2019, 7:49am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/2 "2019-06-06T07:49:26Z")

</div>

> [@roger\_mary](#):
>
> I would prefer to have SYSTEM\_THREAD remain DISABLED

Any reason for that?  
Are you using non-AUTOMATIC mode?  
Have you tried a software timer to call `Particle.disconnect()`?  
You can also check `WiFi.ready()` before trying to connect to the cloud.

---

<div class="post-metadata">

**Author:** ![roger\_mary](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roger\_mary](https://community.particle.io/u/roger_mary)\
**Post date:** [June 6, 2019, 7:57am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/3 "2019-06-06T07:57:12Z")

</div>

The application code has been written with single threaded in mind. Having SYSTEM\_THREAD ENABLED may cause some issues.

I am using SEMI\_AUTOMATIC mode.

I did try using Particle.disconnect(). The result is it takes like 20 times longer to execute the code in loop() if Particle.disconnect() is called while Photon is attempting to connect.

According to Particle Doc, WiFi.ready() " will return `true` once the device is connected to the network and has been assigned an IP address. Otherwise, it will return `false` ." I’m not quite sure how that helps before connect to the cloud. Could you please elaborate?

---

<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:** [June 6, 2019, 7:59am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/4 "2019-06-06T07:59:23Z")

</div>

> [@roger\_mary](#):
>
> I am using SEMI\_AUTOMATIC mode.

That wasn't there originally 😉

> [@roger\_mary](#):
>
> I’m not quite sure how that helps before connect to the cloud. Could you please elaborate?

The actual cloud connection should take about one second _when_ the device is already connected to the WiFi network.  
`Particle.connect()` wihtout being connected to WiFi first will implicitly take all the prerequisite steps but keep blocking for the entire process. If you break that up into individual steps your code will stay in charge more and can be more granular in deciding when to do what and how lown you'd be prepared to wait for the sub-steps.

> [@roger\_mary](#):
>
> The application code has been written with single threaded in mind. Having SYSTEM\_THREAD ENABLED may cause some issues.

_"may cause"_ shouldn't be the excluding argument.  
I cannot really see a lot of cases where the occasional `waitFor()` statement sprinkled in would help pre-/circumvent any issue.

---

<div class="post-metadata">

**Author:** ![roger\_mary](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roger\_mary](https://community.particle.io/u/roger_mary)\
**Post date:** [June 6, 2019, 8:08am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/5 "2019-06-06T08:08:16Z")

</div>

If I understood correctly, should I call `WiFi.connect()` then `Particle.connect()`?

What’s the differences between `WiFi.connect()` and `Particle.connect()` other than one connects to WiFi while the other one connects to Particle cloud?

Is there any secret system function that would check for the availability of WiFi networks stored in Photon?

---

<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:** [June 6, 2019, 8:14am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/6 "2019-06-06T08:14:26Z")

</div>

> [@roger\_mary](#):
>
> What’s the differences between `WiFi.connect()` and `Particle.connect()` other than one connects to WiFi while the other one connects to Particle cloud?

As indicated above `Particle.connect()` - when called without any prerequisite calls - will internally call `WiFi.on()` and wait, `WiFi.connect()` and wait and finally do its own cloud connect stuff including DNS resolution, finding the "nearest" Particle cloud server and then connect to that.

`WiFi.connect()` will only call `WiFi.on()` (when needed) and wait and then connect to the local WiFi AP to acquire an IP - that's it.

> [@roger\_mary](#):
>
> Is there any secret system function that would check for the availability of WiFi networks stored in Photon?

No, no secret function ...

... but there is a clearly documented one here  
[https://docs.particle.io/reference/device-os/firmware/photon/#getcredentials-](https://docs.particle.io/reference/device-os/firmware/photon/#getcredentials-)

And in combination with [`WiFi.scan()`](https://docs.particle.io/reference/device-os/firmware/photon/#scan-) you can correlate the stored networks with the currently available ones.

---

<div class="post-metadata">

**Author:** ![roger\_mary](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roger\_mary](https://community.particle.io/u/roger_mary)\
**Post date:** [June 6, 2019, 8:19am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/7 "2019-06-06T08:19:45Z")

</div>

Thank you! WiFi.getCredentials() and WiFi.scan() is the solution that I’m looking for.

---

<div class="post-metadata">

**Author:** ![roger\_mary](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roger\_mary](https://community.particle.io/u/roger_mary)\
**Post date:** [June 8, 2019, 7:59am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/8 "2019-06-08T07:59:48Z")

</div>

If photon was already connected to the cloud, if the WiFi signal disappears, photon automatically attempt to reconnect(Signal light flashing green). Is there a way to disable that? I have tried to use `Particle.disconnect()` and `WiFi.disconnect()`, none of which can stop photon from keep reconnecting(Signal light flashing green).

---

<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:** [June 8, 2019, 11:00am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/9 "2019-06-08T11:00:33Z")

</div>

> [@roger\_mary](#):
>
> Is there a way to disable that?

Are you sure you want that?  
You may have very brief WiFi "outages" (e.g. regular IP release) that automatism would overcome without too much interruption, but if you disabled the auto-reconnect you'd have to deal with all of these in your code too.

But if you want to take things in your own hands, you should use `SYSTEM_MODE(MANUAL)`, `SYSTEM_THREAD(ENABLED)` and subscribe to the relevant [system events](https://docs.particle.io/reference/device-os/firmware/photon/#system-events-reference)

---

<div class="post-metadata">

**Author:** ![roger\_mary](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roger\_mary](https://community.particle.io/u/roger_mary)\
**Post date:** [June 8, 2019, 2:39pm UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/10 "2019-06-08T14:39:44Z")

</div>

The thought of wanting to manually control the WiFi that is because after WiFi signal has been lost, probably due to resources used by re-connection, loop() is called by the system approximately once every 40 seconds under my current application code, which is way too long.  
Note: SYSTEM\_THREAD has been ENABLED

Furthermore, from my own tests, the description under System Threads Enabled differs from the actual behavior.  
Below is the documented behavior:  
`System mode `AUTOMATIC` mode connects to the cloud as soon as possible. Neither has an effect on when the application `setup()` function is run - it is run as soon as possible, independently from the system network activities, as described above.`  
The behavior that I have encountered was:  
`System mode `AUTOMATIC` mode will attempt to connect to cloud ASAP and setup() is block even if SYSTEM_THREAD has been ENABLED`

---

<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:** [June 8, 2019, 6:50pm UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/11 "2019-06-08T18:50:46Z")

</div>

> [@roger\_mary](#):
>
> The behavior that I have encountered was:  
> `System mode `AUTOMATIC` mode will attempt to connect to cloud ASAP and setup() is block even if SYSTEM_THREAD has been ENABLED`

I can't confirm that.  
What device OS version are you running?  
Can you provide the `particle serial inspect` log of your device?

---

<div class="post-metadata">

**Author:** ![roger\_mary](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roger\_mary](https://community.particle.io/u/roger_mary)\
**Post date:** [June 10, 2019, 3:03am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/12 "2019-06-10T03:03:52Z")

</div>

Device OS v1.0.1

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/c/e/ce0025435e7132c39fde36468068677b94b870e3.png)

---

<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:** [June 10, 2019, 6:40am UTC](https://community.particle.io/t/photon-set-particle-connect-timeout/50207/13 "2019-06-10T06:40:27Z")

</div>

I have tested with 1.1.0 and 1.2.1-rc.2 and also know that `SYSTEM_THREAD(ENABLED)` did in earlier versions, although I can’t vouch for 1.0.1 explicitly.

Can you also provide some test code to test on the same basis?
